lib/mida/document.rb in mida-0.2.0 vs lib/mida/document.rb in mida-0.3.0
- old
+ new
@@ -2,10 +2,11 @@
module Mida
# Class that holds the extracted Microdata
class Document
+ include Enumerable
# An Array of Mida::Item objects. These are all top-level
# and hence not properties of other Items
attr_reader :items
@@ -18,48 +19,49 @@
@doc = Nokogiri(target)
@page_url = page_url
@items = extract_items
end
- # Returns an array of matching Mida::Item objects
- #
- # [vocabulary] A regexp to match the item types against
- # or a Class derived from Mida::VocabularyDesc
- # to match against
- def search(vocabulary, items=@items)
- found_items = []
- regexp_passed = vocabulary.kind_of?(Regexp)
- regexp = if regexp_passed then vocabulary else vocabulary.itemtype end
+ # Implements method for Enumerable
+ def each
+ @items.each {|item| yield(item)}
+ end
- items.each do |item|
+ # Returns an array of matching <tt>Mida::Item</tt> objects
+ #
+ # This drills down through each +Item+ to find match items
+ #
+ # [itemtype] A regexp to match the item types against
+ # [items] An array of items to search. If no argument supplied, will
+ # search through all items in the document.
+ def search(itemtype, items=@items)
+ items.each_with_object([]) do |item, found_items|
# Allows matching against empty string, otherwise couldn't match
# as item.type can be nil
- if (item.type.nil? && "" =~ regexp) || (item.type =~ regexp)
+ if (item.type.nil? && "" =~ itemtype) || (item.type =~ itemtype)
found_items << item
end
- found_items += search_values(item.properties.values, regexp)
+ found_items.concat(search_values(item.properties.values, itemtype))
end
- found_items
end
private
def extract_items
itemscopes = @doc.search('//*[@itemscope and not(@itemprop)]')
return nil unless itemscopes
itemscopes.collect do |itemscope|
- Item.new(itemscope, @page_url)
+ itemscope = Itemscope.new(itemscope, @page_url)
+ Item.new(itemscope)
end
end
def search_values(values, vocabulary)
- items = []
- values.each do |value|
- if value.is_a?(Mida::Item) then items += search(vocabulary, [value])
- elsif value.is_a?(Array) then items += search_values(value, vocabulary)
+ values.each_with_object([]) do |value, items|
+ if value.is_a?(Item) then items.concat(search(vocabulary, [value]))
+ elsif value.is_a?(Array) then items.concat(search_values(value, vocabulary))
end
end
- items
end
end
end