Class: Sergio::ParsedDocument

Inherits:
Object
  • Object
show all
Includes:
HashMethods
Defined in:
lib/sergio/sergio_parsed_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashMethods

#hash_from_path, #hash_recursive_merge, #hash_recursive_merge_to_arrays, #value_at_path

Constructor Details

#initializeParsedDocument

Returns a new instance of ParsedDocument.



6
7
8
# File 'lib/sergio/sergio_parsed_document.rb', line 6

def initialize
  @parsed_hash = {}
end

Instance Attribute Details

#parsed_hashObject

Returns the value of attribute parsed_hash.



4
5
6
# File 'lib/sergio/sergio_parsed_document.rb', line 4

def parsed_hash
  @parsed_hash
end

Instance Method Details

#set_element(path, val, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sergio/sergio_parsed_document.rb', line 10

def set_element(path, val, options = {})
  v = value_at_path(path.clone, self.parsed_hash)

  val = if v
    if v.is_a? Array
      v << val
    else
      if val.is_a?(Hash) && val.empty?
        options[:as_array] ? [v] : v
      else
        [v] << val
      end
    end
  else
    options[:as_array] ? [val] : val
  end

  h = hash_from_path(path, val)
  @parsed_hash = hash_recursive_merge(self.parsed_hash, h)
  @parsed_hash
end