Class: Primalize::JSONAPI::HasOne
- Inherits:
-
Object
- Object
- Primalize::JSONAPI::HasOne
- Defined in:
- lib/primalize/jsonapi.rb
Instance Attribute Summary collapse
-
#attr ⇒ Object
readonly
Returns the value of attribute attr.
Instance Method Summary collapse
- #call(model, cache:) ⇒ Object
-
#initialize(attr, &block) ⇒ HasOne
constructor
A new instance of HasOne.
- #metadata(model, cache:) ⇒ Object
- #primalizer ⇒ Object
Constructor Details
#initialize(attr, &block) ⇒ HasOne
Returns a new instance of HasOne.
67 68 69 70 |
# File 'lib/primalize/jsonapi.rb', line 67 def initialize attr, &block @attr = attr @block = block end |
Instance Attribute Details
#attr ⇒ Object (readonly)
Returns the value of attribute attr.
66 67 68 |
# File 'lib/primalize/jsonapi.rb', line 66 def attr @attr end |
Instance Method Details
#call(model, cache:) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/primalize/jsonapi.rb', line 72 def call(model, cache:) model = model.send(@attr) cache.fetch(:serialization, model) do primalizer.new(model).call end end |
#metadata(model, cache:) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/primalize/jsonapi.rb', line 83 def (model, cache:) model = model.send(@attr) cache.fetch(:metadata, model) do { data: MetadataPrimalizer.new(model).call } end end |
#primalizer ⇒ Object
79 80 81 |
# File 'lib/primalize/jsonapi.rb', line 79 def primalizer @primalizer ||= @block.call end |