Class: SimpleConfig

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

Constant Summary collapse

VERSION =
'0.0.6'
AUTHORS =
["Brendan Baldwin"]
EMAIL =
["[email protected]"]
DESCRIPTION =
%q{This is a really simple system for getting configuration data into your app.  See the wiki at http://github.com/brendan/simpleconfig/wikis for usage.}
FOLDER =
File.expand_path(File.join(File.dirname(__FILE__),'..'))
MANIFEST =
Dir.glob(File.join(FOLDER,'**','*')).map{|path|path[FOLDER.size+1..-1]}
HOMEPAGE =
"http://github.com/brendan/simpleconfig/wikis"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object (private)



94
95
96
97
98
99
100
101
102
103
# File 'lib/simpleconfig.rb', line 94

def method_missing(method_id, *arguments, &block)
  return super if method(method_id) rescue nil
  if defined?(@folders)
    data = load_and_merge(@folders.reverse.map {|f| File.join(f,"#{method_id}.yml")})
    self[method_id] = data unless data == nil
  end
  return super unless key?(method_id)
  class << self; self; end.send(:define_method, method_id) { self[method_id] }
  send(method_id)
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/simpleconfig.rb', line 14

def [](key)
  super key.to_s
end

#[]=(key, value) ⇒ Object



18
19
20
21
# File 'lib/simpleconfig.rb', line 18

def []=(key,value)
  value = self.class.new.replace(value) if value.is_a?(Hash) and !value.is_a?(self.class)
  super key.to_s, value
end

#fetch(key, default = nil, &block) ⇒ Object



23
24
25
# File 'lib/simpleconfig.rb', line 23

def fetch(key, default=nil, &block)
  super(key.to_s, default, &block)
end

#freezeObject



27
28
29
30
31
32
# File 'lib/simpleconfig.rb', line 27

def freeze
  values.each do |value|
    value.freeze if value.respond_to?(:freeze)
  end
  super
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/simpleconfig.rb', line 34

def has_key?(key)
  super key.to_s
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/simpleconfig.rb', line 38

def key?(key)
  super key.to_s
end

#merge!(other) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/simpleconfig.rb', line 42

def merge!(other)
  other.each do |key, value|
    if self[key].is_a?(Hash) and value.is_a?(Hash)
      self[key].merge!(value)
    else
      self[key] = value
    end
  end
end

#replace(other) ⇒ Object



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

def replace(other)
  clear
  other.each do |key, value|
    self[key] = value
  end
  self
end