Class: FunWithJsonApi::Attributes::Relationship

Inherits:
FunWithJsonApi::Attribute show all
Defined in:
lib/fun_with_json_api/attributes/relationship.rb

Instance Attribute Summary collapse

Attributes inherited from FunWithJsonApi::Attribute

#as, #name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FunWithJsonApi::Attribute

#sanitize_attribute_method

Constructor Details

#initialize(name, deserializer_class, options = {}) ⇒ Relationship

Returns a new instance of Relationship.



16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#deserializer_classObject (readonly)

Returns the value of attribute deserializer_class.



13
14
15
# File 'lib/fun_with_json_api/attributes/relationship.rb', line 13

def deserializer_class
  @deserializer_class
end

Class Method Details

.create(name, deserializer_class_or_callable, options = {}) ⇒ Object

Creates a new Relationship with name

Parameters:

  • name (String)

    name of the relationship

  • deserializer_class_or_callable (Class)

    Class of Deserializer or a callable that returns one

  • options (at) (defaults to: {})
    String

    alias value for the attribute



9
10
11
# 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

Instance Method Details

#call(id_value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fun_with_json_api/attributes/relationship.rb', line 25

def call(id_value)
  unless id_value.nil? || !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

#deserializerObject



50
51
52
# File 'lib/fun_with_json_api/attributes/relationship.rb', line 50

def deserializer
  @deserializer ||= build_deserializer_from_options
end

#has_many?Boolean

rubocop:disable Style/PredicateName

Returns:

  • (Boolean)


40
41
42
# File 'lib/fun_with_json_api/attributes/relationship.rb', line 40

def has_many?
  false
end

#param_valueObject

rubocop:enable Style/PredicateName



46
47
48
# File 'lib/fun_with_json_api/attributes/relationship.rb', line 46

def param_value
  :"#{as}_id"
end