Module: Sinclair::Configurable

Defined in:
lib/sinclair/configurable.rb

Overview

Module capable of giving configuration capability

By extending Configurable, class receives the methods public .config, .reset_config and .configure and the private methods .configurable_with, .configurable_by and .as_options

Examples:

Configuring with common Config class

module MyConfigurable
  extend Sinclair::Configurable

  # port is defaulted to 80
  configurable_with :host, port: 80
end

MyConfigurable.configure do
  host 'interstella.com'
  port 5555
end

MyConfigurable.config.host
# returns 'interstella.com'

MyConfigurable.config.port
# returns 5555

MyConfigurable.reset_config

MyConfigurable.config.host
# returns nil

MyConfigurable.config.port
# returns 80

Configured by custom config class

class MyServerConfig < Sinclair::Config
  config_attributes :host, :port

  def url
    if @port
      "http://#{@host}:#{@port}"
    else
      "http://#{@host}"
    end
  end
end

class Client
  extend Sinclair::Configurable

  configurable_by MyServerConfig
end

Client.configure do
  host 'interstella.com'
end

Client.config.url # returns 'http://interstella.com'

Client.configure do |config|
  config.port 8080
end

Client.config.url # returns 'http://interstella.com:8080'

See Also:

Author:

  • darthjee

Constant Summary collapse

CONFIG_CLASS_WARNING =

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

Deprecation warning message

'Config classes attributes should ' \
'be defined inside the class or through the usage of ' \
"configurable_with.\n" \
"In future releases this will be enforced.\n" \
'see more on https://github.com/darthjee/sinclair/blob/master/WARNINGS.md#usage-of-custom-config-classes'

Instance Method Summary collapse

Instance Method Details

#as_options(options_hash = {}) ⇒ Sinclair::Option

Returns options with configurated values

Examples:

returning default options

class LoginConfig < Sinclair::Config
  add_configs :password, username: 'bob'
end

class LoginConfigurable
  extend Sinclair::Configurable

  configurable_by LoginConfig
end

LoginConfigurable.configure do |conf|
  conf.username :some_username
  conf.password :some_password
end

options = LoginConfigurable.config.as_options

config.as_options.username # returns :some_username
config.as_options.password # returns :some_password

returning custom options

LoginConfigurable.configure do |conf|
  conf.username :some_username
  conf.password :some_password
end

options = LoginConfigurable.config.as_options(
  password: :correct_password
)

config.as_options.username # returns :some_username
config.as_options.password # returns :correct_password

Parameters:

  • options_hash (Hash) (defaults to: {})

    optional values for the options

Returns:

  • (Sinclair::Option)


89
# File 'lib/sinclair/configurable.rb', line 89

delegate :as_options, to: :config

#configConfig, Object

Returns current instance of config

the method returns the same instance until reset_config is called

Returns:

  • (Config, Object)

    the instance of given config_class. by default, this returns Class.new(Config).new

See Also:



# File 'lib/sinclair/configurable.rb', line 30

#configurable_by(config_class, with: []) ⇒ ConfigFactory

Allows configuration to happen through custom config class

configurable_with does not add methods to config_class. If needed, those can be added by a subsequent call to #configurable_with

Examples:

Configured by custom config class

class MyServerConfig < Sinclair::Config
  config_attributes :host, :port

  def url
    if @port
      "http://#{@host}:#{@port}"
    else
      "http://#{@host}"
    end
  end
end

class Client
  extend Sinclair::Configurable

  configurable_by MyServerConfig
end

Client.configure do
  host 'interstella.com'
end

Client.config.url # returns 'http://interstella.com'

Client.configure do |config|
  config.port 8080
end

Client.config.url # returns 'http://interstella.com:8080'

Parameters:

  • config_class (Class)

    custom configuration class

  • with (Array<Symbol,String>) (defaults to: [])

    List of all configuration attributes expected to be found on config_class

Returns:



201
202
203
204
205
206
207
208
# File 'lib/sinclair/configurable.rb', line 201

def configurable_by(config_class, with: [])
  warn CONFIG_CLASS_WARNING if with.present?

  @config_factory = ConfigFactory.new(
    config_class: config_class,
    config_attributes: with.map(&:to_sym)
  )
end

#configurable_with(*attributes) ⇒ Array<Symbol>

Adds a configuration option to config class

Examples:

Configuring with common Sinclair::Config class

module MyConfigurable
  extend Sinclair::Configurable

  # port is defaulted to 80
  configurable_with :host, port: 80
end

MyConfigurable.configure do
  host 'interstella.com'
  port 5555
end

MyConfigurable.config.host
# returns 'interstella.com'

MyConfigurable.config.port
# returns 5555

MyConfigurable.reset_config

MyConfigurable.config.host
# returns nil

MyConfigurable.config.port
# returns 80

Returns:

  • (Array<Symbol>)

    list of possible configurations

See Also:



152
153
154
# File 'lib/sinclair/configurable.rb', line 152

def configurable_with(*attributes)
  config_factory.add_configs(*attributes)
end

#configure(config_hash = {}) {|ConfigBuilder| ... } ⇒ Object

Set the values in the config

The block given is evaluated by the Sinclair::ConfigBuilder where each method missed will be used to set a variable in the config

Parameters:

  • config_hash (Hash) (defaults to: {})

    hash with keys and values for the configuration

Yields:

Returns:

  • (Object)

    the result of the block

See Also:



79
# File 'lib/sinclair/configurable.rb', line 79

delegate :config, :reset_config, :configure, to: :config_factory

#reset_configNilClass

Cleans the current config instance

After cleaning it, #config will generate a new instance

Returns:

  • (NilClass)

See Also:



# File 'lib/sinclair/configurable.rb', line 46