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.



30
31
32
33
# File 'lib/simple_config.rb', line 30

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

Instance Method Details

#configure(&block) ⇒ Object



41
42
43
# File 'lib/simple_config.rb', line 41

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

#dupObject



35
36
37
38
39
# File 'lib/simple_config.rb', line 35

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.



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

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

#get(key) ⇒ Object



58
59
60
# File 'lib/simple_config.rb', line 58

def get(key)
  @settings[key]
end

#group(name, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/simple_config.rb', line 45

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/simple_config.rb', line 135

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



131
132
133
# File 'lib/simple_config.rb', line 131

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

#merge!(hash) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/simple_config.rb', line 120

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



52
53
54
55
56
# File 'lib/simple_config.rb', line 52

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

#set?(key) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/simple_config.rb', line 108

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

#to_hashObject



112
113
114
115
116
117
118
# File 'lib/simple_config.rb', line 112

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.



76
77
78
79
# File 'lib/simple_config.rb', line 76

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