class JsonAPIObjectMapper::Deserialize::Resource
Attributes
attr_blocks[RW]
id_block[RW]
rel_has_many_blocks[RW]
rel_has_one_blocks[RW]
rel_options[RW]
type_block[RW]
links[R]
Public Class Methods
call(document)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 33 def self.call(document) load(document) end
inherited(klass)
click to toggle source
Calls superclass method
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 23 def self.inherited(klass) super klass.instance_variable_set("@attr_blocks", attr_blocks.dup) klass.instance_variable_set("@rel_has_one_blocks", rel_has_one_blocks.dup) klass.instance_variable_set("@rel_has_many_blocks", rel_has_many_blocks.dup) klass.instance_variable_set("@rel_options", rel_options.dup) klass.instance_variable_set("@id_block", id_block) klass.instance_variable_set("@type_block", type_block) end
load(document)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 37 def self.load(document) parser = JsonAPIObjectMapper::Parser::Document.new(document) if parser.contains_data_array? || parser.invalid? Collection.new(parser, klass: self) else new(parser) end end
new(parser, document: nil)
click to toggle source
Calls superclass method
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 46 def initialize(parser, document: nil) super() raise InvalidParser unless parser.is_a?(JsonAPIObjectMapper::Parser::Document) @errors = parser.errors if document_valid? @includes = parser.includes @links = parser_links(parser) @data = document_data(parser, document) @id = @data["id"] @type = @data["type"] @attributes = @data.fetch("attributes", {}) @relationships = @data.fetch("relationships", {}) deserialize! end freeze end
Public Instance Methods
each() { |self| ... }
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 65 def each yield self end
Private Instance Methods
deserialize!()
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 79 def deserialize! deserialize_id_type! deserialize_attributes! deserialize_relationships! end
deserialize_attributes!()
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 94 def deserialize_attributes! return if @attributes.empty? @attributes.each_pair(&method(:new_attribute)) end
deserialize_id_type!()
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 85 def deserialize_id_type! # Initialize ID and Type attribute blocks if one does not exist self.class.id unless self.class.id_block self.class.type unless self.class.type_block assign_attribute("id", self.class.id_block.call(@id)) assign_attribute("type", self.class.type_block.call(@type)) end
deserialize_relationships!()
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 99 def deserialize_relationships! return if @relationships.empty? @relationships.each_pair(&method(:new_relationship)) end
document_data(parser, document)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 71 def document_data(parser, document) document.nil? ? parser.document_data : (document["data"] || document) end
new_attribute(attr_name, attr_value)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 104 def new_attribute(attr_name, attr_value) return unless attribute_defined?(attr_name) assign_attribute(attr_name, attr_value) end
new_relationship(rel_type, rel_value)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 109 def new_relationship(rel_type, rel_value) if has_one_defined?(rel_type) assign_has_one_relationship(rel_type, rel_value["data"]) elsif has_many_defined?(rel_type) assign_has_many_relationship(rel_type, rel_value["data"]) end end
parser_links(parser)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/resource.rb, line 75 def parser_links(parser) parser.links unless parser.contains_data_array? end