Module: Microformat::Base

Included in:
Microformat, Simple
Defined in:
lib/microformat.rb

Constant Summary collapse

@@subclasses =
Set.new
@@timeout =
5

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



105
106
107
108
109
110
# File 'lib/microformat.rb', line 105

def method_missing(method, *args, &block)
  super unless %w(one many).include? method.to_s
  (collected = collector).instance_eval(&block) if block_given?
  classes = block_given? ? [args.first => collected] : break_out_hashes(args)
  @attributes[method] += classes
end

Instance Method Details

#find(*args) ⇒ Object

The Gateway



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/microformat.rb', line 12

def find(*args)
  return find_in_children(*args) if self == Microformat

  target, @options = args
  @options ||= target.is_a?(Hash) ? target : {}
  [:first, :all].each { |key| target = @options[key] if @options[key] }

  extract_base_url! target
  
  @doc = build_doc(@options[:text] ? @options : target)

  microformats = find_occurences(@doc)
  raise MicroformatNotFound if @options[:strict] && microformats.empty?
  return @options[:first] ? nil : [] if microformats.empty?

  if @options[:first] || @options[:all]
    return @options[:first] ? find_first(microformats) : find_every(microformats)
  end

  object = find_every(microformats)
  case object.size
  when 1 then object.first
  when 0 then nil
  else object
  end
end

#find_in_children(*args) ⇒ Object



39
40
41
42
43
# File 'lib/microformat.rb', line 39

def find_in_children(*args)
  @@subclasses.map do |klass|
    klass.find(*args)
  end.flatten
end

#timeout=(timeout) ⇒ Object



62
63
64
# File 'lib/microformat.rb', line 62

def timeout=(timeout)
  @@timeout = timeout
end