Class: Mochigome::DataNode

Inherits:
ActiveSupport::OrderedHash
  • Object
show all
Defined in:
lib/data_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_name, name, content = []) ⇒ DataNode

Returns a new instance of DataNode.



10
11
12
13
14
15
16
17
18
# File 'lib/data_node.rb', line 10

def initialize(type_name, name, content = [])
  # Convert content keys to symbols
  super()
  self.merge!(content)
  @type_name = type_name.to_s
  @name = name.to_s
  @comment = nil
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/data_node.rb', line 8

def children
  @children
end

#commentObject

Returns the value of attribute comment.



7
8
9
# File 'lib/data_node.rb', line 7

def comment
  @comment
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#type_nameObject

Returns the value of attribute type_name.



5
6
7
# File 'lib/data_node.rb', line 5

def type_name
  @type_name
end

Instance Method Details

#/(idx) ⇒ Object



41
42
43
# File 'lib/data_node.rb', line 41

def /(idx)
  @children[idx]
end

#<<(item) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/data_node.rb', line 30

def <<(item)
  if item.is_a?(Array)
    item.map {|i| self << i}
  elsif item.is_a?(DataNode)
    @children << item
    @children.last
  else
    raise DataNodeError.new("Can't adopt #{item.inspect}, it's not a DataNode")
  end
end

#cloneObject



45
46
47
48
49
# File 'lib/data_node.rb', line 45

def clone
  twin = super
  twin.instance_variable_set(:@children, @children.map{|c| c.clone})
  twin
end

#merge!(a) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/data_node.rb', line 20

def merge!(a)
  if a.is_a?(Array)
    a.each do |h|
      self[h.keys.first.to_sym] = h.values.first
    end
  else
    super
  end
end

#to_flat_arraysObject



64
65
66
67
68
69
70
# File 'lib/data_node.rb', line 64

def to_flat_arrays
  table = []
  col_names = flat_column_names
  table << col_names
  append_rows_to(table, col_names)
  table
end

#to_flat_ruport_tableObject



57
58
59
60
61
62
# File 'lib/data_node.rb', line 57

def to_flat_ruport_table
  col_names = flat_column_names
  table = Ruport::Data::Table.new(:column_names => col_names)
  append_rows_to(table, col_names)
  table
end

#to_xmlObject



51
52
53
54
55
# File 'lib/data_node.rb', line 51

def to_xml
  doc = Nokogiri::XML::Document.new
  append_xml_to(doc)
  doc
end