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 and .configurable_by

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:

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

#configConfig, Object

Returns current instance of config

the method returns the same instance until reset_config is called

Examples:

General usage

factory = Sinclair::ConfigFactory.new
factory.add_configs(:name)
factory.configure { |c| c.name 'John' }

config = factory.config

config.class.superclass       # returns Sinclair::Config
factory.config.equal?(config) # returns true
config.name                   # returns 'John'

Returns:

  • (Config, Object)

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

See Also:



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

def config
  config_factory.config
end

#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:



156
157
158
159
160
161
162
163
# File 'lib/sinclair/configurable.rb', line 156

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:



107
108
109
# File 'lib/sinclair/configurable.rb', line 107

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

#configure {|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

Examples:

Setting name on config

class MyConfig
  extend Sinclair::ConfigClass

  attr_reader :name
end

factory = Sinclair::ConfigFactory.new(
  config_class: MyConfig,
  config_attributes: [:name]
)

config = factory.config

factory.configure { name 'John' }

config.name # returns 'John'

Yields:

Returns:

  • (Object)

    the result of the block

See Also:



42
43
44
# File 'lib/sinclair/configurable.rb', line 42

def configure(&block)
  config_factory.configure(&block)
end

#reset_configNilClass

Cleans the current config instance

After cleaning it, #config will generate a new instance

Examples:

factory = Sinclair::ConfigFactory.new

config = factory.config

factory.reset_config

factory.config == config # returns false

Returns:

  • (NilClass)

See Also:



36
37
38
# File 'lib/sinclair/configurable.rb', line 36

def reset_config
  config_factory.reset_config
end