Class: Locomotive::ConfigurationHash

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

Overview

specialized hash for storing configuration settings

Instance Method Summary collapse

Methods inherited from Hash

#underscore_keys

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

provides member-based access to keys i.e. params.id === params note: all keys are converted to symbols



92
93
94
95
96
97
98
# File 'lib/locomotive/configuration.rb', line 92

def method_missing(name, *args, &block)
  if name.to_s.ends_with? '='
    send :[]=, name.to_s.chomp('=').to_sym, *args
  else
    send(:[], name.to_sym, &block)
  end
end

Instance Method Details

#[](key, &block) ⇒ Object

retrieves the specified key and yields it if a block is provided



78
79
80
81
82
83
84
85
86
87
# File 'lib/locomotive/configuration.rb', line 78

def [](key, &block)
  if block_given?
    self.delete(key) unless super(key).respond_to?(:keys)
    yield(super(key))
  else
    super(key)
  end

  # block_given? ? yield(super(key)) : super(key)
end

#default(key = nil) ⇒ Object

ensure that default entries always produce instances of the ConfigurationHash class



72
73
74
# File 'lib/locomotive/configuration.rb', line 72

def default(key=nil)
  include?(key) ? self[key] : self[key] = self.class.new
end