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, decoder = 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
71 72 73 74 75 76 77 78 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 |
# File 'lib/crystalline/metadata_fields.rb', line 71 def from_dict(d) to_build = new fields.each do |field| field_type = field.type if T.nilable? field_type field_type = T.nilable_of(field_type) end key = "#{field.name}=" lookup = field..fetch(:format_json, {}).fetch(:letter_case, nil).call value = d[lookup] # 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.send(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.send(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.send(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.send(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.send(key, unmarshalled) else decoder = field..fetch(:format_json, {}).fetch(:decoder, nil) final_value = unmarshal_single(field_type, value, decoder) to_build.send(key, final_value) end end to_build end |
#from_json(json_obj) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/crystalline/metadata_fields.rb', line 61 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, decoder = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/crystalline/metadata_fields.rb', line 40 def unmarshal_single(field_type, value, decoder = nil) if field_type.instance_of?(Class) && field_type.include?(::Crystalline::MetadataFields) unmarshalled = field_type.from_dict(value) return unmarshalled 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 end if decoder.nil? value else decoder.call(value) end end |