Module: Sinclair::Settable

Extended by:
ClassMethods
Included in:
ChainSettable, EnvSettable
Defined in:
lib/sinclair/settable.rb,
lib/sinclair/settable/caster.rb,
lib/sinclair/settable/builder.rb,
lib/sinclair/settable/class_methods.rb

Overview

Module to be extended or included, allowing settings to be read from a source

Examples:

Creating a custom settable


module HashSettable
  extend Sinclair::Settable::ClassMethods
  include Sinclair::Settable

  read_with do |key, default: nil|
    self::HASH[key] || default
  end
end

class HashAppClient
  extend HashSettable

  HASH = {}

  with_settings :username, :password, host: 'my-host.com'
  setting_with_options :port, type: :integer
end

ENV[:username] = 'my_login'
ENV[:port]     = '8080'

HashAppClient.username # returns 'my_login'
HashAppClient.password # returns nil
HashAppClient.host     # returns 'my-host.com'
HashAppClient.port     # returns 8080

See Also:

Author:

  • darthjee

Defined Under Namespace

Modules: ClassMethods Classes: Builder, Caster

Instance Method Summary collapse

Methods included from ClassMethods

read_with

Instance Method Details

#settable_moduleModule

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 settable module that the class extends

This is used in order to find out what is the read block used by the settable

Returns:

  • (Module)

    a Sinclair::Settable



53
54
55
56
57
# File 'lib/sinclair/settable.rb', line 53

def settable_module
  @settable_module ||= singleton_class.included_modules.find do |modu|
    modu <= Sinclair::Settable
  end
end

#setting_with_options(*settings_name, **options) ⇒ Array<MethodDefinition>

Add setting with options

Parameters:

  • settings_name (Array<Symbol,String>)

    Name of all settings to be added

  • options (Hash<Symbol, Object>)

    setting exposition options

Options Hash (**options):

  • type (Symbol)

    type to cast the value fetched

  • default (Object)

    Default value

Returns:

See Also:



99
100
101
102
103
# File 'lib/sinclair/settable.rb', line 99

def setting_with_options(*settings_name, **options)
  opts = default_options.merge(options)

  Builder.build(self, *settings_name, **opts)
end

#with_settings(*settings_name, **defaults) ⇒ Hash<Symbol, Object>

Adds settings

Examples:

Creating a custom settable


module HashSettable
  extend Sinclair::Settable::ClassMethods
  include Sinclair::Settable

  read_with do |key, default: nil|
    self::HASH[key] || default
  end
end

class HashAppClient
  extend HashSettable

  HASH = {}

  with_settings :username, :password, host: 'my-host.com'
  setting_with_options :port, type: :integer
end

ENV[:username] = 'my_login'
ENV[:port]     = '8080'

HashAppClient.username # returns 'my_login'
HashAppClient.password # returns nil
HashAppClient.host     # returns 'my-host.com'
HashAppClient.port     # returns 8080
class MyAppClient
  extend Sinclair::EnvSettable

  settings_prefix 'MY_APP'

  with_settings :username, :password, host: 'my-host.com'
  setting_with_options :port, type: :integer
end

ENV['MY_APP_USERNAME'] = 'my_login'
ENV['MY_APP_PORT']     = '8080'

MyAppClient.username # returns 'my_login'
MyAppClient.password # returns nil
MyAppClient.host     # returns 'my-host.com'
MyAppClient.port     # returns 8080

Parameters:

  • settings_name (Array<Symbol,String>)

    Name of all settings to be added

  • defaults (Hash)

    Settings with default values

Returns:

See Also:



77
78
79
80
81
82
83
# File 'lib/sinclair/settable.rb', line 77

def with_settings(*settings_name, **defaults)
  setting_with_options(*settings_name)

  defaults.each do |key, default|
    setting_with_options(key, default: default)
  end
end