Class: Findyml::KeyNode

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

Direct Known Subclasses

IndexNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ KeyNode

Returns a new instance of KeyNode.



29
30
31
# File 'lib/findyml.rb', line 29

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



27
28
29
# File 'lib/findyml.rb', line 27

def node
  @node
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



33
34
35
36
37
# File 'lib/findyml.rb', line 33

def ==(other)
  other.is_a?(KeyNode) &&
    node.class == other.node.class &&
    to_s == other.to_s
end

#hashObject



40
41
42
# File 'lib/findyml.rb', line 40

def hash
  [node.class, to_s].hash
end

#inspectObject



59
60
61
# File 'lib/findyml.rb', line 59

def inspect
  "#<Findyml::KeyNode @node=#{self}>"
end

#lineObject



44
45
46
# File 'lib/findyml.rb', line 44

def line
  node.start_line + 1
end

#to_sObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/findyml.rb', line 48

def to_s
  @to_s ||= case node
  when Psych::Nodes::Scalar
    node.value
  when Psych::Nodes::Mapping
    "{#{node.children.each_slice(2).map { |k, v| "#{KeyNode.new(k).to_s}:#{KeyNode.new(v).to_s}" }.join(',')}}"
  when Psych::Nodes::Sequence
    "[#{node.children.map { KeyNode.new(_1).to_s }.join(',')}]"
  end
end