Class: Sinclair::Config

Inherits:
Object show all
Extended by:
ConfigClass
Defined in:
lib/sinclair/config.rb,
lib/sinclair/config/methods_builder.rb

Overview

Base class for configuration when using Configurable

The methods will be added later by ConfigFactory

The instance variables will be set by ConfigBuilder

Author:

  • darthjee

Defined Under Namespace

Classes: MethodsBuilder

Instance Method Summary collapse

Methods included from ConfigClass

add_configs, config_attributes, options_class

Instance Method Details

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

Returns options with configurated values

The returned options will use the values defined in the config merged with the extra attributes

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)


83
84
85
# File 'lib/sinclair/config.rb', line 83

def as_options(options_hash = {})
  self.class.options_class.new(to_hash.merge(options_hash))
end

#to_hashHash

Return all the current configurations in a hash

Examples:

Checking all hash/json formats

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

config = LoginConfig.new

config.to_hash
# returns { 'password' => nil, 'username' => 'bob' }

config.as_json
# returns { 'password' => nil, 'username' => 'bob' }

config.to_json
# returns '{"password":null,"username":"bob"}'

Returns:

  • (Hash)


35
36
37
38
39
# File 'lib/sinclair/config.rb', line 35

def to_hash
  self.class.config_attributes.each_with_object({}) do |attribute, hash|
    hash[attribute.to_s] = public_send(attribute)
  end
end