Class: Lutaml::KeyValue::Adapter::Toml::TomlibAdapter

Inherits:
Document show all
Defined in:
lib/lutaml/key_value/adapter/toml/tomlib_adapter.rb

Instance Attribute Summary

Attributes inherited from Document

#attributes, #register

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

#[], #[]=, #initialize, #key?, #to_h

Constructor Details

This class inherits a constructor from Lutaml::KeyValue::Document

Class Method Details

.parse(toml, _options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lutaml/key_value/adapter/toml/tomlib_adapter.rb', line 12

def self.parse(toml, _options = {})
  require "tomlib"
  Tomlib.load(toml)
rescue LoadError
  raise LoadError,
        "tomlib gem is not available. Please add 'tomlib' to your Gemfile or use toml-rb adapter."
rescue StandardError => e
  # Tomlib can throw various errors for invalid TOML (TypeError,
  # ArgumentError, etc.).
  # Re-raise as Tomlib::ParseError which will be caught by serialize.rb
  raise Tomlib::ParseError, e.message
end

Instance Method Details

#to_tomlObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lutaml/key_value/adapter/toml/tomlib_adapter.rb', line 25

def to_toml(*)
  require "tomlib"
  # Handle KeyValueElement input (new symmetric architecture)
  attributes_to_serialize = if @attributes.is_a?(Lutaml::KeyValue::DataModel::Element)
                              # Unwrap __root__ wrapper to get actual content
                              @attributes.to_hash["__root__"]
                            else
                              # Legacy Hash input (backward compatibility)
                              @attributes
                            end

  Tomlib.dump(attributes_to_serialize)
  # Tomlib::Generator.new(to_h).toml_str
rescue LoadError
  raise LoadError,
        "tomlib gem is not available. Please add 'tomlib' to your Gemfile or use toml-rb adapter."
end