Class: Saxerator::Builder::HashElement

Inherits:
Hash
  • Object
show all
Defined in:
lib/mits/saxerator_ext.rb

Instance Method Summary collapse

Instance Method Details

#deep_hashObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/mits/saxerator_ext.rb', line 4

def deep_hash
  reduce({}) do |memo, (key, value)|
    as_hash = if value.is_a? Array
                value.map { |v| try_hash(v) }
              else
                try_hash(value)
              end
    memo[key] = as_hash
    memo
  end
end

#try_hash(val) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mits/saxerator_ext.rb', line 16

def try_hash(val)
  if val.is_a? EmptyElement
    nil
  elsif val.respond_to? :deep_hash
    val.deep_hash
  elsif val.respond_to? :to_h
    val.to_h
  else
    val
  end
end