Class: FastJsonapi::Relationship
- Inherits:
-
Object
- Object
- FastJsonapi::Relationship
- Defined in:
- lib/fast_jsonapi/relationship.rb
Instance Attribute Summary collapse
-
#cached ⇒ Object
readonly
Returns the value of attribute cached.
-
#conditional_proc ⇒ Object
readonly
Returns the value of attribute conditional_proc.
-
#id_method_name ⇒ Object
readonly
Returns the value of attribute id_method_name.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#lazy_load_data ⇒ Object
readonly
Returns the value of attribute lazy_load_data.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#object_block ⇒ Object
readonly
Returns the value of attribute object_block.
-
#object_method_name ⇒ Object
readonly
Returns the value of attribute object_method_name.
-
#polymorphic ⇒ Object
readonly
Returns the value of attribute polymorphic.
-
#record_type ⇒ Object
readonly
Returns the value of attribute record_type.
-
#relationship_type ⇒ Object
readonly
Returns the value of attribute relationship_type.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#transform_method ⇒ Object
readonly
Returns the value of attribute transform_method.
Instance Method Summary collapse
- #fetch_associated_object(record, params) ⇒ Object
- #include_relationship?(record, serialization_params) ⇒ Boolean
-
#initialize(key:, name:, id_method_name:, record_type:, object_method_name:, object_block:, serializer:, relationship_type:, cached: false, polymorphic:, conditional_proc:, transform_method:, links:, lazy_load_data: false) ⇒ Relationship
constructor
A new instance of Relationship.
- #serialize(record, serialization_params, output_hash) ⇒ Object
Constructor Details
#initialize(key:, name:, id_method_name:, record_type:, object_method_name:, object_block:, serializer:, relationship_type:, cached: false, polymorphic:, conditional_proc:, transform_method:, links:, 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 |
# File 'lib/fast_jsonapi/relationship.rb', line 5 def initialize( key:, name:, id_method_name:, record_type:, object_method_name:, object_block:, serializer:, relationship_type:, cached: false, polymorphic:, conditional_proc:, transform_method:, links:, lazy_load_data: false ) @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 || {} @lazy_load_data = lazy_load_data end |
Instance Attribute Details
#cached ⇒ Object (readonly)
Returns the value of attribute cached.
3 4 5 |
# File 'lib/fast_jsonapi/relationship.rb', line 3 def cached @cached end |
#conditional_proc ⇒ Object (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_name ⇒ Object (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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/fast_jsonapi/relationship.rb', line 3 def key @key end |
#lazy_load_data ⇒ Object (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 |
#links ⇒ Object (readonly)
Returns the value of attribute links.
3 4 5 |
# File 'lib/fast_jsonapi/relationship.rb', line 3 def links @links end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/fast_jsonapi/relationship.rb', line 3 def name @name end |
#object_block ⇒ Object (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_name ⇒ Object (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 |
#polymorphic ⇒ Object (readonly)
Returns the value of attribute polymorphic.
3 4 5 |
# File 'lib/fast_jsonapi/relationship.rb', line 3 def polymorphic @polymorphic end |
#record_type ⇒ Object (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_type ⇒ Object (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 |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
3 4 5 |
# File 'lib/fast_jsonapi/relationship.rb', line 3 def serializer @serializer end |
#transform_method ⇒ Object (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
49 50 51 52 |
# File 'lib/fast_jsonapi/relationship.rb', line 49 def fetch_associated_object(record, params) return object_block.call(record, params) unless object_block.nil? record.send(object_method_name) end |
#include_relationship?(record, serialization_params) ⇒ Boolean
54 55 56 57 58 59 60 |
# File 'lib/fast_jsonapi/relationship.rb', line 54 def include_relationship?(record, serialization_params) if conditional_proc.present? conditional_proc.call(record, serialization_params) else true end end |
#serialize(record, serialization_params, output_hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fast_jsonapi/relationship.rb', line 37 def serialize(record, serialization_params, output_hash) if include_relationship?(record, serialization_params) empty_case = relationship_type == :has_many ? [] : nil output_hash[key] = {} unless lazy_load_data output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case end add_links_hash(record, serialization_params, output_hash) if links.present? end end |