Module: OpenApiSDK::MetadataFields

Extended by:
T::Sig
Included in:
Utils::FieldAugmented
Defined in:
lib/open_api_sdk/utils/metadata_fields.rb

Defined Under Namespace

Modules: ClassMethods Classes: Field

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



101
102
103
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 101

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#field(field_name) ⇒ Object



109
110
111
112
113
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 109

def field(field_name)
  fields.each do |f|
    return f if f.name == field_name
  end
end

#fieldsObject



105
106
107
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 105

def fields
  self.class.fields
end

#initializeObject



95
96
97
98
99
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 95

def initialize
  instance = allocate
  fields.each { |mod| instance.singleton_class.include(mod) }
  instance.send(:initialize)
end

#marshal_json(encode: true) ⇒ Object



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
148
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 123

def marshal_json(encode: true)
  d = {}
  fields.sort_by(&:name).each do |field|
    f = send(field.name)
    next if f.nil?

    format_json_meta = field.[:format_json]
    if !format_json_meta.nil? && format_json_meta.include?(:letter_case)
      key = format_json_meta[:letter_case].call(field.name)
    else
      key = field.name
    end
    if f.is_a? Array
      d[key] = f.map { |o| marshal_single(o) }
    elsif f.is_a? Hash
      d[key] = f.map { |k, v| [k, marshal_single(v)] }
    else
      d[key] = marshal_single(f)
    end
  end
  if encode
    JSON.dump(d)
  else
    d
  end
end

#marshal_single(field) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/open_api_sdk/utils/metadata_fields.rb', line 115

def marshal_single(field)
  if field.respond_to? :marshal_json
    field.marshal_json(encode: false)
  else
    Utils.val_to_string(field, primitives: false)
  end
end