Class: Core::Io::Json::Grammar::Leaf

Inherits:
Object
  • Object
show all
Defined in:
app/api/core/io/json/grammar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attribute) ⇒ Leaf

Returns a new instance of Leaf


139
140
141
142
143
# File 'app/api/core/io/json/grammar.rb', line 139

def initialize(name, attribute)
  @name           = name
  @attribute      = attribute.pop
  @attribute_path = attribute
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


137
138
139
# File 'app/api/core/io/json/grammar.rb', line 137

def name
  @name
end

Instance Method Details

#call(object, options, stream) ⇒ Object


145
146
147
148
149
# File 'app/api/core/io/json/grammar.rb', line 145

def call(object, options, stream)
  value = @attribute_path.inject(object) { |o, k| return if o.nil?; o.send(k) } or return

  stream.attribute(@name, value.send(@attribute), options)
end

#dupObject


151
152
153
154
# File 'app/api/core/io/json/grammar.rb', line 151

def dup
  attribute = @attribute_path.dup.tap { |p| p << @attribute }
  self.class.new(name, attribute)
end

#inspectObject


167
168
169
# File 'app/api/core/io/json/grammar.rb', line 167

def inspect
  "Leaf<#{@name},#{@attribute},#{@attribute_path.inspect}>"
end

#merge(_node) ⇒ Object


156
157
158
# File 'app/api/core/io/json/grammar.rb', line 156

def merge(_node)
  raise 'Cannot merge into a leaf as it is attribute only!'
end

#merge_children_with(node) ⇒ Object


160
161
162
163
164
165
# File 'app/api/core/io/json/grammar.rb', line 160

def merge_children_with(node)
  key = "_#{name}"
  raise "Cannot merge as existing leaf node '#{key}'" if node.children.key?(key)

  node.children.merge(key => self)
end