Class: Core::Io::Json::Grammar::Leaf
- Inherits:
-
Object
- Object
- Core::Io::Json::Grammar::Leaf
- Defined in:
- app/api/core/io/json/grammar.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #call(object, options, stream) ⇒ Object
- #dup ⇒ Object
-
#initialize(name, attribute) ⇒ Leaf
constructor
A new instance of Leaf.
- #inspect ⇒ Object
- #merge(_node) ⇒ Object
- #merge_children_with(node) ⇒ Object
Constructor Details
#initialize(name, attribute) ⇒ Leaf
Returns a new instance of Leaf.
146 147 148 149 150 |
# File 'app/api/core/io/json/grammar.rb', line 146 def initialize(name, attribute) @name = name @attribute = attribute.pop @attribute_path = attribute end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
144 145 146 |
# File 'app/api/core/io/json/grammar.rb', line 144 def name @name end |
Instance Method Details
#call(object, options, stream) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'app/api/core/io/json/grammar.rb', line 152 def call(object, , stream) value = @attribute_path.inject(object) do |o, k| return if o.nil? o.send(k) end or return stream.attribute(@name, value.send(@attribute), ) end |
#dup ⇒ Object
163 164 165 166 |
# File 'app/api/core/io/json/grammar.rb', line 163 def dup attribute = @attribute_path.dup.tap { |p| p << @attribute } self.class.new(name, attribute) end |
#inspect ⇒ Object
179 180 181 |
# File 'app/api/core/io/json/grammar.rb', line 179 def inspect "Leaf<#{@name},#{@attribute},#{@attribute_path.inspect}>" end |
#merge(_node) ⇒ Object
168 169 170 |
# File 'app/api/core/io/json/grammar.rb', line 168 def merge(_node) raise 'Cannot merge into a leaf as it is attribute only!' end |
#merge_children_with(node) ⇒ Object
172 173 174 175 176 177 |
# File 'app/api/core/io/json/grammar.rb', line 172 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 |