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

#[], #attribute?, #attributes

Methods inherited from Value

#null?

Constructor Details

#initialize(attributes, schema, definitions) ⇒ Object

Returns a new instance of Object.



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

def initialize(attributes, schema, definitions)
  # Select inherriting schema on polymorphism
  if (discriminator = schema.discriminator)
    schema = discriminator.resolve(
      attributes[discriminator.property_name],
      definitions
    )
  end
  # Wrap attribute values
  @raw_attributes = schema.resolve_properties(:write, definitions)
                          .transform_values do |property|
    JSON.wrap(attributes[property.name], property.schema, definitions)
  end

  super(schema)
end

Instance Attribute Details

#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:



29
30
31
# File 'lib/jsapi/json/object.rb', line 29

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

#inspectObject

:nodoc:



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

def inspect # :nodoc:
  "#<#{self.class.name} " \
  "#{@raw_attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
end

#modelObject Also known as: value

Returns a model to read attributes by.



39
40
41
# File 'lib/jsapi/json/object.rb', line 39

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

#validate(errors) ⇒ Object

See Value#validate



46
47
48
49
50
# File 'lib/jsapi/json/object.rb', line 46

def validate(errors)
  return false unless super

  validate_attributes(errors)
end