class FunWithJsonApi::Attributes::Relationship
Attributes
deserializer_class[R]
Public Class Methods
create(name, deserializer_class_or_callable, options = {})
click to toggle source
Creates a new Relationship
with name @param name [String] name of the relationship @param deserializer_class_or_callable [Class] Class of Deserializer
or
a callable that returns one
@param options [String] alias value for the attribute
# File lib/fun_with_json_api/attributes/relationship.rb, line 9 def self.create(name, deserializer_class_or_callable, options = {}) new(name, deserializer_class_or_callable, options) end
new(name, deserializer_class, options = {})
click to toggle source
Calls superclass method
FunWithJsonApi::Attribute::new
# File lib/fun_with_json_api/attributes/relationship.rb, line 16 def initialize(name, deserializer_class, options = {}) options = options.reverse_merge( attributes: [], relationships: [] ) super(name, options) @deserializer_class = deserializer_class end
Public Instance Methods
decode(id_value)
click to toggle source
# File lib/fun_with_json_api/attributes/relationship.rb, line 25 def decode(id_value) return nil if id_value.nil? if id_value.is_a?(Array) raise build_invalid_relationship_error(id_value) end resource = deserializer.load_resource_from_id_value(id_value) raise build_missing_relationship_error(id_value) if resource.nil? check_resource_is_authorized!(resource, id_value) resource.id end
deserializer()
click to toggle source
# File lib/fun_with_json_api/attributes/relationship.rb, line 52 def deserializer @deserializer ||= build_deserializer_from_options end
has_many?()
click to toggle source
rubocop:disable Style/PredicateName
# File lib/fun_with_json_api/attributes/relationship.rb, line 42 def has_many? false end
param_value()
click to toggle source
rubocop:enable Style/PredicateName
# File lib/fun_with_json_api/attributes/relationship.rb, line 48 def param_value :"#{as}_id" end
Private Instance Methods
build_deserializer_from_options()
click to toggle source
# File lib/fun_with_json_api/attributes/relationship.rb, line 64 def build_deserializer_from_options if @deserializer_class.respond_to?(:call) @deserializer_class.call else @deserializer_class end.create(options) end
build_invalid_relationship_error(id_value)
click to toggle source
# File lib/fun_with_json_api/attributes/relationship.rb, line 72 def build_invalid_relationship_error(id_value) exception_message = "#{name} relationship should contain a single '#{deserializer.type}'"\ ' data hash' payload = ExceptionPayload.new payload.pointer = "/data/relationships/#{name}/data" payload.detail = exception_message Exceptions::InvalidRelationship.new(exception_message + ": #{id_value.inspect}", payload) end
build_missing_relationship_error(id_value, message = nil)
click to toggle source
# File lib/fun_with_json_api/attributes/relationship.rb, line 81 def build_missing_relationship_error(id_value, message = nil) message ||= missing_resource_debug_message(id_value) payload = ExceptionPayload.new payload.pointer = "/data/relationships/#{name}/data" payload.detail = "Unable to find '#{deserializer.type}' with matching id"\ ": #{id_value.inspect}" Exceptions::MissingRelationship.new(message, payload) end
missing_resource_debug_message(id_value)
click to toggle source
# File lib/fun_with_json_api/attributes/relationship.rb, line 90 def missing_resource_debug_message(id_value) "Couldn't find #{deserializer.resource_class.name}"\ " where #{deserializer.id_param} = #{id_value.inspect}" end