Class: KDL::Node
Defined Under Namespace
Classes: Custom
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #arg(key) ⇒ Object
- #args(key) ⇒ Object
- #as_type(type, parser = nil) ⇒ Object
- #child(key) ⇒ Object
- #dash_vals(key) ⇒ Object
- #each(&block) ⇒ Object
- #each_arg(key, &block) ⇒ Object
- #each_dash_val(key, &block) ⇒ Object
-
#initialize(name, _args = [], _props = {}, _children = [], arguments: _args, properties: _props, children: _children, type: nil) ⇒ Node
constructor
A new instance of Node.
- #inspect(level = 0) ⇒ Object
- #to_s(level = 0, m = :to_s) ⇒ Object
- #to_v1 ⇒ Object
- #to_v2 ⇒ Object
- #version ⇒ Object
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
#arguments ⇒ Object
Returns the value of attribute arguments.
25 26 27 |
# File 'lib/kdl/node.rb', line 25 def arguments @arguments end |
#children ⇒ Object
Returns the value of attribute children.
25 26 27 |
# File 'lib/kdl/node.rb', line 25 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
25 26 27 |
# File 'lib/kdl/node.rb', line 25 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties.
25 26 27 |
# File 'lib/kdl/node.rb', line 25 def properties @properties end |
#type ⇒ Object
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_v1 ⇒ Object
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_v2 ⇒ Object
148 149 150 |
# File 'lib/kdl/node.rb', line 148 def to_v2 self end |
#version ⇒ Object
144 145 146 |
# File 'lib/kdl/node.rb', line 144 def version 2 end |