Method: Moneta::Config::ClassMethods#config

Defined in:
lib/moneta/config.rb

#config(name, coerce: nil, default: nil, required: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/moneta/config.rb', line 8

def config(name, coerce: nil, default: nil, required: false, &block)
  raise ArgumentError, 'name must be a symbol' unless Symbol === name

  defaults = config_defaults

  raise ArgumentError, "#{name} is already a config option" if defaults.key?(name)
  raise ArgumentError, "coerce must respond to :to_proc" if coerce && !coerce.respond_to?(:to_proc)

  defaults.merge!(name => default.freeze).freeze
  instance_variable_set :@config_defaults, defaults

  instance_variable_set :@config_coercions, config_coercions.merge!(name => coerce.to_proc) if coerce
  instance_variable_set :@config_required_keys, config_required_keys.add(name).freeze if required
  instance_variable_set :@config_blocks, config_blocks.merge!(name => block) if block
end