Sha256: b2cbe27d27eb662c637facc66fa44ebd504fc178d6a4e1370926e2135b572d63

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# -*- encoding : utf-8 -*-
#Wrapper for redis connection
#============================
#Creates only one connection to redis per application in first time it needs to work with redis
module RedisModelExtension
  module Database

    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
    
    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

    def self.redis
      @redis ||= Redis.new(Database.config)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis-model-extension-0.3.5 lib/database.rb
redis-model-extension-0.3.4 lib/database.rb