Class: DryOpenApi::Discriminator

Inherits:
Object
  • Object
show all
Includes:
EquatableAsContent
Defined in:
lib/dry_open_api/discriminator.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EquatableAsContent

#==

Constructor Details

#initialize(property_name: nil, mapping: {}, **other_fields_hash) ⇒ Discriminator

Returns a new instance of Discriminator.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dry_open_api/discriminator.rb', line 8

def initialize(property_name: nil, mapping: {}, **other_fields_hash)
  self.property_name = property_name
  self.mapping = mapping.with_indifferent_access
  self.other_fields_hash = other_fields_hash

  other_fields_hash.keys.each do |field_name|
    define_singleton_method(field_name) do
      other_fields_hash[field_name]
    end
    define_singleton_method("#{field_name}=") do |value|
      other_fields_hash[field_name] = value
    end
  end
end

Instance Attribute Details

#mappingObject

Returns the value of attribute mapping.



6
7
8
# File 'lib/dry_open_api/discriminator.rb', line 6

def mapping
  @mapping
end

#property_nameObject

Returns the value of attribute property_name.



6
7
8
# File 'lib/dry_open_api/discriminator.rb', line 6

def property_name
  @property_name
end

Class Method Details

.load(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dry_open_api/discriminator.rb', line 23

def self.load(hash)
  return unless hash

  fixed_field_names = %w[propertyName mapping]
  other_fields_hash = hash.reject { |key| key.to_s.in?(fixed_field_names) }

  new(
    property_name: hash['propertyName'].to_s,
    mapping: hash['mapping'],
    **other_fields_hash.symbolize_keys,
  )
end