Class: Jsapi::JSON::Object

Inherits:
Value
  • Object
show all
Includes:
Model::Nestable
Defined in:
lib/jsapi/json/object.rb

Overview

Represents a JSON object.

Instance Attribute Summary collapse

Attributes inherited from Value

#schema

Instance Method Summary collapse

Methods included from Model::Nestable

#[], #additional_attributes, #attribute?, #attributes, #inspect

Methods inherited from Value

#inspect, #null?

Constructor Details

#initialize(hash, schema, definitions, context: nil) ⇒ Object

Returns a new instance of Object.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jsapi/json/object.rb', line 11

def initialize(hash, schema, definitions, context: nil)
  schema = schema.resolve_schema(hash, definitions, context: context)
  properties = schema.resolve_properties(definitions, context: context)

  @raw_attributes = properties.transform_values do |property|
    JSON.wrap(hash[property.name], property.schema, definitions, context: context)
  end

  @raw_additional_attributes =
    if (additional_properties = schema.additional_properties)
      additional_properties_schema = additional_properties.schema.resolve(definitions)

      hash.except(*properties.keys).transform_values do |value|
        JSON.wrap(value, additional_properties_schema, definitions, context: context)
      end
    end || {}

  super(schema)
end

Instance Attribute Details

#raw_additional_attributesObject (readonly)

Returns the value of attribute raw_additional_attributes.



9
10
11
# File 'lib/jsapi/json/object.rb', line 9

def raw_additional_attributes
  @raw_additional_attributes
end

#raw_attributesObject (readonly)

Returns the value of attribute raw_attributes.



9
10
11
# File 'lib/jsapi/json/object.rb', line 9

def raw_attributes
  @raw_attributes
end

Instance Method Details

#empty?Boolean

Returns true if all attributes are empty, false otherwise.

Returns:



32
33
34
# File 'lib/jsapi/json/object.rb', line 32

def empty?
  @raw_attributes.values.all?(&:empty?)
end

#modelObject Also known as: value

Returns a model to read attributes by.



37
38
39
# File 'lib/jsapi/json/object.rb', line 37

def model
  @model ||= (schema.model || Model::Base).new(self)
end

#validate(errors) ⇒ Object

See Value#validate



44
45
46
47
48
# File 'lib/jsapi/json/object.rb', line 44

def validate(errors)
  return false unless super

  validate_attributes(errors)
end