Module: Mixlib::Config

Defined in:
lib/mixlib/config.rb

Constant Summary collapse

@@configuration =
Hash.new

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args) ⇒ Object

Allows for simple lookups and setting of configuration options via method calls on Mixlib::Config. If there any arguments to the method, they are used to set the value of the configuration option. Otherwise, it’s a simple get operation.

Parameters

method_symbol<Symbol>

The method called. Must match a configuration option.

*args

Any arguments passed to the method

Returns

value

The value of the configuration option.

Raises

<ArgumentError>

If the method_symbol does not match a configuration option.



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mixlib/config.rb', line 143

def method_missing(method_symbol, *args)
  num_args = args.length
  
  # Setting
  if num_args > 0
    method_symbol = $1.to_sym unless (method_symbol.to_s =~ /(.+)=$/).nil?
    @@configuration[method_symbol] = (num_args == 1 ? args[0] : args)        
  end
  
  # Returning
  @@configuration[method_symbol]        

end

Instance Method Details

#[](config_option) ⇒ Object

Get the value of a configuration option

Parameters

config_option<Symbol>

The configuration option to return

Returns

value

The value of the configuration option

Raises

<ArgumentError>

If the configuration option does not exist



57
58
59
# File 'lib/mixlib/config.rb', line 57

def [](config_option)
  @@configuration[config_option.to_sym]
end

#[]=(config_option, value) ⇒ Object

Set the value of a configuration option

Parameters

config_option<Symbol>

The configuration option to set (within the [])

value

The value for the configuration option

Returns

value

The new value of the configuration option



69
70
71
# File 'lib/mixlib/config.rb', line 69

def []=(config_option, value)
  internal_set(config_option,value)
end

#configure(&block) ⇒ Object

Pass Mixlib::Config.configure() a block, and it will yield @@configuration.

Parameters

<block>

A block that is sent @@configuration as its argument



43
44
45
# File 'lib/mixlib/config.rb', line 43

def configure(&block)
  block.call(@@configuration)
end

#from_file(filename) ⇒ Object

Loads a given ruby file, and runs instance_eval against it in the context of the current object.

Raises an IOError if the file cannot be found, or is not readable.

Parameters

<string>

A filename to read from



31
32
33
34
35
36
37
# File 'lib/mixlib/config.rb', line 31

def from_file(filename)
  begin
    self.instance_eval(IO.read(filename), filename, 1)
  rescue Exception => e
    raise IOError, "Cannot open or read #{filename}!" + e
  end
end

#has_key?(key) ⇒ Boolean

Check if Mixlib::Config has a configuration option.

Parameters

key<Symbol>

The configuration option to check for

Returns

<True>

If the configuration option exists

<False>

If the configuration option does not exist



81
82
83
# File 'lib/mixlib/config.rb', line 81

def has_key?(key)
  @@configuration.has_key?(key.to_sym)
end

#hash_dupObject

Creates a shallow copy of the internal hash

Returns

result of Hash#dup



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

def hash_dup
  @@configuration.dup
end

#keysObject

Return the set of config hash keys

Returns

result of Hash#keys



100
101
102
# File 'lib/mixlib/config.rb', line 100

def keys
  @@configuration.keys
end

#merge!(hash) ⇒ Object

Merge an incoming hash with our config options

Parameters

hash<Hash>

The incoming hash

Returns

result of Hash#merge!



92
93
94
# File 'lib/mixlib/config.rb', line 92

def merge!(hash)
  @@configuration.merge!(hash)
end