Class: Lutaml::Model::Type::Hash

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/hash.rb

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

from_format, #initialize, #initialized?, register_format_to_from_methods, #to_s

Constructor Details

This class inherits a constructor from Lutaml::Model::Type::Value

Class Method Details

.cast(value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/lutaml/model/type/hash.rb', line 5

def self.cast(value)
  return value if value.nil? || Utils.uninitialized?(value)

  hash = if value.respond_to?(:to_h)
           value.to_h
         else
           Hash(value)
         end

  normalize_hash(hash)
end

.normalize_hash(hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lutaml/model/type/hash.rb', line 17

def self.normalize_hash(hash)
  return hash["text"] if hash.keys == ["text"]

  hash = hash.to_h if hash.is_a?(Lutaml::Model::MappingHash)

  normalized_hash = hash.transform_values do |value|
    normalize_value(value)
  end

  normalized_hash["elements"] || normalized_hash
end

.normalize_value(value) ⇒ Object



29
30
31
32
33
34
# File 'lib/lutaml/model/type/hash.rb', line 29

def self.normalize_value(value)
  return value unless value.is_a?(::Hash)

  nested = normalize_hash(value)
  nested.is_a?(::Hash) ? nested.except("text") : nested
end

.serialize(value) ⇒ Object



36
37
38
39
40
41
# File 'lib/lutaml/model/type/hash.rb', line 36

def self.serialize(value)
  return nil if value.nil?
  return value if value.is_a?(Hash)

  value.respond_to?(:to_h) ? value.to_h : Hash(value)
end

Instance Method Details

#to_json(*_args) ⇒ Object



48
49
50
# File 'lib/lutaml/model/type/hash.rb', line 48

def to_json(*_args)
  value
end

#to_tomlObject



56
57
58
# File 'lib/lutaml/model/type/hash.rb', line 56

def to_toml
  value
end

#to_xmlObject

Format-specific serialization methods



44
45
46
# File 'lib/lutaml/model/type/hash.rb', line 44

def to_xml
  value
end

#to_yamlObject



52
53
54
# File 'lib/lutaml/model/type/hash.rb', line 52

def to_yaml
  value
end