Class: Klepto::Structure
- Inherits:
-
Object
- Object
- Klepto::Structure
- Defined in:
- lib/klepto/structure.rb
Instance Attribute Summary collapse
-
#_bot ⇒ Object
Returns the value of attribute _bot.
-
#_context ⇒ Object
readonly
Returns the value of attribute _context.
-
#_hash ⇒ Object
readonly
Returns the value of attribute _hash.
-
#_parent ⇒ Object
readonly
Returns the value of attribute _parent.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(_context = nil, _parent = nil) ⇒ Structure
constructor
A new instance of Structure.
- #method_missing(meth, *args, &block) ⇒ Object
Constructor Details
#initialize(_context = nil, _parent = nil) ⇒ Structure
Returns a new instance of Structure.
14 15 16 17 18 19 20 |
# File 'lib/klepto/structure.rb', line 14 def initialize(_context=nil, _parent=nil) Klepto.logger.debug("\tnew Structure (#{_parent}) -> (#{_context})") @_context = _context @_parent = _parent @_hash = {} @_after_handler = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/klepto/structure.rb', line 26 def method_missing(meth, *args, &block) = args.last.is_a?(Hash) ? args.pop : {} [:syntax] ||= :css [:match] ||= :first [:attr] ||= nil [:default] ||= nil [:limit] ||= nil [:parser] ||= nil selector = args.shift if selector.is_a?(Proc) selector = selector.call(_bot) end if !block_given? && [:parser] block = [:parser].new end Klepto.logger.debug("\t\tDefining attribute: #{meth} -> #{selector}") if [:as] == :collection @_hash[meth] = [] result = _context.all( [:syntax], selector ) Klepto.logger.debug("\t\t\tAs: collection, Result? #{!result.nil?}") [:limit] ||= result.length result[0, [:limit]].each do |ele| @_hash[meth].push Structure.build(ele, self, &block) end elsif [:as] == :resource result = _context.first( [:syntax], selector ) Klepto.logger.debug("\t\t\tAs: resource, Result? #{!result.nil?}") @_hash[meth] = Structure.build(result, self, &block) elsif block result = selector ? _context.send( [:match], [:syntax], selector ) : _context if [:match] == :all Klepto.logger.debug("\t\t\tAs: block (match all), Result? #{!result.nil?}") @_hash[meth] = [] [:limit] ||= result.length result[0, [:limit]].each do |_node| @_hash[meth] << block.call( _node ) end else if result Klepto.logger.debug("\t\t\tAs: block (match one)") @_hash[meth] = block.call( result ) else Klepto.logger.debug("\t\t\tAs: block (no match, default: #{[:default]})") @_hash[meth] = [:default] end end else result = _context.send( [:match], [:syntax], selector ) if [:match] == :all Klepto.logger.debug("\t\t\tAs: simple (match all), Result? #{!result.nil?}") @_hash[meth] = [] [:limit] ||= result.length result[0, [:limit]].each do |_node| @_hash[meth] << (_node[[:attr]] || _node.try(:text)) end elsif result Klepto.logger.debug("\t\t\tAs: block (match one)") @_hash[meth] = (result[[:attr]] || result.try(:text)) else Klepto.logger.debug("\t\t\tAs: block (no match, default: #{[:default]})") @_hash[meth] = [:default] end end end |
Instance Attribute Details
#_bot ⇒ Object
Returns the value of attribute _bot.
12 13 14 |
# File 'lib/klepto/structure.rb', line 12 def _bot @_bot end |
#_context ⇒ Object (readonly)
Returns the value of attribute _context.
11 12 13 |
# File 'lib/klepto/structure.rb', line 11 def _context @_context end |
#_hash ⇒ Object (readonly)
Returns the value of attribute _hash.
10 11 12 |
# File 'lib/klepto/structure.rb', line 10 def _hash @_hash end |
#_parent ⇒ Object (readonly)
Returns the value of attribute _parent.
9 10 11 |
# File 'lib/klepto/structure.rb', line 9 def _parent @_parent end |