class Apnotic::InstanceCache

Public Class Methods

new(instance, method, ttl) click to toggle source
# File lib/apnotic/instance_cache.rb, line 3
def initialize(instance, method, ttl)
  @instance = instance
  @method   = method
  @ttl      = ttl
end

Public Instance Methods

call() click to toggle source
# File lib/apnotic/instance_cache.rb, line 9
def call
  if @cached_value && !expired?
    @cached_value
  else
    new_value
  end
end

Private Instance Methods

expired?() click to toggle source
# File lib/apnotic/instance_cache.rb, line 19
def expired?
  Time.now - @cached_at >= @ttl
end
new_value() click to toggle source
# File lib/apnotic/instance_cache.rb, line 23
def new_value
  @cached_at = Time.now
  @cached_value = @instance.send(@method)
end