Class: KDL::Node

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

Direct Known Subclasses

Custom, V1::Node

Defined Under Namespace

Classes: Custom

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



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

def initialize(name, _args = [], _props = {}, _children = [],
  arguments: _args,
  properties: _props,
  children: _children,
  type: nil
)
  @name = name
  @arguments = arguments
  @properties = properties.transform_keys(&:to_s)
  @children = children
  @type = type
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



25
26
27
# File 'lib/kdl/node.rb', line 25

def arguments
  @arguments
end

#childrenObject

Returns the value of attribute children.



25
26
27
# File 'lib/kdl/node.rb', line 25

def children
  @children
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/kdl/node.rb', line 25

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



25
26
27
# File 'lib/kdl/node.rb', line 25

def properties
  @properties
end

#typeObject

Returns the value of attribute type.



25
26
27
# File 'lib/kdl/node.rb', line 25

def type
  @type
end

Instance Method Details

#<<(node) ⇒ Object



51
52
53
# File 'lib/kdl/node.rb', line 51

def <<(node)
  children << node
end

#<=>(other) ⇒ Object



93
94
95
# File 'lib/kdl/node.rb', line 93

def <=>(other)
  name <=> other.name
end

#==(other) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/kdl/node.rb', line 118

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

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

#[](key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/kdl/node.rb', line 40

def [](key)
  case key
  when Integer
    arguments[key]&.value
  when String, Symbol
    properties[key.to_s]&.value
  else
    raise ArgumentError, "node can only be indexed by Integer, String, or Symbol"
  end
end

#arg(key) ⇒ Object



66
67
68
# File 'lib/kdl/node.rb', line 66

def arg(key)
  child(key)&.arguments&.first&.value
end

#args(key) ⇒ Object



70
71
72
# File 'lib/kdl/node.rb', line 70

def args(key)
  child(key)&.arguments&.map(&:value)
end

#as_type(type, parser = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/kdl/node.rb', line 127

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::Custom)
      raise ArgumentError, "expected parser to return an instance of ::KDL::Node::Custom, got `#{result.class}'"
    end

    result
  end
end

#child(key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/kdl/node.rb', line 55

def child(key)
  case key
  when Integer
    children[key]
  when String, Symbol
    children.find { _1.name == key.to_s }
  else
    raise ArgumentError, "node can only be indexed by Integer, String, or Symbol"
  end
end

#dash_vals(key) ⇒ Object



78
79
80
81
82
83
# File 'lib/kdl/node.rb', line 78

def dash_vals(key)
  child(key)
    &.children
    &.select { _1.name == "-" }
    &.map { _1.arguments.first&.value }
end

#each(&block) ⇒ Object



89
90
91
# File 'lib/kdl/node.rb', line 89

def each(&block)
  children.each(&block)
end

#each_arg(key, &block) ⇒ Object



74
75
76
# File 'lib/kdl/node.rb', line 74

def each_arg(key, &block)
  args(key)&.each(&block)
end

#each_dash_val(key, &block) ⇒ Object



85
86
87
# File 'lib/kdl/node.rb', line 85

def each_dash_val(key, &block)
  dash_vals(key)&.each(&block)
end

#inspect(level = 0) ⇒ Object



114
115
116
# File 'lib/kdl/node.rb', line 114

def inspect(level = 0)
  to_s(level, :inspect)
end

#to_s(level = 0, m = :to_s) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kdl/node.rb', line 97

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

#to_v1Object



152
153
154
155
156
157
158
159
# File 'lib/kdl/node.rb', line 152

def to_v1
  ::KDL::V1::Node.new(name,
    arguments: arguments.map(&:to_v1),
    properties: properties.transform_values(&:to_v1),
    children: children.map(&:to_v1),
    type: type
  )
end

#to_v2Object



148
149
150
# File 'lib/kdl/node.rb', line 148

def to_v2
  self
end

#versionObject



144
145
146
# File 'lib/kdl/node.rb', line 144

def version
  2
end