Method: Titi::Matcher::MatchHash#match

Defined in:
lib/titi/ignore/matcher.rb

#match(doc) ⇒ Object

Use the match_hash this MatchHash was initialized with to select elements from doc and extract information from them:

m = MatchHash.new({
    :name         => MatchFirstElement.new('li/span.customer'),
    :order_status => MatchAttribute.new('li/ul[@status]','status'),
    :products     => MatchArray.new('li/ul/li')
  })
m.match('<li><span class="customer">John Chimpo</span>
             <ul status="shipped">
               <li>bananas</li>
               <li>mangos</li>
               <li>banangos</li>
             </ul></li>')
# => {
      :name         => "John Chimpo",
      :order_status => "shipped",
      :products     => ["bananas", "mangos", "banangos"]
     }


235
236
237
238
239
240
241
242
243
244
245
# File 'lib/titi/ignore/matcher.rb', line 235

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)
  hsh = { }
  match_hash.each do |attr, m|
    val = m.match(doc)
    case attr
    when Array then hsh.merge!(Hash.zip(attr, val).reject{|k,v| v.nil? }) if val
    else            hsh[attr] = val  end
  end
  self.class.scrub!(hsh)
end