Class: Primalize::JSONAPI::Cache

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

Instance Method Summary collapse

Constructor Details

#initializeCache



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

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

Instance Method Details

#[](type, model) ⇒ Object



111
112
113
# File 'lib/primalize/jsonapi.rb', line 111

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

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



115
116
117
# File 'lib/primalize/jsonapi.rb', line 115

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

#fetch(type, model) ⇒ Object



119
120
121
# File 'lib/primalize/jsonapi.rb', line 119

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