Class: Canfig::Config
- Inherits:
-
Object
show all
- Defined in:
- lib/canfig/config.rb
Instance Method Summary
collapse
-
#[](key) ⇒ Object
-
#[]=(key, val) ⇒ Object
-
#allowed?(opt) ⇒ Boolean
-
#changed ⇒ Object
-
#changed?(key) ⇒ Boolean
-
#clear(*keys) ⇒ Object
-
#configure(argh = {}, file = nil, &block) ⇒ Object
-
#configure_with_args(argh) ⇒ Object
-
#configure_with_block(&block) ⇒ Object
-
#configure_with_file(file) ⇒ Object
-
#disable_state_saves! ⇒ Object
-
#enable_state_saves! ⇒ Object
-
#env(key, default = nil, &block) ⇒ Object
-
#get(key, default = nil, &block) ⇒ Object
-
#method_missing(meth, *args, &block) ⇒ Object
-
#save_state!(&block) ⇒ Object
-
#save_state? ⇒ Boolean
-
#set(key, val) ⇒ Object
-
#state ⇒ Object
-
#thread_configs ⇒ Object
-
#to_h ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/canfig/config.rb', line 123
def method_missing(meth, *args, &block)
if meth.to_s.match(/=\Z/)
opt = meth.to_s.gsub(/=/,'').to_sym
return set(opt, args.first) if allowed?(opt)
else
return get(meth, *args, &block) if allowed?(meth)
end
super
end
|
Instance Method Details
#[](key) ⇒ Object
74
75
76
|
# File 'lib/canfig/config.rb', line 74
def [](key)
get key
end
|
#[]=(key, val) ⇒ Object
65
66
67
|
# File 'lib/canfig/config.rb', line 65
def []=(key, val)
set key, val
end
|
#allowed?(opt) ⇒ Boolean
119
120
121
|
# File 'lib/canfig/config.rb', line 119
def allowed?(opt)
@allowed.include?(opt)
end
|
#changed ⇒ Object
82
83
84
|
# File 'lib/canfig/config.rb', line 82
def changed
Hash[@allowed.map { |key| [key, [@saved_state[key], state[key]]] if @saved_state[key] != state[key] }.compact]
end
|
#changed?(key) ⇒ Boolean
86
87
88
|
# File 'lib/canfig/config.rb', line 86
def changed?(key)
changed.key?(key)
end
|
#clear(*keys) ⇒ Object
59
60
61
62
63
|
# File 'lib/canfig/config.rb', line 59
def clear(*keys)
save_state! do
keys.each { |key| state[key] = nil }
end
end
|
4
5
6
7
8
9
10
11
|
# File 'lib/canfig/config.rb', line 4
def configure(argh={}, file=nil, &block)
save_state! do
configure_with_file file unless file.nil?
configure_with_args argh
configure_with_block &block
end
self
end
|
19
20
21
22
23
|
# File 'lib/canfig/config.rb', line 19
def configure_with_args(argh)
save_state! do
argh.symbolize_keys.each { |key,val| set(key, val) }
end
end
|
25
26
27
28
29
|
# File 'lib/canfig/config.rb', line 25
def configure_with_block(&block)
save_state! do
instance_eval &block if block_given?
end
end
|
13
14
15
16
17
|
# File 'lib/canfig/config.rb', line 13
def configure_with_file(file)
save_state! do
configure_with_args(Canfig::YAML.new(file).load)
end
end
|
#disable_state_saves! ⇒ Object
111
112
113
|
# File 'lib/canfig/config.rb', line 111
def disable_state_saves!
@save_state = false
end
|
#enable_state_saves! ⇒ Object
107
108
109
|
# File 'lib/canfig/config.rb', line 107
def enable_state_saves!
@save_state = true
end
|
#env(key, default = nil, &block) ⇒ Object
49
50
51
52
53
54
55
56
57
|
# File 'lib/canfig/config.rb', line 49
def env(key, default=nil, &block)
state[key] ||= begin
val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
val = default if val.blank?
val = env(val, default, &block) if val && ENV.key?(val.to_s)
val = eval(val) if val == 'true' || val == 'false'
val
end
end
|
#get(key, default = nil, &block) ⇒ Object
69
70
71
72
|
# File 'lib/canfig/config.rb', line 69
def get(key, default=nil, &block)
raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
state[key] || (block_given? ? yield : default)
end
|
#save_state!(&block) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/canfig/config.rb', line 90
def save_state!(&block)
if save_state?
@saved_state = to_h
if block_given?
disable_state_saves!
begin
yield
ensure
enable_state_saves!
end
end
else
yield if block_given?
end
end
|
#save_state? ⇒ Boolean
115
116
117
|
# File 'lib/canfig/config.rb', line 115
def save_state?
@save_state
end
|
#set(key, val) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/canfig/config.rb', line 41
def set(key, val)
raise NoMethodError, "undefined method `#{key.to_s}=' for #{self.to_s}" unless allowed?(key)
save_state! do
state[key] = val
end
end
|
#state ⇒ Object
36
37
38
39
|
# File 'lib/canfig/config.rb', line 36
def state
thread_configs[object_id] ||= {}
thread_configs[object_id]
end
|
#thread_configs ⇒ Object
31
32
33
34
|
# File 'lib/canfig/config.rb', line 31
def thread_configs
Thread.current[:_canfig_configs] ||= {}
Thread.current[:_canfig_configs]
end
|
#to_h ⇒ Object
78
79
80
|
# File 'lib/canfig/config.rb', line 78
def to_h
Hash[@allowed.map { |key| [key, state[key]] }]
end
|