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.



130
131
132
133
134
135
136
137
# File 'lib/primalize/jsonapi.rb', line 130

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



139
140
141
142
143
# File 'lib/primalize/jsonapi.rb', line 139

def [] type, model
  return if model.nil?

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

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



145
146
147
148
149
# File 'lib/primalize/jsonapi.rb', line 145

def []= type, model, value
  return if model.nil?

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

#fetch(type, model) ⇒ Object



151
152
153
154
155
# File 'lib/primalize/jsonapi.rb', line 151

def fetch type, model
  return if model.nil?

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