Class: JSONAPI::Materializer::Resource::Relation

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/jsonapi/materializer/resource/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject

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

#fromObject

Returns the value of attribute from.



12
13
14
# File 'lib/jsonapi/materializer/resource/relation.rb', line 12

def from
  @from
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/jsonapi/materializer/resource/relation.rb', line 10

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



9
10
11
# File 'lib/jsonapi/materializer/resource/relation.rb', line 9

def owner
  @owner
end

#typeObject

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 |related_object|
                                  materializer_class.new(
                                    **subject.raw,
                                    object: related_object
                                  )
                                end
                              when :one
                                if fetch_relation(subject).present?
                                  materializer_class.new(
                                    **subject.raw,
                                    object: fetch_relation(subject)
                                  )
                                end
                              end
end

#many?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/jsonapi/materializer/resource/relation.rb', line 53

def many?
  type == :many
end

#one?Boolean

Returns:

  • (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