Class: GrapeClient::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_client/cache.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



3
4
5
# File 'lib/grape_client/cache.rb', line 3

def initialize
  clear
end

Class Attribute Details

.instanceObject (readonly)

Returns the value of attribute instance.



37
38
39
# File 'lib/grape_client/cache.rb', line 37

def instance
  @instance
end

Instance Method Details

#clearObject



7
8
9
# File 'lib/grape_client/cache.rb', line 7

def clear
  @objects = {}
end

#fetch(params) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/grape_client/cache.rb', line 11

def fetch(params)
  id = params.is_a?(Hash) ? params[:id] : params
  object = @objects[id] if id.present?
  unless object.present?
    object = yield
    store(object)
  end
  object
end

#remove(object) ⇒ Object



27
28
29
30
31
# File 'lib/grape_client/cache.rb', line 27

def remove(object)
  id = object.try(:id)
  return unless id.present?
  @objects[id] = nil
end

#store(object) ⇒ Object



21
22
23
24
25
# File 'lib/grape_client/cache.rb', line 21

def store(object)
  id = object.try(:id)
  return unless id.present?
  @objects[id] = object
end