module JsonAPIObjectMapper::Deserialize::DSL::ClassMethods
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 66 def initialize(*args) @_class_attributes = {} @_class_relationships = {} super end
Public Instance Methods
inspect()
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 80 def inspect "#<#{self.class}:0x#{object_id.to_s(16)}, "\ "@errors = #{@errors.inspect}, "\ "attributes = #{@_class_attributes.inspect}, "\ "relationships = #{@_class_relationships.inspect}>" end
Also aliased as: to_s
to_hash()
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 72 def to_hash hashed_relationships = @_class_relationships.map do |key, value| { key => value.respond_to?(:to_hash) ? value.to_hash : value } end [@_class_attributes, *hashed_relationships].reduce({}, :merge) end
Also aliased as: to_h
Protected Instance Methods
assign_attribute(key, value)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 98 def assign_attribute(key, value) block = self.class.attr_blocks.fetch(key.to_s, DEFAULT_BLOCK) @_class_attributes[key.to_s] = block.call(value) end
assign_has_many_relationship(key, values)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 111 def assign_has_many_relationship(key, values) key = key.to_s block = self.class.rel_has_many_blocks.fetch(key, HAS_MANY_BLOCK) rel_embed_class = self.class.rel_options.dig(key, :embed_with) rel_values = values.map { |value| @includes.fetch(value) } @_class_relationships[key] = block.call(embed!(rel_embed_class, rel_values)) end
assign_has_one_relationship(key, value)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 103 def assign_has_one_relationship(key, value) key = key.to_s block = self.class.rel_has_one_blocks.fetch(key, DEFAULT_BLOCK) rel_embed_class = self.class.rel_options.dig(key, :embed_with) rel_value = embed!(rel_embed_class, @includes.fetch(value)) @_class_relationships[key] = block.call(rel_value) end
attribute_defined?(attribute_name)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 119 def attribute_defined?(attribute_name) self.class.attr_blocks.key?(attribute_name) end
embed!(rel_embed_class, attributes)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 135 def embed!(rel_embed_class, attributes) return attributes unless self.class.kind_of_resource?(rel_embed_class) rel_embed_class.load("data" => attributes) end
fetch_attribute(key)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 90 def fetch_attribute(key) @_class_attributes[key.to_s] end
fetch_relationship(key)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 94 def fetch_relationship(key) @_class_relationships[key.to_s] end
has_many_defined?(rel_name)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 127 def has_many_defined?(rel_name) self.class.rel_has_many_blocks.key?(rel_name) end
has_one_defined?(rel_name)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 123 def has_one_defined?(rel_name) self.class.rel_has_one_blocks.key?(rel_name) end
kind_of_resource?(rel_embed_class)
click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 131 def kind_of_resource?(rel_embed_class) self.class.kind_of_resource?(rel_embed_class) end