Class: Savvy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/savvy/configuration.rb

Overview

Shared configuration object for a Savvy-enabled application

Constant Summary collapse

APP_ENV_VARS =
%w[
  RAILS_ENV
  RACK_ENV
].freeze
DEFAULT_REDIS_ENV_VARS =
%w[
  REDISTOGO_URL
  REDISCLOUD_URL
  BOXEN_REDIS_URL
  REDIS_URL
].freeze
DEFAULT_REDIS_URL =
'redis://localhost:6379'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, env: Savvy.env) ⇒ Configuration

Returns a new instance of Configuration.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/savvy/configuration.rb', line 28

def initialize(root:, env: Savvy.env)
  @root = root
  @env  = env

  @config_file = find_config_file

  @app_name           = root.basename.to_s
  @include_app_env    = false
  @redis_env_vars     = DEFAULT_REDIS_ENV_VARS.dup
  @redis_default_url  = DEFAULT_REDIS_URL
end

Instance Attribute Details

#app_envObject

Returns the value of attribute app_env.



73
74
75
# File 'lib/savvy/configuration.rb', line 73

def app_env
  @app_env
end

#app_nameString #app_name=(new_app_name) ⇒ Object

Overloads:

  • #app_nameString

    Get the current app name

    Returns:

    • (String)
  • #app_name=(new_app_name) ⇒ Object

    Parameters:

    • new_app_name (String, Symbol)

    Raises:

    • (TypeError)

      if provided with an invalid app name



81
82
83
# File 'lib/savvy/configuration.rb', line 81

def app_name
  @app_name
end

#envObject (readonly)

Returns the value of attribute env.



25
26
27
# File 'lib/savvy/configuration.rb', line 25

def env
  @env
end

#include_app_envBoolean Also known as: include_app_env?

Whether the Rails/Rack env should be included when building namespaces.

Returns:

  • (Boolean)


94
95
96
# File 'lib/savvy/configuration.rb', line 94

def include_app_env
  @include_app_env
end

#redis_default_urlString

Returns:

  • (String)


104
105
106
# File 'lib/savvy/configuration.rb', line 104

def redis_default_url
  @redis_default_url
end

#redis_env_vars<String> #redis_env_vars=(new_env_keys) ⇒ Object

Overloads:

  • #redis_env_vars<String>

    Get the current redis env keys

    Returns:

    • (<String>)
  • #redis_env_vars=(new_env_keys) ⇒ Object

    Parameters:

    • new_env_keys (<String>)

    Raises:

    • (TypeError)

      if not an array of strings.



118
119
120
# File 'lib/savvy/configuration.rb', line 118

def redis_env_vars
  @redis_env_vars
end

#rootObject (readonly)

Returns the value of attribute root.



26
27
28
# File 'lib/savvy/configuration.rb', line 26

def root
  @root
end

Instance Method Details

#build_namespace(*parts, prefix: nil, separator: ?.) ⇒ String

Build a namespace that takes environmental and application factors into account.

Parameters:

  • parts (<String>)

    additional parts that will be suffixed after the generated namespace

  • prefix (String) (defaults to: nil)

    a component that will appear before the savvy-generated namepsace

  • separator (String) (defaults to: ?.)

    what separates the components used in the namespace

Returns:

  • (String)


52
53
54
55
56
57
58
59
# File 'lib/savvy/configuration.rb', line 52

def build_namespace(*parts, prefix: nil, separator: ?.)
  [
    prefix,
    app_name,
    ( app_env if include_app_env? ),
    *parts
  ].compact.join(separator)
end

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

Yields:

  • (to_dsl)


61
62
63
64
65
# File 'lib/savvy/configuration.rb', line 61

def configure
  yield to_dsl if block_given?

  return self
end

#read_from_env(*vars, **options) ⇒ String

Returns:

  • (String)

See Also:

  • Savvy::Configuration.[Savvy[Savvy::EnvironmentReader[Savvy::EnvironmentReader#[]]


69
70
71
# File 'lib/savvy/configuration.rb', line 69

def read_from_env(*vars, **options)
  @env[*vars, **options]
end

#setup!Object



40
41
42
43
44
# File 'lib/savvy/configuration.rb', line 40

def setup!
  @app_env = @env[*APP_ENV_VARS]

  load_from_file!
end