Class: Jsapi::JSON::Object
- Includes:
- Model::Nestable
- Defined in:
- lib/jsapi/json/object.rb
Overview
Represents a JSON object.
Instance Attribute Summary collapse
-
#raw_attributes ⇒ Object
readonly
Returns the value of attribute raw_attributes.
Attributes inherited from Value
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Returns
trueif all attributes are empty,falseotherwise. -
#initialize(attributes, schema, definitions) ⇒ Object
constructor
A new instance of Object.
-
#inspect ⇒ Object
:nodoc:.
-
#model ⇒ Object
(also: #value)
Returns a model to read attributes by.
-
#validate(errors) ⇒ Object
See Value#validate.
Methods included from Model::Nestable
Methods inherited from Value
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_attributes ⇒ Object (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.
29 30 31 |
# File 'lib/jsapi/json/object.rb', line 29 def empty? @raw_attributes.values.all?(&:empty?) end |
#inspect ⇒ Object
: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 |
#model ⇒ Object 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 |