Module: Dry::Configurable

Defined in:
lib/dry/configurable.rb,
lib/dry/configurable/error.rb,
lib/dry/configurable/config.rb,
lib/dry/configurable/version.rb,
lib/dry/configurable/config/value.rb,
lib/dry/configurable/nested_config.rb,
lib/dry/configurable/test_interface.rb,
lib/dry/configurable/argument_parser.rb

Overview

Argument parser

Passing and array or arguments, it will decide wich one are arguments and which one are options.

We have a limitation if setting the value without options, as a hash having the same key as one of the valid options, will parse the value as options.

Examples:

p = Dry::Configurable::ArgumentParser.new(['db:sqlite', { reader: true })

p.value # => 'db:sqlite'
p.options # => { reader: true }

Dry::Configurable::ArgumentParser.call(['db:sqlite', { reader: true })
 # => [ 'db:sqlite', { reader: true } ]

Defined Under Namespace

Modules: TestInterface Classes: ArgumentParser, Config, NestedConfig

Constant Summary collapse

Error =
Class.new(::StandardError)
AlreadyDefinedConfig =
::Class.new(Error)
FrozenConfig =
::Class.new(Error)
VERSION =
'0.7.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



176
177
178
# File 'lib/dry/configurable.rb', line 176

def method_missing(method, *args, &block)
  _reader_attributes.include?(method) ? config.public_send(method, *args, &block) : super
end

Class Method Details

.extended(base) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/dry/configurable.rb', line 34

def self.extended(base)
  base.class_eval do
    @_config_mutex = ::Mutex.new
    @_settings = ::Concurrent::Array.new
    @_reader_attributes = ::Concurrent::Array.new
  end
end

Instance Method Details

#_reader_attributesObject



130
131
132
# File 'lib/dry/configurable.rb', line 130

def _reader_attributes
  @_reader_attributes
end

#_settingsObject



126
127
128
# File 'lib/dry/configurable.rb', line 126

def _settings
  @_settings
end

#configDry::Configurable::Config

Return configuration



56
57
58
59
# File 'lib/dry/configurable.rb', line 56

def config
  return @_config if defined?(@_config)
  create_config
end

#configure {|Dry::Configuration::Config| ... } ⇒ Dry::Configurable::Config

Return configuration

Yields:

  • (Dry::Configuration::Config)

Returns:



68
69
70
71
# File 'lib/dry/configurable.rb', line 68

def configure
  raise_frozen_config if frozen?
  yield(config) if block_given?
end

#enable_test_interfaceObject

Mixes in test interface into the configurable module



18
19
20
# File 'lib/dry/configurable/test_interface.rb', line 18

def enable_test_interface
  extend Dry::Configurable::TestInterface
end

#finalize!Dry::Configurable::Config

Finalize and freeze configuration



78
79
80
81
# File 'lib/dry/configurable.rb', line 78

def finalize!
  freeze
  config.finalize!
end

#inherited(subclass) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/dry/configurable.rb', line 43

def inherited(subclass)
  subclass.instance_variable_set(:@_config_mutex, ::Mutex.new)
  subclass.instance_variable_set(:@_settings, @_settings.clone)
  subclass.instance_variable_set(:@_reader_attributes, @_reader_attributes.clone)
  subclass.instance_variable_set(:@_config, @_config.clone) if defined?(@_config)
  super
end

#setting(key, *args) { ... } ⇒ Dry::Configurable::Config

Add a setting to the configuration

Parameters:

  • key (Mixed)

    The accessor key for the configuration value

  • default (Mixed)

    The default config value

Yields:

  • If a block is given, it will be evaluated in the context of and new configuration class, and bound as the default value

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dry/configurable.rb', line 97

def setting(key, *args, &block)
  raise_already_defined_config(key) if defined?(@_config)
  value, options = ArgumentParser.call(args)
  if block
    if block.parameters.empty?
      value = _config_for(&block)
    else
      processor = block
    end
  end

  _settings << ::Dry::Configurable::Config::Value.new(
    key,
    !value.nil? ? value : ::Dry::Configurable::Config::Value::NONE,
    processor || ::Dry::Configurable::Config::DEFAULT_PROCESSOR
  )
  store_reader_options(key, options) if options.any?
end

#settingsArray

Return an array of setting names

Returns:

  • (Array)


121
122
123
# File 'lib/dry/configurable.rb', line 121

def settings
  _settings.map(&:name)
end