Module: Crystalline::MetadataFields::ClassMethods
- Extended by:
- T::Sig
- Defined in:
- lib/crystalline/metadata_fields.rb
Instance Method Summary collapse
- #field(field_name, type, metadata = {}) ⇒ Object
- #field_augmented? ⇒ Boolean
- #fields ⇒ Object
- #from_dict(d) ⇒ Object
- #from_json(json_obj) ⇒ Object
- #unmarshal_single(field_type, value, format_metadata = nil) ⇒ Object
Instance Method Details
#field(field_name, type, metadata = {}) ⇒ Object
30 31 32 33 34 |
# File 'lib/crystalline/metadata_fields.rb', line 30 def field(field_name, type, = {}) attr_accessor field_name fields << Field.new(field_name, type, ) end |
#field_augmented? ⇒ Boolean
36 37 38 |
# File 'lib/crystalline/metadata_fields.rb', line 36 def field_augmented? true end |
#fields ⇒ Object
24 25 26 27 28 |
# File 'lib/crystalline/metadata_fields.rb', line 24 def fields @__fields__ = [] if @__fields__.nil? @__fields__ end |
#from_dict(d) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/crystalline/metadata_fields.rb', line 79 def from_dict(d) to_build = {} fields.each do |field| key = field.name = field..fetch(:format_json, {}) lookup = .fetch(:letter_case, nil).call value = d[lookup] field_type = field.type if T.nilable? field_type if value == 'null' to_build[key] = nil next end field_type = T.nilable_of(field_type) end # If field is not nilable, and the value is not in the dict, raise a KeyError raise KeyError, "key #{lookup} not found in hash" if value.nil? && !T.nilable?(field.type) # If field is nilable, and the value is not in the dict, just move to the next field next if value.nil? if T.arr? field_type inner_type = T.arr_of(field_type) unmarshalled_array = value.map { |f| unmarshal_single(inner_type, f, ) } to_build[key] = unmarshalled_array elsif T.hash? field_type val_type = T.hash_of(field_type) # rubocop:disable Style/HashTransformValues unmarshalled_hash = value.map { |k, v| [k, unmarshal_single(val_type, v, )] }.to_h # rubocop:enable Style/HashTransformValues to_build[key] = unmarshalled_hash elsif T.union? field_type discriminator = field..fetch(:discriminator, nil) if !discriminator.nil? type_to_deserialize = value.fetch(discriminator) type_to_deserialize = T.get_union_types(field_type).find { |t| t.name.split('::').last == type_to_deserialize } unmarshalled_val = Crystalline.unmarshal_json(value, type_to_deserialize) to_build[key] = unmarshalled_val else union_types = T.get_union_types(field_type) union_types = union_types.sort_by { |klass| Crystalline.non_nilable_attr_count(klass) } union_types.each do |union_type| begin unmarshalled_val = Crystalline.unmarshal_json(value, union_type) to_build[key] = unmarshalled_val rescue TypeError next rescue NoMethodError next rescue KeyError next end break end end elsif field_type.instance_of?(Class) && field_type.include?(::Crystalline::MetadataFields) unmarshalled = Crystalline.unmarshal_json(value, field_type) to_build[key] = unmarshalled else final_value = unmarshal_single(field_type, value, ) to_build[key] = final_value end end new(**to_build) end |
#from_json(json_obj) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/crystalline/metadata_fields.rb', line 69 def from_json(json_obj) begin d = JSON.parse(json_obj) rescue TypeError, JSON::ParserError d = json_obj end from_dict(d) end |
#unmarshal_single(field_type, value, format_metadata = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/crystalline/metadata_fields.rb', line 40 def unmarshal_single(field_type, value, = nil) decoder = .fetch(:decoder, nil) if field_type.instance_of?(Class) && field_type.include?(::Crystalline::MetadataFields) return field_type.from_dict(value) elsif field_type.to_s == 'Date' return Date.parse(value) elsif field_type.to_s == 'DateTime' return DateTime.parse(value) elsif field_type.to_s == 'Object' # rubocop:disable Lint/SuppressedException begin value = JSON.parse(value) rescue TypeError, JSON::ParserError end # rubocop:enable Lint/SuppressedException return value elsif field_type.to_s == 'Float' return value.to_f end if decoder.nil? value else decoder.call(value) end end |