Class: RamlParser::YamlNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, key, value) ⇒ YamlNode

Returns a new instance of YamlNode.



5
6
7
8
9
10
# File 'lib/raml_parser/yaml_helper.rb', line 5

def initialize(parent, key, value)
  @parent = parent
  @key = key
  @value = value
  @marks = {}
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/raml_parser/yaml_helper.rb', line 3

def key
  @key
end

#marksObject (readonly)

Returns the value of attribute marks.



3
4
5
# File 'lib/raml_parser/yaml_helper.rb', line 3

def marks
  @marks
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/raml_parser/yaml_helper.rb', line 3

def parent
  @parent
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/raml_parser/yaml_helper.rb', line 3

def value
  @value
end

Instance Method Details

#array(index) ⇒ Object



51
52
53
54
55
# File 'lib/raml_parser/yaml_helper.rb', line 51

def array(index)
  new_node = YamlNode.new(self, "[#{index}]", @value[index])
  new_node.mark(:used)
  new_node
end

#array_values(&code) ⇒ Object



57
58
59
# File 'lib/raml_parser/yaml_helper.rb', line 57

def array_values(&code)
  (@value || []).each_with_index.map { |_,i| code.call(array(i)) }
end

#arrayhash(index) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/raml_parser/yaml_helper.rb', line 71

def arrayhash(index)
  new_node = array(index)
  new_node.mark(:used)
  new_node2 = new_node.hash(new_node.value.first[0])
  new_node2.mark(:used)
  new_node2
end

#arrayhash_values(&code) ⇒ Object



79
80
81
82
83
84
# File 'lib/raml_parser/yaml_helper.rb', line 79

def arrayhash_values(&code)
  Hash[(@value || []).each_with_index.map { |_,i|
    node = arrayhash(i)
    [node.key, code.call(node)]
  }]
end

#hash(key) ⇒ Object



61
62
63
64
65
# File 'lib/raml_parser/yaml_helper.rb', line 61

def hash(key)
  new_node = YamlNode.new(self, key, @value[key])
  new_node.mark(:used)
  new_node
end

#hash_values(&code) ⇒ Object



67
68
69
# File 'lib/raml_parser/yaml_helper.rb', line 67

def hash_values(&code)
  Hash[(@value || {}).map { |k,v| [k, code.call(hash(k))] }]
end

#mark(what, p = path) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/raml_parser/yaml_helper.rb', line 28

def mark(what, p = path)
  if parent.nil?
    @marks[p] = what
  else
    @parent.mark(what, p)
  end
  self
end

#mark_all(what) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/raml_parser/yaml_helper.rb', line 37

def mark_all(what)
  mark(what)
  if @value.is_a? Hash
    hash_values { |n| n.mark_all(what) }
  elsif @value.is_a? Array
    array_values { |n| n.mark_all(what) }
  end
  self
end

#or_default(default) ⇒ Object



47
48
49
# File 'lib/raml_parser/yaml_helper.rb', line 47

def or_default(default)
  @value != nil ? self : YamlNode.new(@parent, @key, default)
end

#pathObject



20
21
22
23
24
25
26
# File 'lib/raml_parser/yaml_helper.rb', line 20

def path
  if @parent != nil
    "#{@parent.path}.#{@key}"
  else
    @key
  end
end

#rootObject



12
13
14
15
16
17
18
# File 'lib/raml_parser/yaml_helper.rb', line 12

def root
  if @parent != nil
    @parent.root
  else
    self
  end
end