Class: KDL::Document
Direct Known Subclasses
Instance Attribute Summary collapse
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #arg(key) ⇒ Object
- #args(key) ⇒ Object
- #dash_vals(key) ⇒ Object
- #each(&block) ⇒ Object
- #each_arg(key, &block) ⇒ Object
- #each_dash_val(key, &block) ⇒ Object
-
#initialize(nodes = []) ⇒ Document
constructor
A new instance of Document.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #to_v1 ⇒ Object
- #to_v2 ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(nodes = []) ⇒ Document
Returns a new instance of Document.
9 10 11 |
# File 'lib/kdl/document.rb', line 9 def initialize(nodes = []) @nodes = nodes end |
Instance Attribute Details
#nodes ⇒ Object
Returns the value of attribute nodes.
7 8 9 |
# File 'lib/kdl/document.rb', line 7 def nodes @nodes end |
Instance Method Details
#<<(node) ⇒ Object
24 25 26 |
# File 'lib/kdl/document.rb', line 24 def <<(node) nodes << node end |
#==(other) ⇒ Object
63 64 65 66 67 |
# File 'lib/kdl/document.rb', line 63 def ==(other) return false unless other.is_a?(Document) nodes == other.nodes end |
#[](key) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/kdl/document.rb', line 13 def [](key) case key when Integer nodes[key] when String, Symbol nodes.find { _1.name == key.to_s } else raise ArgumentError, "document can only be indexed by Integer, String, or Symbol" end end |
#arg(key) ⇒ Object
28 29 30 |
# File 'lib/kdl/document.rb', line 28 def arg(key) self[key]&.arguments&.first&.value end |
#args(key) ⇒ Object
32 33 34 |
# File 'lib/kdl/document.rb', line 32 def args(key) self[key]&.arguments&.map(&:value) end |
#dash_vals(key) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/kdl/document.rb', line 40 def dash_vals(key) self[key] &.children &.select { _1.name == "-" } &.map { _1.arguments.first&.value } end |
#each(&block) ⇒ Object
51 52 53 |
# File 'lib/kdl/document.rb', line 51 def each(&block) nodes.each(&block) end |
#each_arg(key, &block) ⇒ Object
36 37 38 |
# File 'lib/kdl/document.rb', line 36 def each_arg(key, &block) args(key)&.each(&block) end |
#each_dash_val(key, &block) ⇒ Object
47 48 49 |
# File 'lib/kdl/document.rb', line 47 def each_dash_val(key, &block) dash_vals(key)&.each(&block) end |
#inspect ⇒ Object
59 60 61 |
# File 'lib/kdl/document.rb', line 59 def inspect nodes.map(&:inspect).join("\n") + "\n" end |
#to_s ⇒ Object
55 56 57 |
# File 'lib/kdl/document.rb', line 55 def to_s nodes.map(&:to_s).join("\n") + "\n" end |
#to_v1 ⇒ Object
77 78 79 |
# File 'lib/kdl/document.rb', line 77 def to_v1 KDL::V1::Document.new(nodes.map(&:to_v1)) end |
#to_v2 ⇒ Object
73 74 75 |
# File 'lib/kdl/document.rb', line 73 def to_v2 self end |
#version ⇒ Object
69 70 71 |
# File 'lib/kdl/document.rb', line 69 def version 2 end |