Class: Hpreserve::AbstractCacher

Inherits:
Object
  • Object
show all
Defined in:
lib/hpreserve/abstract_cacher.rb

Direct Known Subclasses

FileCacher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns) ⇒ AbstractCacher

Returns a new instance of AbstractCacher.



6
7
8
# File 'lib/hpreserve/abstract_cacher.rb', line 6

def initialize(patterns)
  self.patterns = patterns
end

Instance Attribute Details

#patternsObject

Returns the value of attribute patterns.



4
5
6
# File 'lib/hpreserve/abstract_cacher.rb', line 4

def patterns
  @patterns
end

#storageObject

Returns the value of attribute storage.



4
5
6
# File 'lib/hpreserve/abstract_cacher.rb', line 4

def storage
  @storage
end

Instance Method Details

#expire(pattern) ⇒ Object



29
30
31
32
# File 'lib/hpreserve/abstract_cacher.rb', line 29

def expire(pattern)
  @storage ||= {}
  storage.delete_if {|key, value| key.match(pattern) }
end

#match?(variable) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/hpreserve/abstract_cacher.rb', line 10

def match?(variable)
  pattern = patterns.detect {|p| variable.match(p[:match]) }
  return nil unless pattern
  variable.gsub(pattern[:match], pattern[:key])
end

#retrieve(key) ⇒ Object

overwrite these in your ConcreteCacher to do use whatever you use



19
20
21
22
# File 'lib/hpreserve/abstract_cacher.rb', line 19

def retrieve(key)
  @storage ||= {}
  storage[key]
end

#store(key, value) ⇒ Object



24
25
26
27
# File 'lib/hpreserve/abstract_cacher.rb', line 24

def store(key, value)
  @storage ||= {}
  storage[key] = value
end