Class: Microformat::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/microformat/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, options) ⇒ Parser

Returns a new instance of Parser.



18
19
20
21
22
# File 'lib/microformat/parser.rb', line 18

def initialize(doc, options)
  @doc = doc
  @options = options
  parse(elements)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



16
17
18
# File 'lib/microformat/parser.rb', line 16

def doc
  @doc
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/microformat/parser.rb', line 16

def options
  @options
end

Class Method Details

.parse(doc, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/microformat/parser.rb', line 7

def self.parse(doc, options = {})
  # ensure the document is parsed
  unless doc.respond_to?(:css)
    doc = Nokogiri::HTML(doc)
  end
  # return the collection
  new(doc, options).collection
end

Instance Method Details

#collectionObject



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

def collection
  @collection ||= Collection.new
end

#limitObject



31
32
33
# File 'lib/microformat/parser.rb', line 31

def limit
  @limit ||= options[:limit] || Float::INFINITY
end

#parse(elements) ⇒ Object



24
25
26
27
28
29
# File 'lib/microformat/parser.rb', line 24

def parse(elements)
  i = 0; while i < [limit, elements.size].min do
    collection << elements[i]
    i += 1
  end
end

#selectorsObject



35
36
37
# File 'lib/microformat/parser.rb', line 35

def selectors
  @selectors ||= Selectors.filter(options[:filter])
end