Class: YleTf::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/yle_tf/config.rb,
lib/yle_tf/config/erb.rb,
lib/yle_tf/config/file.rb,
lib/yle_tf/config/loader.rb,
lib/yle_tf/config/defaults.rb

Overview

Configuration object to be used especially by the middleware stack

Defined Under Namespace

Modules: Defaults, ERB Classes: File, Loader

Constant Summary collapse

NotFoundError =
Class.new(Error)
DEFAULT_NOT_FOUND_BLOCK =
lambda do |keys|
  raise NotFoundError, "Configuration key not found: #{keys.join(' > ')}"
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tf_env) ⇒ Config

Returns a new instance of Config.



15
16
17
18
19
20
21
22
23
# File 'lib/yle_tf/config.rb', line 15

def initialize(tf_env)
  Logger.debug("Initializing configuration for the #{tf_env.inspect} environment")

  @tf_env = tf_env
  @module_dir = Pathname.pwd
  @config = Loader.new(tf_env: tf_env, module_dir: module_dir).load

  Logger.debug(inspect)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/yle_tf/config.rb', line 13

def config
  @config
end

#module_dirObject (readonly)

Returns the value of attribute module_dir.



13
14
15
# File 'lib/yle_tf/config.rb', line 13

def module_dir
  @module_dir
end

#tf_envObject (readonly)

Returns the value of attribute tf_env.



13
14
15
# File 'lib/yle_tf/config.rb', line 13

def tf_env
  @tf_env
end

Instance Method Details

#fetch(*keys, &block) ⇒ Object

Returns a value from the configuration hierarchy specified by a list of keys. If the key is not specified, return result of a specied block, or raise ‘NotFoundError` if none specified.



32
33
34
35
36
37
38
39
# File 'lib/yle_tf/config.rb', line 32

def fetch(*keys, &block)
  block ||= DEFAULT_NOT_FOUND_BLOCK

  keys.inject(config) do |conf, key|
    break block.call(keys) if !conf || !conf.key?(key)
    conf[key]
  end
end

#to_sObject



25
26
27
# File 'lib/yle_tf/config.rb', line 25

def to_s
  YAML.dump(config)
end