Class: Titi::Matcher::MatchAttribute

Inherits:
Base
  • Object
show all
Defined in:
lib/titi/ignore/matcher.rb

Overview

Concrete subclass of IMW::Parsers::HtmlMatchers::Matcher for matching an attribute of the first element of a document matching a selector.

Instance Attribute Summary collapse

Attributes inherited from Base

#matcher, #options, #selector

Instance Method Summary collapse

Constructor Details

#initialize(selector, attribute, matcher = nil) ⇒ MatchAttribute

Unlike IMW::Parsers::HtmlMatchers::Matcher, IMW::Parsers::HtmlMatchers::MatchAttribute is initialized with three arguments: the selector which collects elements from an HTML document, an attribute to extract, and (optionally) a matcher to perform the matching.



109
110
111
112
# File 'lib/titi/ignore/matcher.rb', line 109

def initialize selector, attribute, matcher=nil
  super selector, matcher
  self.attribute = attribute.to_s
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



102
103
104
# File 'lib/titi/ignore/matcher.rb', line 102

def attribute
  @attribute
end

Instance Method Details

#match(doc) ⇒ Object

Grab the first element from doc matching the selector this class was initialized with. If initialized with a matcher, then return the matcher‘s match against the value of the attribute this class was initialized with, else just return the value of the attribute.

m = MatchAttribute.new('span#bio/a.homepage', 'href')
m.match('<span id="bio"><a class="homepage" href="http://foo.bar">My Homepage</a></span>')
# => 'http://foo.bar'


123
124
125
126
127
# File 'lib/titi/ignore/matcher.rb', line 123

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)
  val = doc.path_attr(selector, attribute)
  matcher ? matcher.match(val) : val
end