Class: Jsapi::Meta::Schema::Object::Wrapper

Inherits:
Wrapper show all
Defined in:
lib/jsapi/meta/schema/object.rb

Instance Attribute Summary

Attributes inherited from Wrapper

#existence

Attributes inherited from Model::Wrapper

#definitions

Instance Method Summary collapse

Methods inherited from Wrapper

#default_value, #initialize

Methods inherited from Model::Wrapper

#==, #initialize, #inspect

Constructor Details

This class inherits a constructor from Jsapi::Meta::Schema::Wrapper

Instance Method Details

#additional_propertiesObject



8
9
10
# File 'lib/jsapi/meta/schema/object.rb', line 8

def additional_properties
  AdditionalProperties.wrap(super, definitions)
end

#resolve_properties(context: nil) ⇒ Object



12
13
14
15
16
# File 'lib/jsapi/meta/schema/object.rb', line 12

def resolve_properties(context: nil)
  super(definitions, context: context).transform_values do |property|
    Property.wrap(property, definitions)
  end
end

#resolve_schema(object, context: nil) ⇒ Object

Resolves the schema within context.

Raises a RuntimeError when the schema couldn’t be resolved.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jsapi/meta/schema/object.rb', line 21

def resolve_schema(object, context: nil)
  return self if discriminator.nil?

  properties = resolve_properties(context: context)

  discriminating_property = properties[discriminator.property_name]
  if discriminating_property.nil?
    raise Messages.invalid_value(
      name: 'discriminator property',
      value: discriminator.property_name,
      valid_values: properties.keys
    )
  end

  discriminating_value = discriminating_property.reader.call(object)
  if discriminating_value.nil?
    discriminating_value = discriminating_property.default_value(context: context)

    if discriminating_value.nil? && discriminator.default_mapping.nil?
      raise "discriminating value can't be nil"
    end
  end

  schema_name = discriminator.mapping(discriminating_value) || discriminating_value

  schema = definitions.find_schema(schema_name)
  if schema.nil?
    default_mapping = discriminator.default_mapping
    schema = definitions.find_schema(default_mapping) unless default_mapping.nil?

    if schema.nil?
      raise "inheriting schema couldn't be found: " \
            "#{[schema_name, default_mapping].compact.map(&:inspect).join(' or ')}"
    end
  end

  Wrapper
    .new(schema.resolve(definitions), definitions)
    .resolve_schema(object, context: context)
end