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

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

Constant Summary

Constants inherited from Value

Value::EMPTY_OPTIONS

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Methods inherited from Value

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

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

Constructor Details

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

Class Method Details

.cast(value) ⇒ Object



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

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

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

  normalize_hash(hash)
end

.default_xsd_typeString

XSD type for Hash

Returns:



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

def self.default_xsd_type
  "xs:anyType"
end

.normalize_hash(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lutaml/model/type/hash.rb', line 20

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



32
33
34
35
36
37
# File 'lib/lutaml/model/type/hash.rb', line 32

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



39
40
41
42
43
44
# File 'lib/lutaml/model/type/hash.rb', line 39

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