Class: Momm::RedisStore

Inherits:
Storage show all
Defined in:
lib/momm/redis_store.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ host: "localhost", port: 6379, namespace: "momm"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Storage

#update

Constructor Details

#initialize(options = {}) ⇒ RedisStore

Returns a new instance of RedisStore.



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

def initialize(options = {})
  @options = DEFAULT_OPTIONS.dup.merge options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/momm/redis_store.rb', line 5

def options
  @options
end

Instance Method Details

#clientObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/momm/redis_store.rb', line 11

def client
  @client ||= begin
    ns = options.delete(:namespace)

    require 'redis/namespace'
    native_client = Redis.new options

    Redis::Namespace.new(ns, :redis => native_client)
  end
end

#get_rate(currency, date = Date.today) ⇒ Object



27
28
29
30
# File 'lib/momm/redis_store.rb', line 27

def get_rate(currency, date = Date.today)
  date = Date.parse(date) if date.is_a? String
  client.get("#{date}#{currency}").to_f
end

#set_rate(currency, rate, date = Date.today) ⇒ Object



22
23
24
25
# File 'lib/momm/redis_store.rb', line 22

def set_rate(currency, rate, date = Date.today)
  date = Date.parse(date) if date.is_a? String
  client.set "#{date}#{currency}", rate
end