Class: C::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/cast-to-yaml/to_h.rb,
lib/cast-to-yaml/to_yaml.rb

Instance Method Summary collapse

Instance Method Details

#to_hObject

Serialize a node to a Hash representation.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cast-to-yaml/to_h.rb', line 8

def to_h
  res = {}
  kind = self.class.kind
  res["kind"] = kind
  fields.each do |f|
    name = f.init_key.to_s
    value = self.send(f.reader)
    if value && !(value == f.make_default)
      res[name] =
        if f.child?
          if value.kind_of? C::NodeList
            value.collect { |n| n.to_h }
          else
            value.to_h
          end
        else
          value
        end
    end
  end
  return res
end

#to_h_splitObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cast-to-yaml/to_yaml.rb', line 5

def to_h_split
  res = {}
  kind = self.class.kind
  res["kind"] = kind
  fields.each do |f|
    name = f.init_key.to_s
    value = self.send(f.reader)
    if value && !(value == f.make_default)
      res[name] =
        if f.child?
          if value.kind_of? C::NodeList
            value.collect { |n| n.to_h_split }
          else
            value.to_h_split
          end
        else
          value
        end
    end
  end
  return res
end