Class: GrapeClient::Cache
- Inherits:
-
Object
- Object
- GrapeClient::Cache
- Defined in:
- lib/grape_client/cache.rb
Class Attribute Summary collapse
-
.instance ⇒ Object
readonly
Returns the value of attribute instance.
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(params) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #remove(object) ⇒ Object
- #store(object) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
3 4 5 |
# File 'lib/grape_client/cache.rb', line 3 def initialize clear end |
Class Attribute Details
.instance ⇒ Object (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
#clear ⇒ Object
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 |