configatron 2.7.2 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,24 +2,29 @@ require 'singleton'
2
2
 
3
3
  class Configatron
4
4
  include Singleton
5
-
5
+
6
6
  alias_method :send!, :send
7
-
7
+
8
8
  def initialize # :nodoc:
9
9
  @_namespace = [:default]
10
10
  reset!
11
11
  end
12
-
12
+
13
13
  # Forwards the method call onto the 'namespaced' Configatron::Store
14
- def method_missing(sym, *args)
15
- @_store[@_namespace.last].send(sym, *args)
14
+ def method_missing(sym, *args, &block)
15
+ @_store[@_namespace.last].send(sym, *args, &block)
16
16
  end
17
-
17
+
18
+ # respond_to to respond_to
19
+ def respond_to?(method)
20
+ !@_store[@_namespace.last].send(method).nil? || super
21
+ end
22
+
18
23
  # Removes ALL configuration parameters
19
24
  def reset!
20
25
  @_store = {:default => Configatron::Store.new}
21
26
  end
22
-
27
+
23
28
  # Allows for the temporary overriding of parameters in a block.
24
29
  # Takes an optional Hash of parameters that will be applied before
25
30
  # the block gets called. At the end of the block, the temporary
@@ -27,14 +32,14 @@ class Configatron
27
32
  def temp(options = nil)
28
33
  begin
29
34
  temp_start(options)
30
- yield
35
+ yield @_store[@_namespace.last]
31
36
  rescue Exception => e
32
37
  raise e
33
38
  ensure
34
39
  temp_end
35
40
  end
36
41
  end
37
-
42
+
38
43
  def temp_start(options = nil)
39
44
  n_space = rand
40
45
  @_store[n_space] = @_store[@_namespace.last].deep_clone
@@ -43,11 +48,11 @@ class Configatron
43
48
  self.method_missing(:configure_from_hash, options)
44
49
  end
45
50
  end
46
-
51
+
47
52
  def temp_end
48
53
  @_store.delete(@_namespace.pop)
49
54
  end
50
-
55
+
51
56
  begin
52
57
  undef :inspect # :nodoc:
53
58
  undef :nil? # :nodoc:
@@ -55,5 +60,5 @@ class Configatron
55
60
  rescue Exception => e
56
61
  end
57
62
 
58
-
59
- end
63
+
64
+ end
@@ -6,7 +6,7 @@ class Configatron
6
6
  end
7
7
 
8
8
  alias_method :send!, :send
9
-
9
+
10
10
  # Takes an optional Hash of parameters
11
11
  def initialize(options = {}, name = nil, parent = nil)
12
12
  @_name = name
@@ -16,7 +16,7 @@ class Configatron
16
16
  @_protected = []
17
17
  @_locked = false
18
18
  end
19
-
19
+
20
20
  # Returns a Hash representing the configurations
21
21
  def to_hash
22
22
  h = Hash.new
@@ -24,9 +24,9 @@ class Configatron
24
24
  # Descend the tree and hashify each node
25
25
  h[k] = v.is_a?(Store) ? v.to_hash : v
26
26
  }
27
- h
27
+ h
28
28
  end
29
-
29
+
30
30
  def heirarchy
31
31
  path = [@_name]
32
32
  parent = @_parent
@@ -38,13 +38,13 @@ class Configatron
38
38
  path.reverse!
39
39
  path.join('.')
40
40
  end
41
-
41
+
42
42
  def configatron_keys
43
43
  return @_store.keys.collect{|k| k.to_s}.sort
44
44
  end
45
-
45
+
46
46
  # Checks whether or not a parameter exists
47
- #
47
+ #
48
48
  # Examples:
49
49
  # configatron.i.am.alive = 'alive!'
50
50
  # configatron.i.am.exists?(:alive) # => true
@@ -52,7 +52,12 @@ class Configatron
52
52
  def exists?(name)
53
53
  @_store.has_key?(name.to_sym) || @_store.has_key?(name.to_s)
54
54
  end
55
-
55
+
56
+ # respond_to to respond_to
57
+ def respond_to?(name)
58
+ exists?(name) || super
59
+ end
60
+
56
61
  def inspect
57
62
  path = [@_name]
58
63
  parent = @_parent
@@ -92,7 +97,7 @@ class Configatron
92
97
  # Allows for the configuration of the system from a YAML file.
93
98
  # Takes the path to the YAML file. Also takes an optional parameter,
94
99
  # <tt>:hash</tt>, that indicates a specific hash that should be
95
- # loaded from the file.
100
+ # loaded from the file.
96
101
  def configure_from_yaml(path, opts = {})
97
102
  begin
98
103
  yml = ::Yamler.load(path)
@@ -119,7 +124,7 @@ class Configatron
119
124
  val = method_missing(name.to_sym)
120
125
  return val.is_a?(Configatron::Store) ? default_value : val
121
126
  end
122
-
127
+
123
128
  # Removes a parameter. In the case of a nested parameter
124
129
  # it will remove all below it.
125
130
  def remove(name)
@@ -134,13 +139,17 @@ class Configatron
134
139
  self.send("#{name}=", default_value)
135
140
  end
136
141
  end
137
-
142
+
138
143
  def method_missing(sym, *args) # :nodoc:
139
144
  if sym.to_s.match(/(.+)=$/)
140
145
  name = sym.to_s.gsub("=", '').to_sym
141
146
  raise Configatron::ProtectedParameter.new(name) if @_protected.include?(name) || methods_include?(name)
142
147
  raise Configatron::LockedNamespace.new(@_name) if @_locked && !@_store.has_key?(name)
143
148
  @_store[name] = parse_options(*args)
149
+ elsif sym.to_s.match(/(.+)\?/)
150
+ return !@_store[$1.to_sym].blank?
151
+ elsif block_given?
152
+ yield self.send(sym)
144
153
  elsif @_store.has_key?(sym)
145
154
  val = @_store[sym]
146
155
  if val.is_a?(Configatron::Proc)
@@ -151,19 +160,17 @@ class Configatron
151
160
  return res
152
161
  end
153
162
  return val
154
- elsif sym.to_s.match(/(.+)\?/)
155
- return !@_store[$1.to_sym].blank?
156
163
  else
157
164
  store = Configatron::Store.new({}, sym, self)
158
165
  @_store[sym] = store
159
166
  return store
160
167
  end
161
168
  end
162
-
169
+
163
170
  def ==(other) # :nodoc:
164
171
  self.to_hash == other
165
172
  end
166
-
173
+
167
174
  # Prevents a parameter from being reassigned. If called on a 'namespace' then
168
175
  # all parameters below it will be protected as well.
169
176
  def protect(name)
@@ -179,12 +186,12 @@ class Configatron
179
186
  @_protected << k
180
187
  end
181
188
  end
182
-
189
+
183
190
  # Removes the protection of a parameter.
184
191
  def unprotect(name)
185
192
  @_protected.reject! { |e| e == name.to_sym }
186
193
  end
187
-
194
+
188
195
  def unprotect_all!
189
196
  @_protected.clear
190
197
  @_store.keys.each do |k|
@@ -206,7 +213,7 @@ class Configatron
206
213
  raise ArgumentError, "Namespace #{name.inspect} does not exist" if namespace.nil?
207
214
  namespace.unlock!
208
215
  end
209
-
216
+
210
217
  # = DeepClone
211
218
  #
212
219
  # == Version
@@ -274,7 +281,7 @@ class Configatron
274
281
  end
275
282
  end
276
283
  end
277
-
284
+
278
285
  protected
279
286
  def lock!
280
287
  @_locked = true
@@ -285,12 +292,12 @@ class Configatron
285
292
  @_locked = false
286
293
  @_store.values.each { |store| store.unlock! if store.is_a?(Configatron::Store) }
287
294
  end
288
-
295
+
289
296
  private
290
297
  def methods_include?(name)
291
298
  self.methods.include?(RUBY_VERSION > '1.9.0' ? name.to_sym : name.to_s)
292
299
  end
293
-
300
+
294
301
  def parse_options(options)
295
302
  if options.is_a?(Hash)
296
303
  options.each do |k,v|
@@ -308,13 +315,13 @@ class Configatron
308
315
  return options
309
316
  end
310
317
  end
311
-
318
+
312
319
  begin
313
320
  undef :test # :nodoc:
314
321
  rescue Exception => e
315
322
  end
316
-
323
+
317
324
  SYCK_CONSTANT = (RUBY_VERSION.match(/^1\.9/) ? Syck::MergeKey : YAML::Syck::MergeKey)
318
-
325
+
319
326
  end # Store
320
327
  end # Configatron
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: configatron
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.7.2
5
+ version: 2.8.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - markbates
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-23 00:00:00 -04:00
14
- default_executable:
13
+ date: 2011-05-20 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: yamler
@@ -31,7 +30,6 @@ executables: []
31
30
  extensions: []
32
31
 
33
32
  extra_rdoc_files:
34
- - README
35
33
  - LICENSE
36
34
  files:
37
35
  - lib/configatron/configatron.rb
@@ -53,7 +51,6 @@ files:
53
51
  - generators/templates/configatron/production.rb
54
52
  - generators/templates/configatron/test.rb
55
53
  - generators/templates/initializers/configatron.rb
56
- has_rdoc: true
57
54
  homepage: http://www.metabates.com
58
55
  licenses: []
59
56
 
@@ -67,7 +64,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
64
  requirements:
68
65
  - - ">="
69
66
  - !ruby/object:Gem::Version
70
- hash: 534994782528037126
67
+ hash: 2250820605778868147
71
68
  segments:
72
69
  - 0
73
70
  version: "0"
@@ -80,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
77
  requirements: []
81
78
 
82
79
  rubyforge_project: magrathea
83
- rubygems_version: 1.5.3
80
+ rubygems_version: 1.8.2
84
81
  signing_key:
85
82
  specification_version: 3
86
83
  summary: A powerful Ruby configuration system.