Class: Labor::Config

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/labor/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Config

Returns a new instance of Config.



22
23
24
25
# File 'lib/labor/config.rb', line 22

def initialize(parent = nil)
  @config = {}
  @parent = parent unless parent.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (private)



98
99
100
101
102
103
104
# File 'lib/labor/config.rb', line 98

def method_missing(sym, *args)
  if args.length == 0 && @config.has_key?(sym)
    get(sym)
  else
    @parent.get(sym) unless @parent.nil?
  end
end

Instance Method Details

#delete(key) ⇒ Object

Deletes a key from the config.

key - The Symbol key name.

Returns the value of the key.



63
64
65
# File 'lib/labor/config.rb', line 63

def delete(key)
  @config.delete key.to_sym
end

#each(&block) ⇒ Object

Call a block for each key in the config.

block - The Block to execute for each element.

Returns a Boolean of whether or not the key exists.



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

def each(&block)
  @config.each(&block)
end

#exists?(key) ⇒ Boolean

Checks if a key is set in the config.

key - The Symbol key name.

Returns a Boolean of whether or not the key exists.

Returns:

  • (Boolean)


72
73
74
# File 'lib/labor/config.rb', line 72

def exists?(key)
  @config.has_key? key.to_sym
end

#get(key) ⇒ Object Also known as: []

Retreives the value of a key.

key - The Symbol key name.

Returns the value of the key.



53
54
55
# File 'lib/labor/config.rb', line 53

def get(key)
  @config[key.to_sym] 
end

#group(key, &block) ⇒ Object

Checks if a key is set in the config.

key - The Symbol key name

Returns a Boolean of whether or not the key exists.



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

def group(key, &block)
  @config[key.to_sym] = Config.new(self)
  @config[key.to_sym].instance_eval(&block) if block_given?
end

#load(file_name) ⇒ Object

Loads a config file.

file_name - The String path to the file.

Returns Config instance.



32
33
34
35
# File 'lib/labor/config.rb', line 32

def load(file_name)
  instance_eval(File.read(file_name))
  self
end

#set(key, value) ⇒ Object Also known as: []=

Assigns a value to a key.

key - The Symbol key name. value - Anything you’d like to assign to the key.

Returns the value of the key.



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

def set(key, value)
  @config[key.to_sym] = value
end