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


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

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


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

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


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

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