Module: Hutch::Config

Defined in:
lib/hutch/config.rb

Overview

Configuration settings, available everywhere

There are defaults, which can be overridden by ENV variables prefixed by HUTCH_, and each of these can be overridden using the Config.set method.

Examples:

Configuring on the command-line

HUTCH_PUBLISHER_CONFIRMS=false hutch

Constant Summary collapse

ALL_KEYS =

Set of all setting keys

@boolean_keys + @number_keys + @string_keys

Class Method Summary collapse

Class Method Details

.check_attr(attr) ⇒ Object



254
255
256
257
258
# File 'lib/hutch/config.rb', line 254

def self.check_attr(attr)
  unless user_config.key?(attr)
    raise UnknownAttributeError, "#{attr.inspect} is not a valid config attribute"
  end
end

.convert_value(attr, value) ⇒ Object



274
275
276
277
278
279
280
281
# File 'lib/hutch/config.rb', line 274

def self.convert_value(attr, value)
  case attr
  when 'tracer'
    Kernel.const_get(value)
  else
    value
  end
end

.default_configHash

Default settings

Returns:

  • (Hash)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/hutch/config.rb', line 175

def self.default_config
  @settings_defaults.merge({
    mq_client_properties: {},
    mq_exchange_options: {},
    mq_tls_cert: nil,
    mq_tls_key: nil,
    mq_tls_ca_certificates: nil,
    uri: nil,
    log_level: Logger::INFO,
    client_logger: nil,
    require_paths: [],
    error_handlers: [Hutch::ErrorHandlers::Logger.new],
    # note that this is not a list, it is a chain of responsibility
    # that will fall back to "nack unconditionally"
    error_acknowledgements: [],
    setup_procs: [],
    consumer_groups: {},
    tracer: Hutch::Tracers::NullTracer,
    namespace: nil,
    pidfile: nil,
    serializer: Hutch::Serializers::JSON
  })
end

.define_methodsObject



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/hutch/config.rb', line 295

def self.define_methods
  @config.keys.each do |key|
    define_singleton_method(key) do
      get(key)
    end

    define_singleton_method("#{key}=") do |val|
      set(key, val)
    end
  end
end

.env_based_configHash

Override defaults with ENV variables which begin with HUTCH_

Returns:

  • (Hash)


202
203
204
205
206
207
208
# File 'lib/hutch/config.rb', line 202

def self.env_based_config
  env_keys_configured.each_with_object({}) {|attr, result|
    value = ENV[key_for(attr)]

    result[attr] = type_cast(attr, value)
  }
end

.env_keys_configuredArray<Symbol>

Returns:

  • (Array<Symbol>)


211
212
213
214
215
# File 'lib/hutch/config.rb', line 211

def self.env_keys_configured
  ALL_KEYS.each {|attr| check_attr(attr) }

  ALL_KEYS.select { |attr| ENV.key?(key_for(attr)) }
end

.get(attr) ⇒ Object Also known as: []



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

def self.get(attr)
  check_attr(attr.to_sym)
  user_config[attr.to_sym]
end

.initialize(params = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/hutch/config.rb', line 162

def self.initialize(params = {})
  unless @config
    @config = default_config
    define_methods
    @config.merge!(env_based_config)
  end
  @config.merge!(params)
  @config
end

.is_bool(attr) ⇒ Object



227
228
229
# File 'lib/hutch/config.rb', line 227

def self.is_bool(attr)
  @boolean_keys.include?(attr)
end

.is_num(attr) ⇒ Object



240
241
242
# File 'lib/hutch/config.rb', line 240

def self.is_num(attr)
  @number_keys.include?(attr)
end

.key_for(attr) ⇒ Object



222
223
224
225
# File 'lib/hutch/config.rb', line 222

def self.key_for(attr)
  key = attr.to_s.gsub('.', '__').upcase
  "HUTCH_#{key}"
end

.load_from_file(file) ⇒ Object



268
269
270
271
272
# File 'lib/hutch/config.rb', line 268

def self.load_from_file(file)
  YAML.load(ERB.new(File.read(file)).result).each do |attr, value|
    Hutch::Config.send("#{attr}=", convert_value(attr, value))
  end
end

.set(attr, value) ⇒ Object Also known as: []=



244
245
246
247
# File 'lib/hutch/config.rb', line 244

def self.set(attr, value)
  check_attr(attr.to_sym)
  user_config[attr.to_sym] = type_cast(attr, value)
end

.to_bool(value) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/hutch/config.rb', line 231

def self.to_bool(value)
  case value
  when nil, false, '', /^(false|f|no|n|0)$/i
    false
  else
    true
  end
end

.to_hashObject



264
265
266
# File 'lib/hutch/config.rb', line 264

def self.to_hash
  user_config
end

.user_configObject



260
261
262
# File 'lib/hutch/config.rb', line 260

def self.user_config
  @config ||= initialize
end