Class: Scrapes::RuleParser::Rule
- Inherits:
-
Object
- Object
- Scrapes::RuleParser::Rule
- Defined in:
- lib/scrapes/rule_parser.rb
Overview
:nodoc:all
Instance Attribute Summary collapse
-
#extractor ⇒ Object
readonly
Returns the value of attribute extractor.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
-
#initialize(name, selector, extractor, limit) ⇒ Rule
constructor
A new instance of Rule.
- #inspect ⇒ Object
- #process(node, context) ⇒ Object
Constructor Details
#initialize(name, selector, extractor, limit) ⇒ Rule
Returns a new instance of Rule.
230 231 232 |
# File 'lib/scrapes/rule_parser.rb', line 230 def initialize(name, selector, extractor, limit) @name, @selector, @extractor, @limit = name.to_s.intern, selector, extractor, limit end |
Instance Attribute Details
#extractor ⇒ Object (readonly)
Returns the value of attribute extractor.
227 228 229 |
# File 'lib/scrapes/rule_parser.rb', line 227 def extractor @extractor end |
#limit ⇒ Object
Returns the value of attribute limit.
225 226 227 |
# File 'lib/scrapes/rule_parser.rb', line 225 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
224 225 226 |
# File 'lib/scrapes/rule_parser.rb', line 224 def name @name end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
226 227 228 |
# File 'lib/scrapes/rule_parser.rb', line 226 def selector @selector end |
Instance Method Details
#inspect ⇒ Object
257 258 259 |
# File 'lib/scrapes/rule_parser.rb', line 257 def inspect @selector ? "[to #{@name} from #{@selector.inspect}, #{@extractor.inspect}, limit #{@limit}]" : "[to #{@name} from #{@extractor.inspect}, limit #{@limit}]" end |
#process(node, context) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/scrapes/rule_parser.rb', line 235 def process(node, context) context.instance_variable_set '@hpricot', node return true if @limit == 0 result = @selector.call(node) result = [result] unless result.respond_to? :each current = context.instance_variable_set "@#@name", [] result.compact.each do |node| value = case @extractor when UnboundMethod then @extractor.bind(context).call(node) when Extractor then @extractor.extract(node) when Proc, Method then @extractor.call(node) when Class then @extractor.parse(node) end next unless value current << value break if current.size == @limit end context.instance_variable_set "@#@name", current[0] if @limit == 1 true end |