Class: AdvancedConnection::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/advanced_connection/config.rb

Constant Summary collapse

VALID_QUEUE_TYPES =
[
  :fifo, :lifo, :stack, :prefer_younger, :prefer_older
].freeze
CALLBACK_TYPES =
ActiveSupport::OrderedOptions.new.merge(
  before: nil,
  around: nil,
  after: nil
).freeze
DEFAULT_CONFIG =
ActiveSupport::OrderedOptions.new.merge(
  enable_without_connection:      false,
  enable_statement_pooling:       false,
  enable_idle_connection_manager: false,
  connection_pool_queue_type:     :fifo,
  warmup_connections:             false,
  min_idle_connections:           0,
  max_idle_connections:           ::Float::INFINITY,
  max_idle_time:                  0,
  idle_check_interval:            0,
  callbacks:                      ActiveSupport::OrderedOptions.new
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



100
101
102
103
104
# File 'lib/advanced_connection/config.rb', line 100

def initialize
  @loaded = false
  @config = DEFAULT_CONFIG.deep_dup
  @mutex  = Monitor.new
end

Instance Attribute Details

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



97
98
99
# File 'lib/advanced_connection/config.rb', line 97

def loaded
  @loaded
end

Class Method Details

.add_callback(*names) ⇒ Object Also known as: add_callbacks



66
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
# File 'lib/advanced_connection/config.rb', line 66

def add_callback(*names)
  Array(names).flatten.each { |name|
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{name}_callbacks
        @config.callbacks.#{name} ||= CALLBACK_TYPES.dup
      end

      def #{name}_callbacks=(value)
        if not value.is_a? Hash
          fail Error::ConfigError, "#{name} callbacks must be a hash"
        elsif (bad_options = (value.keys.collect(&:to_sym) - CALLBACK_TYPES.keys)).size > 0
          plural = bad_options .size > 1 ? 's' : ''
          fail Error::ConfigError, "Unexpected callback option\#{plural}: " \
                                   " `\#{bad_options.join('`, `')}`"
        elsif (uncallable = value.select { |k,v| !v.respond_to? :call }).present?
          plural = uncallable.size > 1 ? 's' : ''
          fail Error::ConfigError, "Expected #{name} callback\#{plural}" \
                                   " `\#{uncallable.keys.join('`, `')}` to be callable"
        end

        @config.callbacks.#{name} = CALLBACK_TYPES.merge(value)
      end
    EOS
    DEFAULT_CONFIG.callbacks[name.to_sym] = CALLBACK_TYPES.dup
  }
end

.include?(key) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/advanced_connection/config.rb', line 62

def include?(key)
  instance.include?(key) || super
end

.method_missing(method, *args, &block) ⇒ Object



53
54
55
56
# File 'lib/advanced_connection/config.rb', line 53

def method_missing(method, *args, &block)
  return super unless instance.include?(method) || instance.respond_to?(method)
  instance.public_send(method, *args, &block)
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/advanced_connection/config.rb', line 58

def respond_to_missing?(method, include_private = false)
  instance.respond_to?(method, include_private) || super
end

Instance Method Details

#[](key) ⇒ Object



123
124
125
# File 'lib/advanced_connection/config.rb', line 123

def [](key)
  @config[key.to_sym]
end

#[]=(key, value) ⇒ Object



127
128
129
# File 'lib/advanced_connection/config.rb', line 127

def []=(key, value)
  public_send("#{key}=".to_sym, value)
end

#callbacksObject



139
140
141
# File 'lib/advanced_connection/config.rb', line 139

def callbacks
  @config.callbacks
end

#can_enable?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'lib/advanced_connection/config.rb', line 111

def can_enable?
  # don't enable if we're running rake tasks generally,
  # and specifically, if it's db: or assets: tasks
  return false if $0.include? 'rake'
  return false if ARGV.grep(/^(assets|db):/).any?
  true
end

#connection_pool_queue_typeObject



241
242
243
# File 'lib/advanced_connection/config.rb', line 241

def connection_pool_queue_type
  @config[:connection_pool_queue_type]
end

#connection_pool_queue_type=(value) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/advanced_connection/config.rb', line 245

def connection_pool_queue_type=(value)
  unless value.is_a?(String) || value.is_a?(Symbol)
    fail Error::ConfigError, 'Expected String or Symbol for connection_pool_queue_type ' \
                       "but found `#{value.class.name}`"
  end

  unless VALID_QUEUE_TYPES.include? value.to_sym
    fail Error::ConfigError, 'Expected connection_pool_queue_type to be one of ' \
                       ':fifo, :lifo, :stack, :prefer_younger, or :prefer_older ' \
                       "but found `#{value.inspect}`"
  end
  synchronize { @config[:connection_pool_queue_type] = value }
end

#enable_idle_connection_managerObject



165
166
167
# File 'lib/advanced_connection/config.rb', line 165

def enable_idle_connection_manager
  @config[:enable_idle_connection_manager]
end

#enable_idle_connection_manager=(value) ⇒ Object



169
170
171
# File 'lib/advanced_connection/config.rb', line 169

def enable_idle_connection_manager=(value)
  synchronize { @config[:enable_idle_connection_manager] = !!value }
end

#enable_statement_poolingObject



154
155
156
# File 'lib/advanced_connection/config.rb', line 154

def enable_statement_pooling
  @config[:enable_statement_pooling]
end

#enable_statement_pooling=(value) ⇒ Object



158
159
160
161
162
163
# File 'lib/advanced_connection/config.rb', line 158

def enable_statement_pooling=(value)
  if enable_without_connection && !!value
    raise Error::ConfigError, "Statement Pooling conflicts with WithoutConnection feature"
  end
  synchronize { @config[:enable_statement_pooling] = !!value }
end

#enable_without_connectionObject



143
144
145
# File 'lib/advanced_connection/config.rb', line 143

def enable_without_connection
  @config[:enable_without_connection]
end

#enable_without_connection=(value) ⇒ Object



147
148
149
150
151
152
# File 'lib/advanced_connection/config.rb', line 147

def enable_without_connection=(value)
  if enable_statement_pooling && !!value
    raise Error::ConfigError, "WithoutConnection blocks conflict with Statement Pooling feature"
  end
  synchronize { @config[:enable_without_connection] = !!value }
end

#idle_check_intervalObject



229
230
231
# File 'lib/advanced_connection/config.rb', line 229

def idle_check_interval
  @config[:idle_check_interval]
end

#idle_check_interval=(value) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/advanced_connection/config.rb', line 233

def idle_check_interval=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected idle_check_interval to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize { @config[:idle_check_interval] = value.to_i }
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/advanced_connection/config.rb', line 131

def include?(key)
  @config.include? key.to_s.tr('=', '').to_sym
end

#loaded!Object



119
120
121
# File 'lib/advanced_connection/config.rb', line 119

def loaded!
  synchronize { @loaded = true }
end

#max_idle_connectionsObject



198
199
200
# File 'lib/advanced_connection/config.rb', line 198

def max_idle_connections
  @config[:max_idle_connections]
end

#max_idle_connections=(value) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/advanced_connection/config.rb', line 202

def max_idle_connections=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected max_idle_connections to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize {
    @config[:max_idle_connections] = begin
      value.to_i
    rescue FloatDomainError
      raise unless $!.message =~ /infinity/i
      ::Float::INFINITY
    end
  }
end

#max_idle_timeObject



217
218
219
# File 'lib/advanced_connection/config.rb', line 217

def max_idle_time
  @config[:max_idle_time]
end

#max_idle_time=(value) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/advanced_connection/config.rb', line 221

def max_idle_time=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected max_idle_time to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize { @config[:max_idle_time] = value.to_i }
end

#min_idle_connectionsObject



186
187
188
# File 'lib/advanced_connection/config.rb', line 186

def min_idle_connections
  @config[:min_idle_connections]
end

#min_idle_connections=(value) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/advanced_connection/config.rb', line 190

def min_idle_connections=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected min_idle_connections to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize { @config[:min_idle_connections] = value.to_i }
end

#to_hObject



135
136
137
# File 'lib/advanced_connection/config.rb', line 135

def to_h
  @config.dup
end

#warmup_connectionsObject



173
174
175
# File 'lib/advanced_connection/config.rb', line 173

def warmup_connections
  @config[:warmup_connections]
end

#warmup_connections=(value) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/advanced_connection/config.rb', line 177

def warmup_connections=(value)
  unless !!value || value.is_a?(Fixnum) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected warmup_connections to be nil, false ' \
                       "or a valid positive integer, but found `#{value.inspect}`"
  end

  synchronize { @config[:warmup_connections] = value.to_s =~ /^\d+$/ ? value.to_i : false }
end