Class: Primalize::JSONAPI::HasOne

Inherits:
Object
  • Object
show all
Defined in:
lib/primalize/jsonapi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr, type: attr, &block) ⇒ HasOne

Returns a new instance of HasOne.



92
93
94
95
# File 'lib/primalize/jsonapi.rb', line 92

def initialize attr, type: attr, &block
  @attr = attr
  @block = block || proc { JSONAPI.fetch(type) }
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



91
92
93
# File 'lib/primalize/jsonapi.rb', line 91

def attr
  @attr
end

Instance Method Details

#call(model, cache:) ⇒ Object



97
98
99
100
101
102
# File 'lib/primalize/jsonapi.rb', line 97

def call(model, cache:)
  model = model.send(@attr)
  cache.fetch(:serialization, model) do
    primalizer.new(model).call
  end
end

#metadata(model, cache:) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/primalize/jsonapi.rb', line 108

def (model, cache:)
  model = model.send(@attr)
  data = cache.fetch(:metadata, model) do
    MetadataPrimalizer.new(model, primalizer.type).call
  end

  { data: data }
end

#primalizerObject



104
105
106
# File 'lib/primalize/jsonapi.rb', line 104

def primalizer
  @primalizer ||= @block.call
end