Module: Sinclair::EnvSettable

Defined in:
lib/sinclair/env_settable.rb,
lib/sinclair/env_settable/builder.rb

Overview

Module to be extended allowing configurations from environment

Examples:

class MyAppClient
  extend Sinclair::EnvSettable

  settings_prefix 'MY_APP'

  with_settings :username, :password, host: 'my-host.com'
end

ENV['MY_APP_USERNAME'] = 'my_login'

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

ENV['MY_APP_HOST'] = 'other-host.com'

MyAppClient.host     # returns 'other-host.com'

Author:

  • darthjee

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Instance Method Details

#settings_prefix(prefix) ⇒ String

Sets environment keys prefix

Examples:

class MyAppClient
  extend Sinclair::EnvSettable

  settings_prefix 'MY_APP'

  with_settings :username, :password, host: 'my-host.com'
end

ENV['MY_APP_USERNAME'] = 'my_login'

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

ENV['MY_APP_HOST'] = 'other-host.com'

MyAppClient.host     # returns 'other-host.com'

Parameters:

  • prefix (String)

    prefix of the env keys

Returns:

  • (String)


44
45
46
# File 'lib/sinclair/env_settable.rb', line 44

def settings_prefix(prefix)
  @settings_prefix = prefix
end

#with_settings(*settings_name, **defaults) ⇒ Array<MethodDefinition>

Adds settings

Examples:

class MyAppClient
  extend Sinclair::EnvSettable

  settings_prefix 'MY_APP'

  with_settings :username, :password, host: 'my-host.com'
end

ENV['MY_APP_USERNAME'] = 'my_login'

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

ENV['MY_APP_HOST'] = 'other-host.com'

MyAppClient.host     # returns 'other-host.com'

Parameters:

  • settings_name (Array<Symbol,String>)

    Name of all settings to be added

  • defaults (Hash)

    Settings with default values

Returns:



61
62
63
# File 'lib/sinclair/env_settable.rb', line 61

def with_settings(*settings_name, **defaults)
  Builder.new(self, @settings_prefix, *settings_name, **defaults).build
end