Class: HtmlEntry::Page::ValuesCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/html_entry/page/values_collector.rb

Overview

This class responsible for getting values according to an instruction

See Also:

  • tests/html_entry/page/test_entity_fetchertests/html_entry/page/test_entity_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ValuesCollector

Returns a new instance of ValuesCollector.



28
29
30
31
# File 'lib/html_entry/page/values_collector.rb', line 28

def initialize(options = {})
  @options = options
  @data    = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



26
27
28
# File 'lib/html_entry/page/values_collector.rb', line 26

def data
  @data
end

Instance Method Details

#fetch(name, instruction, node) ⇒ String, Nokogiri::XML::Element

Fetch value of element

Parameters:

  • name (Symbol)
  • instruction (Hash)
  • node (Nokogiri::XML::Element)

Returns:

  • (String, Nokogiri::XML::Element)


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
# File 'lib/html_entry/page/values_collector.rb', line 41

def fetch(name, instruction, node)
  if node && (instruction[:type] == :attribute)
    value = get_node_attribute(
      node,
      instruction
    )
  elsif instruction[:type] == :function
    value = call_function(name, instruction)
  elsif instruction[:type] == :boolean || instruction[:type] == :bool
    value = !!node
  elsif node && instruction[:type] == :children
    value = children(
      name:        name,
      instruction: instruction,
      node:        node,
      plenty:      if instruction[:children_plenty].nil?
                     true
                   else
                     instruction[:children_plenty]
                   end
    )
  elsif node && (instruction[:type] == :value || instruction[:type].nil?)
    # empty type should be determined as :value
    value = node
  elsif instruction.is_a?(Hash) && !instruction[:default].nil?
    value = instruction[:default]
  elsif node.nil?
    value = nil
  else
    raise HtmlEntry::Error, 'Unknown instruction type or XML/HTML value not found.'
  end

  value = filter_value(value, instruction)
  if data[name].instance_of?(Array) && value.instance_of?(Array)
    data[name] = [data[name], value].flatten
  else
    unless data[name].nil? && (!instruction[:overwrite])
      raise "Value already set for data key name '#{name}'."
    end
    data[name] = value
  end

  data[name]
end