Class: Cash::Mock::CacheEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/cash/mock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, raw, ttl) ⇒ CacheEntry

Returns a new instance of CacheEntry.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cash/mock.rb', line 16

def initialize(value, raw, ttl)
  if raw
    @value = value.to_s
  else
    @value = Marshal.dump(value)
  end
  
  if ttl.zero?
    @ttl = self.class.default_ttl
  else
    @ttl = ttl
  end
  
  @expires_at = self.class.now + @ttl
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/cash/mock.rb', line 6

def value
  @value
end

Class Method Details

.default_ttlObject



8
9
10
# File 'lib/cash/mock.rb', line 8

def self.default_ttl
  1_000_000
end

.nowObject



12
13
14
# File 'lib/cash/mock.rb', line 12

def self.now
  Time.now
end

Instance Method Details

#decrement(amount = 1) ⇒ Object



41
42
43
# File 'lib/cash/mock.rb', line 41

def decrement(amount = 1)
  @value = (@value.to_i - amount).to_s
end

#expired?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/cash/mock.rb', line 33

def expired?
  self.class.now > @expires_at
end

#increment(amount = 1) ⇒ Object



37
38
39
# File 'lib/cash/mock.rb', line 37

def increment(amount = 1)
  @value = (@value.to_i + amount).to_s
end

#to_iObject



49
50
51
# File 'lib/cash/mock.rb', line 49

def to_i
  @value.to_i
end

#unmarshalObject



45
46
47
# File 'lib/cash/mock.rb', line 45

def unmarshal
  Marshal.load(@value)
end