Class: Sinclair::ConfigFactory Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sinclair/config_factory.rb,
lib/sinclair/config_factory/methods_builder.rb

Overview

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

Class responsible for configuring the configuration class

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'

Defined Under Namespace

Classes: MethodsBuilder

Instance Method Summary collapse

Constructor Details

#initialize(config_class: Class.new(Config), config_attributes: []) ⇒ ConfigFactory

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 a new instance of ConfigFactory.

Parameters:

  • config_class (Class) (defaults to: Class.new(Config)) —

    configuration class to be used

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

    list of possible configurations



23
24
25
26
# File 'lib/sinclair/config_factory.rb', line 23

def initialize(config_class: Class.new(Config), config_attributes: [])
  @config_class = config_class
  @config_attributes = config_attributes.dup
end

Instance Method Details

#add_configs(*args) ⇒ 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 possible configurations

It change the configuration class adding methods and keeps track of those configurations so that Sinclair::ConfigBuilder is able to set those values when invoked

Examples:

Adding configuration name

factory = Sinclair::ConfigFactory.new
config = factory.config

config.respond_to? :active
# returns false

factory.add_configs(:active)

config.respond_to? :active
# returns true

Returns:

  • (Array<Symbol>) —

    all known config attributes



82
83
84
85
86
87
88
# File 'lib/sinclair/config_factory.rb', line 82

def add_configs(*args)
  builder = MethodsBuilder.new(config_class, *args)

  builder.build

  config_attributes.concat(builder.config_names.map(&:to_sym))
end

#child ⇒ ConfigFactory

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 a new instance of ConfigFactory

the new instance will have the same config_attributes and for config_class a child of the current config_class

This method is called when initializing Sinclair::Configurable config_factory and the superclass is also configurable

This way, child classes from other Sinclair::Configurable classes will have a config_class that is a child from the original config_class

Returns:



135
136
137
138
139
140
# File 'lib/sinclair/config_factory.rb', line 135

def child
  self.class.new(
    config_class: Class.new(config_class),
    config_attributes: config_attributes
  )
end

#config ⇒ Config, Object

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



40
41
42
# File 'lib/sinclair/config_factory.rb', line 40

def config
  @config ||= config_class.new
end

#configure {|ConfigBuilder| ... } ⇒ Object

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.

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



117
118
119
# File 'lib/sinclair/config_factory.rb', line 117

def configure(&block)
  config_builder.instance_eval(&block)
end

#reset_config ⇒ NilClass

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.

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)


59
60
61
# File 'lib/sinclair/config_factory.rb', line 59

def reset_config
  @config = nil
end