Class: SimpleConfig::Config

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

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



27
28
29
30
# File 'lib/simple_config.rb', line 27

def initialize
  @groups   = {}
  @settings = {}
end

Instance Method Details

#configure(&block) ⇒ Object



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

def configure(&block)
  instance_eval(&block)
end

#dupObject



32
33
34
35
36
# File 'lib/simple_config.rb', line 32

def dup
  self.class.new.tap do |duplicate|
    duplicate.merge!(to_hash)
  end
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

Parameters:

  • The (Symbol)

    key to check.

Returns:

  • (Boolean)

    True if the key is set.



101
102
103
# File 'lib/simple_config.rb', line 101

def exists?(key)
  @settings.key?(key)
end

#get(key) ⇒ Object



55
56
57
# File 'lib/simple_config.rb', line 55

def get(key)
  @settings[key]
end

#group(name, &block) ⇒ Object



42
43
44
45
46
47
# File 'lib/simple_config.rb', line 42

def group(name, &block)
  group = (@groups[name] ||= Config.new)
  group.configure(&block) if block_given?
  define_accessor(name) { group }
  group
end

#load(external_config_file, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/simple_config.rb', line 132

def load(external_config_file, options={})
  options = {:if_exists? => false}.merge(options)

  if options[: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

#merge(hash) ⇒ Object



128
129
130
# File 'lib/simple_config.rb', line 128

def merge(hash)
  dup.merge!(hash)
end

#merge!(hash) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/simple_config.rb', line 117

def merge!(hash)
  hash.each do |key, value|
    if value.is_a?(Hash)
      group(key.to_sym).merge!(value)
    else
      set(key.to_sym, value)
    end
  end
  self
end

#set(key, value) ⇒ Object



49
50
51
52
53
# File 'lib/simple_config.rb', line 49

def set(key, value)
  unset(key) if set?(key)
  define_accessor(key) { value }
  @settings[key] = value
end

#set?(key) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/simple_config.rb', line 105

def set?(key)
  @settings.key?(key)
end

#to_hashObject



109
110
111
112
113
114
115
# File 'lib/simple_config.rb', line 109

def to_hash
  hash = @settings.dup
  @groups.each do |key,group|
    hash[key] = group.to_hash
  end
  hash
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

Parameters:

  • The (Symbol)

    key to unset.

Returns:

  • The current value for :key.



73
74
75
76
# File 'lib/simple_config.rb', line 73

def unset(key)
  singleton_class.send(:remove_method, key)
  @settings.delete(key)
end