Class: Primalize::JSONAPI::Cache

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

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



100
101
102
103
104
105
106
107
# File 'lib/primalize/jsonapi.rb', line 100

def initialize
  # Three-layer cache: metadata/serialization, class, and id
  @cache = Hash.new do |h, k|
    h[k] = Hash.new do |h, k|
      h[k] = {}
    end
  end
end

Instance Method Details

#[](type, model) ⇒ Object



109
110
111
# File 'lib/primalize/jsonapi.rb', line 109

def [] type, model
  @cache[type][model.class][model.id]
end

#[]=(type, model, value) ⇒ Object



113
114
115
# File 'lib/primalize/jsonapi.rb', line 113

def []= type, model, value
  @cache[type][model.class][model.id] = value
end

#fetch(type, model) ⇒ Object



117
118
119
# File 'lib/primalize/jsonapi.rb', line 117

def fetch type, model
  @cache[type][model.class][model.id] ||= yield
end