class Lutaml::KeyValue::DataModel::Element

# => {“items” => [“apple”, “banana”]}
element.to_hash
element.add_child(“banana”)
element.add_child(“apple”)
element = Element.new(“items”)
@example Array values
# => {“person” => {“name” => “John”, “age” => 30}}
parent.to_hash
parent.add_child(Element.new(“age”, 30))
parent.add_child(Element.new(“name”, “John”))
parent = Element.new(“person”)
@example Nested structure
# => {“name” => “John”}
element.to_hash
element = Element.new(“name”, “John”)
@example Simple key-value
- Enable format-specific optimizations
- Maintain clear separation of concerns
- Support transformation logic
- Track metadata about value types
Unlike Hash, Element is an explicit OOP model that can:
presentation (how to serialize).
data structures, separating content (what to serialize) from
This class provides an intermediate representation for key-value
Represents a key-value element for JSON, YAML, TOML formats.

def [](key)

Returns:
  • (Object) - Value for the key

Parameters:
  • key (String, Symbol) -- Key to access
def [](key)
  hash = to_hash
  # If this is the root element, access the inner hash
  if @key == "__root__" && hash.key?("__root__")
    inner = hash["__root__"]
    inner.is_a?(Hash) ? inner[key.to_s] : nil
  else
    hash[key.to_s]
  end
end

def []=(key, value)

Returns:
  • (Object) - The assigned value

Parameters:
  • value (Object) -- Value to set
  • key (String, Symbol) -- Key to set
def []=(key, value)
  # For root element, add child with the key-value pair
  if @key == "__root__"
    # Remove existing child with same key if any
    @children.reject! do |child|
      child.is_a?(Element) && child.key == key.to_s
    end
    # Add new child
  else
    # For non-root elements, convert to hash and merge
    # This shouldn't normally happen in the transformation flow
  end
  @children << Element.new(key.to_s, value)
  value
end

def add_child(child)

Returns:
  • (self) -

Parameters:
  • child (Element, Object) -- Child to add
def add_child(child)
  @children << child
  self
end

def all_key_value_elements?

Returns:
  • (Boolean) -
def all_key_value_elements?
  @children.all?(Element)
end

def all_primitives?

Returns:
  • (Boolean) -
def all_primitives?
  @children.none?(Element)
end

def children_to_array

Returns:
  • (Array) -
def children_to_array
  @children
end

def children_to_hash

Returns:
  • (Hash) -
def children_to_hash
  @children.map(&:to_hash).reduce({}, :merge)
end

def children_to_value

Returns:
  • (Hash, Array, Object) -
def children_to_value
  if all_key_value_elements?
    children_to_hash
  elsif all_primitives?
    children_to_array
  else
    # Mixed: some Elements, some primitives
    # Convert all to their values and return as array
    @children.map do |child|
      child.is_a?(Element) ? child.to_hash.values.first : child
    end
  end
end

def has_children?

Returns:
  • (Boolean) -
def has_children?
  !@children.empty?
end

def has_value?

Returns:
  • (Boolean) -
def has_value?
  !@value.nil?
end

def initialize(key, value = nil)

Parameters:
  • value (Object, nil) -- Optional direct value
  • key (String, Symbol) -- The key name
def initialize(key, value = nil)
  @key = key.to_s
  @value = value
  @children = []
end

def inspect

Returns:
  • (String) -
def inspect
  to_s
end

def leaf?

Returns:
  • (Boolean) -
def leaf?
  has_value? && !has_children?
end

def to_hash

Returns:
  • (Hash) -
def to_hash
  if has_children?
    { @key => children_to_value }
  elsif has_value?
    { @key => @value }
  else
    # Special case: __root__ element should return empty hash when all attributes omitted
    # This allows hash["__root__"].keys to work without raising NoMethodError
    { @key => (@key == "__root__" ? {} : nil) }
  end
end

def to_s

Returns:
  • (String) -
def to_s
  if leaf?
    "<Element key=#{@key.inspect} value=#{@value.inspect}>"
  elsif has_children?
    "<Element key=#{@key.inspect} children=#{@children.length}>"
  else
    "<Element key=#{@key.inspect}>"
  end
end