Class: ComplexConfig::Config

Inherits:
Struct
  • Object
show all
Defined in:
lib/complex_config/config.rb

Overview

Configuration class for setting up ComplexConfig behavior

This class provides a structured way to configure the ComplexConfig system, including environment settings, deep freezing behavior, and plugin registration.

Examples:

Basic configuration

ComplexConfig.configure do |config|
  config.deep_freeze = false
  config.env = 'production'
end

Adding custom plugins

ComplexConfig.configure do |config|
  config.add_plugin -> id do
    if base64_string = ask_and_send("#{id}_base64")
      Base64.decode64(base64_string)
    else
      skip
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Initializes a new configuration instance



25
26
27
28
# File 'lib/complex_config/config.rb', line 25

def initialize(*)
  super
  self.plugins = []
end

Instance Attribute Details

#config_dirObject

Returns the value of attribute config_dir

Returns:

  • (Object)

    the current value of config_dir



23
24
25
# File 'lib/complex_config/config.rb', line 23

def config_dir
  @config_dir
end

#deep_freezeObject

Returns the value of attribute deep_freeze

Returns:

  • (Object)

    the current value of deep_freeze



23
24
25
# File 'lib/complex_config/config.rb', line 23

def deep_freeze
  @deep_freeze
end

#envObject

Returns the value of attribute env

Returns:

  • (Object)

    the current value of env



23
24
25
# File 'lib/complex_config/config.rb', line 23

def env
  @env
end

#pluginsObject

Returns the value of attribute plugins

Returns:

  • (Object)

    the current value of plugins



23
24
25
# File 'lib/complex_config/config.rb', line 23

def plugins
  @plugins
end

Instance Method Details

#add_plugin(code) ⇒ self

Adds a plugin to the configuration

Plugins are lambda expressions that can provide custom behavior for configuration attributes.

Parameters:

  • code (Proc)

    The plugin code to add

Returns:

  • (self)

    Returns self for chaining



56
57
58
59
# File 'lib/complex_config/config.rb', line 56

def add_plugin(code)
  plugins << code
  self
end

#configure(provider) ⇒ self

Applies the configuration to a provider

This method sets all configuration attributes on the provider and registers any plugins. It’s called internally by ComplexConfig.configure.

Parameters:

Returns:

  • (self)

    Returns self for chaining



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/complex_config/config.rb', line 37

def configure(provider)
  each_pair do |name, value|
    value.nil? and next
    name == :plugins and next
    provider.__send__("#{name}=", value)
  end
  plugins.each do |code|
    provider.add_plugin(code)
  end
  self
end