Module: Blockhole

Defined in:
lib/blockhole.rb,
lib/blockhole/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.storageObject

Returns the value of attribute storage.



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

def storage
  @storage
end

Class Method Details

.collapse(name) ⇒ Object



38
39
40
# File 'lib/blockhole.rb', line 38

def collapse(name)
  storage.del name
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Blockhole)

    the object that the method was called on



9
10
11
12
# File 'lib/blockhole.rb', line 9

def configure
  yield self
  self
end

.get(name) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/blockhole.rb', line 29

def get(name)
  value = storage.get(name)
  begin
    return MultiJson.load value
  rescue MultiJson::DecodeError
    return value
  end
end

.use_hole(hole_name, lifespan = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/blockhole.rb', line 14

def use_hole(hole_name, lifespan = nil, &block)
  if lifespan and lifespan < 1
    raise ArgumentError,
      "lifespan (if provided) must be >= 0, got #{lifespan}"
  end

  unless value = storage.get(hole_name)
    value = block.call(hole_name)
    storage.set(hole_name, encode(value))
    storage.expire(hole_name, lifespan) if lifespan
  end

  value
end