Class: FastJsonapi::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_jsonapi/relationship.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner:, key:, name:, id_method_name:, record_type:, object_method_name:, object_block:, serializer:, relationship_type:, cached: false, polymorphic:, conditional_proc:, transform_method:, links:, meta:, lazy_load_data: false) ⇒ Relationship

Returns a new instance of Relationship.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fast_jsonapi/relationship.rb', line 5

def initialize(
  owner:,
  key:,
  name:,
  id_method_name:,
  record_type:,
  object_method_name:,
  object_block:,
  serializer:,
  relationship_type:,
  cached: false,
  polymorphic:,
  conditional_proc:,
  transform_method:,
  links:,
  meta:,
  lazy_load_data: false
)
  @owner = owner
  @key = key
  @name = name
  @id_method_name = id_method_name
  @record_type = record_type
  @object_method_name = object_method_name
  @object_block = object_block
  @serializer = serializer
  @relationship_type = relationship_type
  @cached = cached
  @polymorphic = polymorphic
  @conditional_proc = conditional_proc
  @transform_method = transform_method
  @links = links || {}
  @meta = meta || {}
  @lazy_load_data = lazy_load_data
  @record_types_for = {}
  @serializers_for_name = {}
end

Instance Attribute Details

#cachedObject (readonly)

Returns the value of attribute cached.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def cached
  @cached
end

#conditional_procObject (readonly)

Returns the value of attribute conditional_proc.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def conditional_proc
  @conditional_proc
end

#id_method_nameObject (readonly)

Returns the value of attribute id_method_name.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def id_method_name
  @id_method_name
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def key
  @key
end

#lazy_load_dataObject (readonly)

Returns the value of attribute lazy_load_data.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def lazy_load_data
  @lazy_load_data
end

Returns the value of attribute links.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def links
  @links
end

#metaObject (readonly)

Returns the value of attribute meta.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def name
  @name
end

#object_blockObject (readonly)

Returns the value of attribute object_block.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def object_block
  @object_block
end

#object_method_nameObject (readonly)

Returns the value of attribute object_method_name.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def object_method_name
  @object_method_name
end

#ownerObject (readonly)

Returns the value of attribute owner.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def owner
  @owner
end

#polymorphicObject (readonly)

Returns the value of attribute polymorphic.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def polymorphic
  @polymorphic
end

#record_typeObject (readonly)

Returns the value of attribute record_type.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def record_type
  @record_type
end

#relationship_typeObject (readonly)

Returns the value of attribute relationship_type.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def relationship_type
  @relationship_type
end

#serializerObject (readonly)

Returns the value of attribute serializer.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def serializer
  @serializer
end

#transform_methodObject (readonly)

Returns the value of attribute transform_method.



3
4
5
# File 'lib/fast_jsonapi/relationship.rb', line 3

def transform_method
  @transform_method
end

Instance Method Details

#fetch_associated_object(record, params) ⇒ Object



55
56
57
58
59
# File 'lib/fast_jsonapi/relationship.rb', line 55

def fetch_associated_object(record, params)
  return FastJsonapi.call_proc(object_block, record, params) unless object_block.nil?

  record.send(object_method_name)
end

#include_relationship?(record, serialization_params) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/fast_jsonapi/relationship.rb', line 61

def include_relationship?(record, serialization_params)
  if conditional_proc.present?
    FastJsonapi.call_proc(conditional_proc, record, serialization_params)
  else
    true
  end
end

#serialize(record, included, serialization_params, output_hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fast_jsonapi/relationship.rb', line 43

def serialize(record, included, serialization_params, output_hash)
  if include_relationship?(record, serialization_params)
    empty_case = relationship_type == :has_many ? [] : nil

    output_hash[key] = {}
    output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case unless lazy_load_data && !included

    add_meta_hash(record, serialization_params, output_hash) if meta.present?
    add_links_hash(record, serialization_params, output_hash) if links.present?
  end
end

#serializer_for(record, serialization_params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fast_jsonapi/relationship.rb', line 69

def serializer_for(record, serialization_params)
  # TODO: Remove this, dead code...
  if @static_serializer
    @static_serializer

  elsif polymorphic
    name = polymorphic[record.class] if polymorphic.is_a?(Hash)
    name ||= record.class.name
    serializer_for_name(name)

  elsif serializer.is_a?(Proc)
    FastJsonapi.call_proc(serializer, record, serialization_params)

  elsif object_block
    serializer_for_name(record.class.name)

  else
    # TODO: Remove this, dead code...
    raise "Unknown serializer for object #{record.inspect}"
  end
end

#static_record_typeObject



96
97
98
99
# File 'lib/fast_jsonapi/relationship.rb', line 96

def static_record_type
  initialize_static_serializer unless @initialized_static_serializer
  @static_record_type
end

#static_serializerObject



91
92
93
94
# File 'lib/fast_jsonapi/relationship.rb', line 91

def static_serializer
  initialize_static_serializer unless @initialized_static_serializer
  @static_serializer
end