Class: JSONAPI::Ruby::Deserializer::Resource
- Inherits:
-
Object
- Object
- JSONAPI::Ruby::Deserializer::Resource
- Includes:
- Parser
- Defined in:
- lib/jsonapi-ruby-deserializer/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ Object
Returns the value of attribute id.
-
#links ⇒ Object
Returns the value of attribute links.
-
#relationships ⇒ Object
Returns the value of attribute relationships.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #fetch_relation(data, index) ⇒ Object
-
#initialize(data) ⇒ Resource
constructor
A new instance of Resource.
- #parse_links!(data) ⇒ Object
- #parse_relationships!(relationships) ⇒ Object
Methods included from Parser
Constructor Details
#initialize(data) ⇒ Resource
Returns a new instance of Resource.
12 13 14 15 16 17 18 19 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 12 def initialize(data) @id = data['id'] @type = data['type'] @attributes = data['attributes'] @links = parse_links!(data['links']) parse!(@attributes) if @attributes parse_relationships!(data['relationships']) if data['relationships'] end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
10 11 12 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 10 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 10 def id @id end |
#links ⇒ Object
Returns the value of attribute links.
10 11 12 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 10 def links @links end |
#relationships ⇒ Object
Returns the value of attribute relationships.
10 11 12 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 10 def relationships @relationships end |
#type ⇒ Object
Returns the value of attribute type.
10 11 12 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 10 def type @type end |
Instance Method Details
#fetch_relation(data, index) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 36 def fetch_relation(data, index) if data.kind_of?(Array) data.map { |element| index[[element.type, element.id]] } else index[[data.type, data.id]] end end |
#parse_links!(data) ⇒ Object
30 31 32 33 34 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 30 def parse_links!(data) return if data.nil? Links.new(data) end |
#parse_relationships!(relationships) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/jsonapi-ruby-deserializer/resource.rb', line 21 def parse_relationships!(relationships) @relationships = [] relationships.map do |key, h| @relationships << key self.class.send(:attr_accessor, key) instance_variable_set("@#{key}", Document.new(h)) end end |