class Teckel::Config

Public Class Methods

new() click to toggle source

@!visibility private

# File lib/teckel/config.rb, line 6
def initialize
  @config = {}
end

Public Instance Methods

dup() click to toggle source

@!visibility private

Calls superclass method
# File lib/teckel/config.rb, line 42
def dup
  super.tap do |copy|
    copy.instance_variable_set(:@config, @config.dup)
  end
end
for(key, value = nil, &block) click to toggle source

Allow getting or setting a value, with some weird rules:

  • The value might not be nil

  • Setting via value is allowed only once. Successive calls will raise a {FrozenConfigError}

  • Setting via block works almost like {Hash#fetch}:

    • returns the existing value if key is present

    • sets (and returns) the blocks return value otherwise

  • calling without value and block works like {Hash#[]}

@raise [FrozenConfigError] When overwriting a key @!visibility private

# File lib/teckel/config.rb, line 20
def for(key, value = nil, &block)
  if value.nil?
    get_or_set(key, &block)
  elsif @config.key?(key)
    raise FrozenConfigError, "Configuration #{key} is already set"
  else
    @config[key] = value
  end
end
freeze() click to toggle source

@!visibility private

Calls superclass method
# File lib/teckel/config.rb, line 36
def freeze
  @config.freeze
  super
end
replace(key) { || ... } click to toggle source

@!visibility private

# File lib/teckel/config.rb, line 31
def replace(key)
  @config[key] = yield if @config.key?(key)
end

Private Instance Methods

get_or_set(key, &block) click to toggle source
# File lib/teckel/config.rb, line 50
def get_or_set(key, &block)
  if block
    @config[key] ||= @config.fetch(key, &block)
  else
    @config[key]
  end
end