Class: Redis::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/settings.rb,
lib/redis/settings/railtie.rb,
lib/redis/settings/version.rb,
lib/redis/settings/active_record.rb

Defined Under Namespace

Modules: ActiveRecord, Version Classes: NewRecordError, Railtie

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ Settings

Initialize a new object that will be wrapped into the specified namespace.

s = Redis::Settings.new("app")


41
42
43
# File 'lib/redis/settings.rb', line 41

def initialize(namespace)
  @namespace = namespace
end

Class Attribute Details

.connectionObject

Returns the value of attribute connection.



19
20
21
# File 'lib/redis/settings.rb', line 19

def connection
  @connection
end

.root_namespaceObject

Returns the value of attribute root_namespace.



20
21
22
# File 'lib/redis/settings.rb', line 20

def root_namespace
  @root_namespace
end

Class Method Details

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

Return Redis::Settings.

Redis::Settings.configure do |config|
  config.connection = Redis.new(:host => "localhost", :port => 6379)
end

Yields:

  • (_self)

Yield Parameters:



32
33
34
# File 'lib/redis/settings.rb', line 32

def self.configure(&block)
  yield self
end

Instance Method Details

#allObject

Return a hash with all settings



99
100
101
# File 'lib/redis/settings.rb', line 99

def all
  Hash[redis.hgetall(namespace).collect {|k, v| [k.to_sym, JSON.parse(v)["data"]]}]
end

#clearObject

Remove all settings from the current namespace



94
95
96
# File 'lib/redis/settings.rb', line 94

def clear
  redis.del(namespace)
end

#get(name, default = nil) ⇒ Object Also known as: [], fetch

Retrieve setting by its name. When nil, return the default value.

s = Redis::Settings.new("app")
s.get(:items_per_page)
s.get(:items_per_page, 10)


61
62
63
64
65
66
67
68
69
70
# File 'lib/redis/settings.rb', line 61

def get(name, default = nil)
  value = redis.hget(namespace, name)

  if value
    payload = JSON.parse(value)
    value = payload["data"]
  end

  value.nil? ? default : value
end

#namespaceObject

Return instance’s namespace concatenated with Redis::Settings.root_namespace.

s = Redis::Settings.new("app")
s.namespace
#=> "settings/app"


51
52
53
# File 'lib/redis/settings.rb', line 51

def namespace
  "#{self.class.root_namespace}/#{@namespace}"
end

#remove(name) ⇒ Object Also known as: delete

Delete the specified option. Just a shortcut for settings.set(:name, nil).



87
88
89
# File 'lib/redis/settings.rb', line 87

def remove(name)
  set(name, nil)
end

#set(name, value) ⇒ Object Also known as: []=

Define a value for the specified setting.

s = Redis::Settings.new("app")
s.set(:items_per_page, 10)


77
78
79
80
81
82
83
# File 'lib/redis/settings.rb', line 77

def set(name, value)
  if value.nil?
    redis.hdel(namespace, name)
  else
    redis.hset(namespace, name, {:data => value}.to_json)
  end
end