Created
July 18, 2011 06:25
-
-
Save guilleiguaran/1088671 to your computer and use it in GitHub Desktop.
Save configuration parameters in Redis database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# | |
# - With set/get: | |
# App.config.set("some_config_value", "123456") | |
# value = App.config.get("some_config_value") | |
# | |
# - With hash: | |
# App.config["some_config_value"] = "123456" | |
# value = App.config["some_config_value"] | |
# | |
# - With methods: | |
# App.config.some_config_value = "123456" | |
# value = App.config.some_config_value | |
require 'singleton' | |
$redis = Redis.new | |
module App | |
class Config | |
include Singleton | |
def get(key) | |
$redis.get("config:#{key}") | |
end | |
def set(key, value) | |
$redis.set("config:#{key}", value) | |
end | |
alias_method :[], :get | |
alias_method :[]=, :set | |
def method_missing(method, *args) | |
if method.to_s =~ /(.+)=$/ | |
self.class.send(:define_method, method) do |value| | |
set($1, value) | |
end | |
send(method, args[0]) | |
else | |
self.class.send(:define_method, method) do | |
get(method.to_s) | |
end | |
send(method) | |
end | |
end | |
end | |
def self.config | |
Config.instance | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment