class Redis::Store::Factory

Constants

DEFAULT_PORT

Public Class Methods

create(*options) click to toggle source
# File lib/redis/store/factory.rb, line 9
def self.create(*options)
  new(options).create
end
extract_host_options_from_hash(options) click to toggle source
# File lib/redis/store/factory.rb, line 39
def self.extract_host_options_from_hash(options)
  options = normalize_key_names(options)
  if host_options?(options)
    options
  else
    nil 
  end
end
extract_host_options_from_uri(uri) click to toggle source
# File lib/redis/store/factory.rb, line 64
def self.extract_host_options_from_uri(uri)
  uri = URI.parse(uri)
  _, db, namespace = if uri.path
                       uri.path.split(/\//)
                     end

  options = {
    :host     => uri.hostname,
    :port     => uri.port || DEFAULT_PORT, 
    :password => uri.password
  }

  options[:db]        = db.to_i   if db
  options[:namespace] = namespace if namespace

  options
end
host_options?(options) click to toggle source
# File lib/redis/store/factory.rb, line 56
def self.host_options?(options)
  if options.keys.any? {|n| [:host, :db, :port].include?(n) }
    options
  else
    nil # just to be clear
  end
end
new(*options) click to toggle source
# File lib/redis/store/factory.rb, line 13
def initialize(*options)
  @addresses = []
  @options   = {}
  extract_addresses_and_options(options)
end
normalize_key_names(options) click to toggle source
# File lib/redis/store/factory.rb, line 48
def self.normalize_key_names(options)
  options = options.dup
  if options.key?(:key_prefix) && !options.key?(:namespace)
    options[:namespace] = options.delete(:key_prefix) # RailsSessionStore
  end
  options
end
resolve(uri) click to toggle source
# File lib/redis/store/factory.rb, line 31
def self.resolve(uri) #:api: private
  if uri.is_a?(Hash)
    extract_host_options_from_hash(uri)
  else
    extract_host_options_from_uri(uri)
  end
end

Public Instance Methods

create() click to toggle source
# File lib/redis/store/factory.rb, line 19
def create
  if @addresses.empty?
    @addresses << {}
  end
  
  if @addresses.size > 1
    ::Redis::DistributedStore.new @addresses, @options
  else
    ::Redis::Store.new @addresses.first.merge(@options)
  end
end

Private Instance Methods

extract_addresses_and_options(*options) click to toggle source
# File lib/redis/store/factory.rb, line 84
def extract_addresses_and_options(*options)
  options.flatten.compact.each do |token| 
    resolved = self.class.resolve(token)
    if resolved
      @addresses << resolved
    else
      @options.merge!(self.class.normalize_key_names(token))
    end
  end
end