Module: OpenApiSDK::MetadataFields::ClassMethods
- Extended by:
- T::Sig
- Defined in:
- lib/open_api_sdk/utils/metadata_fields.rb
Instance Method Summary collapse
- #field(field_name, type, metadata = {}) ⇒ Object
- #fields ⇒ Object
- #unmarshal_json(json_obj) ⇒ Object
- #unmarshal_single(field_type, value, decoder = nil) ⇒ Object
Instance Method Details
#field(field_name, type, metadata = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 29 def field(field_name, type, = {}) attr_accessor field_name fields << Field.new(field_name, type, ) end |
#fields ⇒ Object
23 24 25 26 27 |
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 23 def fields @__fields__ = [] if @__fields__.nil? @__fields__ end |
#unmarshal_json(json_obj) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 56 def unmarshal_json(json_obj) to_build = new begin d = JSON.parse(json_obj) rescue TypeError, JSON::ParserError d = json_obj end 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] 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) 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 |
#unmarshal_single(field_type, value, decoder = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 35 def unmarshal_single(field_type, value, decoder = nil) if field_type.respond_to? :unmarshal_json unmarshalled = field_type.unmarshal_json(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 |