Class: AnyStyle::Dictionary::Redis

Inherits:
AnyStyle::Dictionary show all
Defined in:
lib/anystyle/dictionary/redis.rb

Instance Attribute Summary

Attributes inherited from AnyStyle::Dictionary

#db, #options

Instance Method Summary collapse

Methods inherited from AnyStyle::Dictionary

create, instance, #populate!, #tag_counts, #tags, #truncate

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis



12
13
14
# File 'lib/anystyle/dictionary/redis.rb', line 12

def initialize(options = {})
  super(self.class.defaults.merge(options))
end

Instance Method Details

#closeObject



30
31
32
# File 'lib/anystyle/dictionary/redis.rb', line 30

def close
  db.close
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/anystyle/dictionary/redis.rb', line 38

def empty?
  open? and db.dbsize == 0
end

#get(key) ⇒ Object



42
43
44
# File 'lib/anystyle/dictionary/redis.rb', line 42

def get(key)
  db[key.to_s].to_i
end

#namespaceObject



50
51
52
# File 'lib/anystyle/dictionary/redis.rb', line 50

def namespace
  options[:namespace]
end

#openObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/anystyle/dictionary/redis.rb', line 16

def open
  unless open?
    @db = ::Redis.new(options)

    unless namespace.nil? or not defined?(::Redis::Namespace)
      @db = ::Redis::Namespace.new namespace, redis: @db
    end
  end

  self
ensure
  populate! if empty?
end

#open?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/anystyle/dictionary/redis.rb', line 34

def open?
  not db.nil?
end

#put(key, value) ⇒ Object



46
47
48
# File 'lib/anystyle/dictionary/redis.rb', line 46

def put(key, value)
  db[key.to_s] = value.to_i
end