Class: KDL::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/kdl/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, arguments = [], properties = {}, children = [], type: nil) ⇒ Node

Returns a new instance of Node.



5
6
7
8
9
10
11
# File 'lib/kdl/node.rb', line 5

def initialize(name, arguments = [], properties = {}, children = [], type: nil)
  @name = name
  @arguments = arguments
  @properties = properties
  @children = children
  @type = type
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



3
4
5
# File 'lib/kdl/node.rb', line 3

def arguments
  @arguments
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/kdl/node.rb', line 3

def children
  @children
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/kdl/node.rb', line 3

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



3
4
5
# File 'lib/kdl/node.rb', line 3

def properties
  @properties
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/kdl/node.rb', line 3

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/kdl/node.rb', line 30

def ==(other)
  return false unless other.is_a?(Node)

  name       == other.name       &&
  arguments  == other.arguments  &&
  properties == other.properties &&
  children   == other.children
end

#as_type(type, parser = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kdl/node.rb', line 39

def as_type(type, parser = nil)
  if parser.nil?
    @type = type
    self
  else
    result = parser.call(self, type)

    return self.as_type(type) if result.nil?

    unless result.is_a?(::KDL::Node)
      raise ArgumentError, "expected parser to return an instance of ::KDL::Node, got `#{result.class}'"
    end

    result
  end
end

#to_s(level = 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kdl/node.rb', line 13

def to_s(level = 0)
  indent = '    ' * level
  s = "#{indent}#{type ? "(#{id_to_s type})" : ''}#{id_to_s name}"
  unless arguments.empty?
    s += " #{arguments.map(&:to_s).join(' ')}"
  end
  unless properties.empty?
    s += " #{properties.map { |k, v| "#{id_to_s k}=#{v}" }.join(' ')}"
  end
  unless children.empty?
    s += " {\n"
    s += children.map { |c| "#{c.to_s(level + 1)}\n" }.join("\n")
    s += "#{indent}}"
  end
  s
end