Module: RedisModelExtension::Database

Defined in:
lib/database.rb

Class Method Summary collapse

Class Method Details

.configObject


8
9
10
11
12
13
14
15
16
# File 'lib/database.rb', line 8

def self.config
  if File.exists?('config/redis_config.yml')
    YAML.load_file('config/redis_config.yml')[ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'].symbolize_keys
  else
    FileUtils.mkdir_p('config') unless File.exists?('config')
    FileUtils.cp(File.join(File.dirname(__FILE__),"../config/redis_config.yml.example"), 'config/redis_config.yml.example')
    raise ArgumentError, "Redis configuration file does not exists -> 'config/redis_config.yml', please provide it! I have created example file in config directory..."
  end
end

.redisObject


33
34
35
36
37
38
39
40
# File 'lib/database.rb', line 33

def self.redis
  #if redis is already defined
  return @redis if @redis
  #if you provided redis config
  return @redis = Redis.new(@redis_config) if @redis_config
  #if you provided yml config
  return @redis = Redis.new(Database.config)
end

.redis=(redis) ⇒ Object


23
24
25
26
27
28
29
30
31
# File 'lib/database.rb', line 23

def self.redis= redis
  if redis.is_a?(Redis) #valid redis instance
    @redis = redis  
  elsif redis.nil? #remove redis instance for changing connection or using in next call configs
    @redis = nil
  else #else you assigned something wrong
    raise ArgumentError, "You have to assign Redis instance!"
  end
end

.redis_config=(conf) ⇒ Object

Raises:

  • (ArgumentError)

18
19
20
21
# File 'lib/database.rb', line 18

def self.redis_config= conf
  raise ArgumentError, "Argument must be hash {:host => '..', :port => 6379, :db => 0 }" unless conf.has_key?(:host) && conf.has_key?(:port) && conf.has_key?(:db)
  @redis_config = conf
end