Class: Redis::Store::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/store/factory.rb

Constant Summary collapse

DEFAULT_PORT =
6379

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Factory

Returns a new instance of Factory.



12
13
14
15
16
# File 'lib/redis/store/factory.rb', line 12

def initialize(*options)
  @addresses = []
  @options   = {}
  extract_addresses_and_options(options)
end

Class Method Details

.create(*options) ⇒ Object



8
9
10
# File 'lib/redis/store/factory.rb', line 8

def self.create(*options)
  new(options).create
end

.extract_host_options_from_hash(options) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/redis/store/factory.rb', line 38

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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/redis/store/factory.rb', line 67

def self.extract_host_options_from_uri(uri)
  uri = URI.parse(uri)
  if uri.scheme == "unix"
    options = { :path => uri.path }
  else
    _, db, namespace = if uri.path
      uri.path.split(/\//)
    end

    options = {
      :scheme   => uri.scheme,
      :host     => uri.hostname,
      :port     => uri.port || DEFAULT_PORT,
      :password => uri.password.nil? ? nil : CGI.unescape(uri.password.to_s)
    }

    options[:db]        = db.to_i   if db
    options[:namespace] = namespace if namespace
  end
  if uri.query
    query = Hash[URI.decode_www_form(uri.query)]
    query.each do |(key, value)|
      options[key.to_sym] = value
    end
  end

  options
end

.host_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/redis/store/factory.rb', line 63

def self.host_options?(options)
  options.keys.any? { |n| [:host, :db, :port, :path].include?(n) }
end

.normalize_key_names(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redis/store/factory.rb', line 47

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[:raw] = case
                  when options.key?(:serializer)
                    options[:serializer].nil?
                  when options.key?(:marshalling)
                    !options[:marshalling]
                  else
                    false
  end
  options
end

.resolve(uri) ⇒ Object

:api: private



30
31
32
33
34
35
36
# File 'lib/redis/store/factory.rb', line 30

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

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redis/store/factory.rb', line 18

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