Class: JSONAPI::Materializer::Resource::Relation
- Inherits:
-
Object
- Object
- JSONAPI::Materializer::Resource::Relation
- Includes:
- ActiveModel::Model
- Defined in:
- lib/jsonapi/materializer/resource/relation.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#from ⇒ Object
Returns the value of attribute from.
-
#name ⇒ Object
Returns the value of attribute name.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #for(subject) ⇒ Object
-
#initialize(**keyword_arguments) ⇒ Relation
constructor
A new instance of Relation.
- #many? ⇒ Boolean
- #one? ⇒ Boolean
- #using(parent) ⇒ Object
Constructor Details
#initialize(**keyword_arguments) ⇒ Relation
Returns a new instance of Relation.
21 22 23 24 25 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 21 def initialize(**keyword_arguments) super(**keyword_arguments) validate! end |
Instance Attribute Details
#class_name ⇒ Object
Returns the value of attribute class_name.
13 14 15 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 13 def class_name @class_name end |
#from ⇒ Object
Returns the value of attribute from.
12 13 14 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 12 def from @from end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 10 def name @name end |
#owner ⇒ Object
Returns the value of attribute owner.
9 10 11 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 9 def owner @owner end |
#type ⇒ Object
Returns the value of attribute type.
11 12 13 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 11 def type @type end |
Instance Method Details
#for(subject) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 27 def for(subject) @for ||= {} @for[checksum(subject)] ||= case type when :many unlessing(fetch_relation(subject), -> { subject.includes.any? { |included| included.include?(from.to_s) } || fetch_relation(subject).loaded? }) do |subject| subject.select(:id) end.map do || materializer_class.new( **subject.raw, object: ) end when :one if fetch_relation(subject).present? materializer_class.new( **subject.raw, object: fetch_relation(subject) ) end end end |
#many? ⇒ Boolean
53 54 55 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 53 def many? type == :many end |
#one? ⇒ Boolean
57 58 59 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 57 def one? type == :one end |
#using(parent) ⇒ Object
49 50 51 |
# File 'lib/jsonapi/materializer/resource/relation.rb', line 49 def using(parent) Resource::Relationship.new(related: self, parent:) end |