Class: Klepto::Structure

Inherits:
Object
  • Object
show all
Defined in:
lib/klepto/structure.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

options :collection, :resource options :first, :all options :xpath, :css options Integer elements to structure when :match => :all or :as => :collection



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)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options[:syntax]  ||= :css
  options[:match]   ||= :first
  options[:attr]    ||= nil
  options[:default] ||= nil
  options[:limit]   ||= nil
  options[:parser]  ||= nil
  selector          = args.shift

  if selector.is_a?(Proc)
    selector = selector.call(_bot)
  end

  if !block_given? && options[:parser]
    block = options[:parser].new
  end

  Klepto.logger.debug("\t\tDefining attribute: #{meth} -> #{selector}")

  if options[:as] == :collection
    @_hash[meth] = []
    result = _context.all( options[:syntax], selector )      

    Klepto.logger.debug("\t\t\tAs: collection, Result? #{!result.nil?}")

    options[:limit] ||= result.length
    result[0, options[:limit]].each do |ele|
      @_hash[meth].push Structure.build(ele, self, &block)
    end 

  elsif options[:as] == :resource
    result = _context.first( options[: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( options[:match], options[:syntax], selector ) : _context

    if options[:match] == :all
      Klepto.logger.debug("\t\t\tAs: block (match all), Result? #{!result.nil?}")
      @_hash[meth] = []
      options[:limit] ||= result.length
      result[0, options[: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: #{options[:default]})")
        @_hash[meth] = options[:default]
      end
    end

  else
    result = _context.send( options[:match], options[:syntax], selector )

    if options[:match] == :all
      Klepto.logger.debug("\t\t\tAs: simple (match all), Result? #{!result.nil?}")
      @_hash[meth] = []
      options[:limit] ||= result.length
      result[0, options[:limit]].each do |_node|
        @_hash[meth] << (_node[options[:attr]] || _node.try(:text))
      end        
    elsif result
      Klepto.logger.debug("\t\t\tAs: block (match one)")
      @_hash[meth] = (result[options[:attr]] || result.try(:text))
    else
      Klepto.logger.debug("\t\t\tAs: block (no match, default: #{options[:default]})")
      @_hash[meth] = options[:default]
    end
  end
end

Instance Attribute Details

#_botObject

Returns the value of attribute _bot.



12
13
14
# File 'lib/klepto/structure.rb', line 12

def _bot
  @_bot
end

#_contextObject (readonly)

Returns the value of attribute _context.



11
12
13
# File 'lib/klepto/structure.rb', line 11

def _context
  @_context
end

#_hashObject (readonly)

Returns the value of attribute _hash.



10
11
12
# File 'lib/klepto/structure.rb', line 10

def _hash
  @_hash
end

#_parentObject (readonly)

Returns the value of attribute _parent.



9
10
11
# File 'lib/klepto/structure.rb', line 9

def _parent
  @_parent
end

Class Method Details

.build(_context = nil, _parent = nil, &block) ⇒ Object



3
4
5
6
7
# File 'lib/klepto/structure.rb', line 3

def self.build(_context=nil, _parent=nil, &block)
  structure = Structure.new(_context, _parent)
  structure.instance_eval &block
  structure._hash
end