Module: Crystalline

Extended by:
T::Sig
Defined in:
lib/crystalline.rb,
lib/crystalline/utils.rb,
lib/crystalline/metadata_fields.rb

Overview

typed: true frozen_string_literal: true

Defined Under Namespace

Modules: MetadataFields Classes: FieldAugmented

Class Method Summary collapse

Class Method Details

.marshal_dict_complex(complex) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/crystalline/utils.rb', line 16

def self.marshal_dict_complex(complex)
  if complex.is_a? Array
    complex.map { |v| Crystalline.marshal_dict_complex(v) }
  elsif complex.is_a? Hash
    complex.transform_values { |v| Crystalline.marshal_dict_complex(v) }
  elsif complex.is_a? Crystalline::FieldAugmented
    complex.to_dict
  else
    complex
  end
end

.marshal_json_complex(complex) ⇒ Object



28
29
30
# File 'lib/crystalline/utils.rb', line 28

def self.marshal_json_complex(complex)
  JSON.dump(marshal_dict_complex(complex))
end

.unmarshal_complex(data, type) ⇒ Object



33
34
35
# File 'lib/crystalline/utils.rb', line 33

def self.unmarshal_complex(data, type)
  unmarshal_json(data, type)
end

.unmarshal_json(data, type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/crystalline/utils.rb', line 38

def self.unmarshal_json(data, type)
  if T.simplifiable? type
    type = T.simplify_type type
  end
  if type.instance_of?(Class) && type < ::Crystalline::FieldAugmented
    type.from_dict(data)
  elsif T.arr? type
    data.map { |v| Crystalline.unmarshal_complex(v, T.arr_of(type)) }
  elsif T.hash? type
    data.transform_values { |v| Crystalline.unmarshal_complex(v, T.hash_of(type)) }
  else
    data
  end
end

.val_to_string(val, primitives: true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/crystalline/utils.rb', line 54

def self.val_to_string(val, primitives: true)
  if val.is_a? T::Enum
    val.serialize
  elsif val.is_a? DateTime
    val.strftime('%Y-%m-%dT%H:%M:%S.%NZ')
  elsif primitives
    val.to_s
  else
    val
  end
end