Class: HtmlEntry::Page::ValuesCollector
- Inherits:
-
Object
- Object
- HtmlEntry::Page::ValuesCollector
- Defined in:
- lib/html_entry/page/values_collector.rb
Overview
This class responsible for getting values according to an instruction
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#fetch(name, instruction, node) ⇒ String, Nokogiri::XML::Element
Fetch value of element.
-
#initialize(options = {}) ⇒ ValuesCollector
constructor
A new instance of ValuesCollector.
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 = @data = {} end |
Instance Attribute Details
#data ⇒ Object (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
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 |