Class: SimpleConfig::Config
Instance Method Summary collapse
- #configure(&block) ⇒ Object
-
#exists?(key) ⇒ Boolean
Returns whether a variable with given
keyis set. - #get(key) ⇒ Object
- #group(name, &block) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #load(external_config_file, options = {}) ⇒ Object
- #set(key, value) ⇒ Object
-
#unset(key) ⇒ Object
Unsets any variable with given
keyand returns variable value if it exists, nil otherwise.
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
37 38 39 40 |
# File 'lib/simple_config.rb', line 37 def initialize @groups = {} @settings = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/simple_config.rb', line 119 def method_missing(method_name, *args) case true when @groups.key?(method_name) return @groups[method_name] when @settings.key?(method_name) return get(method_name) else super(method_name, *args) end end |
Instance Method Details
#configure(&block) ⇒ Object
42 43 44 |
# File 'lib/simple_config.rb', line 42 def configure(&block) instance_eval(&block) end |
#exists?(key) ⇒ Boolean
Returns whether a variable with given key is set.
Please note that this method doesn’t care about variable value. A nil variable is considered as set.
exists? :bar # => false
set :bar, 'foo'
exists? :bar # => true
set :bar, nil
exists? :bar # => true
Use unset to completely remove a variable from the collection.
set :bar, 'foo'
exists? :bar # => true
unset :bar
exists? :bar # => false
99 100 101 |
# File 'lib/simple_config.rb', line 99 def exists?(key) @settings.key?(key) end |
#get(key) ⇒ Object
56 57 58 |
# File 'lib/simple_config.rb', line 56 def get(key) @settings[key] end |
#group(name, &block) ⇒ Object
46 47 48 49 50 |
# File 'lib/simple_config.rb', line 46 def group(name, &block) returning @groups[name] ||= Config.new do |group| group.configure(&block) if block_given? end end |
#load(external_config_file, options = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/simple_config.rb', line 103 def load(external_config_file, ={}) = {:if_exists? => false}.merge() if [:if_exists?] return unless File.exist?(external_config_file) end case File.extname(external_config_file) when /rb/ instance_eval(File.read(external_config_file)) when /yml|yaml/ YAMLParser.parse_contents_of_file(external_config_file).parse_into(self) end end |
#set(key, value) ⇒ Object
52 53 54 |
# File 'lib/simple_config.rb', line 52 def set(key, value) @settings[key] = value end |
#unset(key) ⇒ Object
Unsets any variable with given key and returns variable value if it exists, nil otherwise. Any successive call to exists? :key will return false.
exists? :bar # => false
set :bar, 'foo'
exists? :bar # => true
unset :bar # => 'foo'
exists? :bar # => false
73 74 75 |
# File 'lib/simple_config.rb', line 73 def unset(key) @settings.delete(key) end |