Class: Canfig::Config
- Inherits:
-
Object
show all
- Defined in:
- lib/canfig/config.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/canfig/config.rb', line 78
def method_missing(meth, *args, &block)
if meth.to_s.match(/=\Z/)
opt = meth.to_s.gsub(/=/,'').to_sym
return configure_option(opt, args.first) if allowed?(meth)
else
return @options[meth] if allowed?(meth)
end
super
end
|
Instance Method Details
#allowed?(opt) ⇒ Boolean
74
75
76
|
# File 'lib/canfig/config.rb', line 74
def allowed?(opt)
@allowed.include?(opt)
end
|
34
35
36
37
38
|
# File 'lib/canfig/config.rb', line 34
def changed
@changed = {}
@options.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
@changed
end
|
#changed?(key) ⇒ Boolean
40
41
42
|
# File 'lib/canfig/config.rb', line 40
def changed?(key)
changed.key?(key)
end
|
4
5
6
7
8
9
10
11
|
# File 'lib/canfig/config.rb', line 4
def configure(argh={}, &block)
@options ||= {}
save_state! do
configure_with_args argh
configure_with_block &block
end
self
end
|
25
26
27
28
29
30
31
32
|
# File 'lib/canfig/config.rb', line 25
def configure_option(key, val)
raise ArgumentError, "#{key} is not an allowed configuration option" unless allowed?(key)
save_state! do
@changed[key] = [@options[key], val]
@options[key] = val
end
end
|
13
14
15
16
17
|
# File 'lib/canfig/config.rb', line 13
def configure_with_args(argh)
save_state! do
argh.symbolize_keys.each { |key,val| configure_option(key, val) }
end
end
|
19
20
21
22
23
|
# File 'lib/canfig/config.rb', line 19
def configure_with_block(&block)
save_state! do
instance_eval &block if block_given?
end
end
|
#disable_state_saves! ⇒ Object
66
67
68
|
# File 'lib/canfig/config.rb', line 66
def disable_state_saves!
@save_state = false
end
|
#enable_state_saves! ⇒ Object
62
63
64
|
# File 'lib/canfig/config.rb', line 62
def enable_state_saves!
@save_state = true
end
|
#save_state!(&block) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/canfig/config.rb', line 44
def save_state!(&block)
if save_state?
@saved_state = @options.dup
@changed = {}
if block_given?
disable_state_saves!
begin
yield
ensure
enable_state_saves!
end
end
else
yield if block_given?
end
end
|
#save_state? ⇒ Boolean
70
71
72
|
# File 'lib/canfig/config.rb', line 70
def save_state?
@save_state
end
|