Module: Sinclair::ConfigClass

Included in:
Config
Defined in:
lib/sinclair/config_class.rb

Overview

Module with all class methods for Config

Any class that will be used as configuration class should extend ConfigClass as #config_attributes is used to check what configurations have been added

Examples:

Adding configurations to config class

class AppConfig
  extend Sinclair::ConfigClass

  add_configs :secret, app_name: 'MyApp'
end

config = AppConfig.new

config.secret # return nil
config.app_name # return 'MyApp'

config_builder = Sinclair::ConfigBuilder.new(config)

config_builder.secret '123abc'
config_builder.app_name 'MySuperApp'

config.secret # return '123abc'
config.app_name # return 'MySuperApp'

Author:

  • darthjee

Instance Method Summary collapse

Instance Method Details

#add_configs(*names, default) ⇒ MethodsBuilder

Add a config attribute

This method adds an attribute (see #config_attributes) and the method readers

Examples:

Adding configurations to config class

class AppConfig
  extend Sinclair::ConfigClass

  add_configs :secret, app_name: 'MyApp'
end

config = AppConfig.new

config.secret # return nil
config.app_name # return 'MyApp'

config_builder = Sinclair::ConfigBuilder.new(config)

config_builder.secret '123abc'
config_builder.app_name 'MySuperApp'

config.secret # return '123abc'
config.app_name # return 'MySuperApp'

Parameters:

  • names (Array<Symbol,String>)

    List of configuration names to be added

  • default (Hash)

    Configurations that will receive a default value when not configured

Returns:

  • (MethodsBuilder)

See Also:

  • MethodsBuilder#build


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sinclair/config_class.rb', line 82

def add_configs(*args)
  Config::MethodsBuilder.new(self, *args).tap do |builder|
    builder.build

    Sinclair::InputHash.input_hash(*args).each do |name, value|
      options_class.with_options(name => value)
    end

    config_attributes(*builder.config_names)
  end
end

#config_attributes(*attributes) ⇒ Array<Symbol>

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.

Adds an attribute to the list of attributes

The list of attributes represent attribute readers that class instances can respond to

Subclasses will respond to the same attributes as the parent class plus it’s own

This method does not add the method or .attr_reader

Parameters:

  • attributes (Array<Symbol,String>)

    list of attributes the instances should respond to

Returns:

  • (Array<Symbol>)

    all attributes the class have

See Also:

  • #attributes


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sinclair/config_class.rb', line 33

def config_attributes(*attributes)
  @config_attributes ||= []

  if attributes.present?
    new_attributes = attributes.map(&:to_sym) - @config_attributes
    @config_attributes.concat(new_attributes)
  end

  if superclass.is_a?(ConfigClass)
    (superclass.config_attributes.dup + @config_attributes).uniq
  else
    @config_attributes
  end
end

#options_classClass<Sinclair::Options>

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.

Returns the options class exclusive to this configurable

The returned class is configured in parallel with the configurable itself

Returns:



101
102
103
# File 'lib/sinclair/config_class.rb', line 101

def options_class
  @options_class ||= Class.new(superclass.try(:options_class) || Options)
end