Class: Configurator::Configuration
- Inherits:
-
Hash
- Object
- Hash
- Configurator::Configuration
show all
- Includes:
- Option
- Defined in:
- lib/configurator/configuration.rb
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
19
20
21
22
23
|
# File 'lib/configurator/configuration.rb', line 19
def initialize(options = {})
options.each do |key, value|
set(key, value)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
50
51
52
53
54
55
56
57
58
|
# File 'lib/configurator/configuration.rb', line 50
def method_missing(method, *args, &block)
method_name = method.to_s
setter = method_name.chomp!('=') || !args.empty? || block_given?
if setter
set(method_name, args.first, &block)
else
get(method)
end
end
|
Instance Method Details
#add_option(name, default, &block) ⇒ Object
7
8
9
|
# File 'lib/configurator/configuration.rb', line 7
def add_option(name, default, &block)
defaults[name.to_sym] = default || block
end
|
#config ⇒ Object
11
12
13
|
# File 'lib/configurator/configuration.rb', line 11
def config
self
end
|
#defaults ⇒ Object
15
16
17
|
# File 'lib/configurator/configuration.rb', line 15
def defaults
@defaults ||= {}
end
|
#get(name) ⇒ Object
25
26
27
28
29
30
31
32
|
# File 'lib/configurator/configuration.rb', line 25
def get(name)
name = name.to_sym
value = self.key?(name) ? self[name] : defaults[name]
if value.respond_to? :call
value = value.call
end
value
end
|
#set(name, value, &block) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/configurator/configuration.rb', line 34
def set(name, value, &block)
name = name.to_sym
if block_given?
self[name] ||= defaults[name] || self.class.new
self[name].instance_eval(&block)
elsif value.is_a?(Hash)
self[name] = defaults[name] || self.class.new
value.each do |key, value_two|
self[name].send("#{key}=", value_two)
end
else
self[name] = value
end
end
|