configatron 2.2.1 → 2.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/configatron.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  base = File.join(File.dirname(__FILE__), 'configatron')
2
2
  require 'yaml'
3
- require File.join(base, 'kernel')
4
3
  require File.join(base, 'configatron')
5
4
  require File.join(base, 'store')
6
- require File.join(base, 'errors')
5
+ require File.join(base, 'errors')
6
+ require File.join(base, 'core_ext', 'kernel')
7
+ require File.join(base, 'core_ext', 'object')
8
+ require File.join(base, 'core_ext', 'string')
9
+ require File.join(base, 'core_ext', 'class')
@@ -0,0 +1,26 @@
1
+ class Class
2
+
3
+ # Returns access to configuration parameters named after the class.
4
+ #
5
+ # Examples:
6
+ # configatron.foo.bar = :bar
7
+ # configatron.a.b.c.d = 'D'
8
+ #
9
+ # class Foo
10
+ # end
11
+ #
12
+ # module A
13
+ # module B
14
+ # class C
15
+ # end
16
+ # end
17
+ # end
18
+ #
19
+ # Foo.to_configatron.bar # => :bar
20
+ # A::B::C.to_configatron.d # => 'D'
21
+ def to_configatron
22
+ name_spaces = self.name.split("::").collect{|s| s.methodize}
23
+ configatron.send_with_chain(name_spaces)
24
+ end
25
+
26
+ end
File without changes
@@ -0,0 +1,9 @@
1
+ class Object # :nodoc:
2
+
3
+ def send_with_chain(methods, *args) # :nodoc:
4
+ obj = self
5
+ [methods].flatten.each {|m| obj = obj.send(m, *args)}
6
+ obj
7
+ end
8
+
9
+ end
@@ -0,0 +1,84 @@
1
+ class String # :nodoc:
2
+
3
+ def underscore # :nodoc:
4
+ self.to_s.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("-", "_").
8
+ downcase
9
+ end
10
+
11
+ def methodize # :nodoc:
12
+ x = self
13
+
14
+ # if we get down to a nil or an empty string raise an exception!
15
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
16
+
17
+ # get rid of the big stuff in the front/back
18
+ x.strip!
19
+
20
+ # if we get down to a nil or an empty string raise an exception!
21
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
22
+
23
+ x = x.underscore
24
+
25
+ # get rid of spaces and make the _
26
+ x.gsub!(' ', '_')
27
+ # get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _
28
+ x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_')
29
+
30
+ # if we get down to a nil or an empty string raise an exception!
31
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
32
+
33
+ # condense multiple 'safe' non a-z chars to just one.
34
+ # ie. ___ becomes _ !!!! becomes ! etc...
35
+ [' ', '_', '?', '!', "="].each do |c|
36
+ x.squeeze!(c)
37
+ end
38
+
39
+ # if we get down to a nil or an empty string raise an exception!
40
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
41
+
42
+ #down case the whole thing
43
+ x.downcase!
44
+
45
+ # get rid of any characters at the beginning that aren't a-z
46
+ while !x.match(/^[a-z]/)
47
+ x.slice!(0)
48
+
49
+ # if we get down to a nil or an empty string raise an exception!
50
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
51
+ end
52
+
53
+ # let's trim this bad boy down a bit now that we've cleaned it up, somewhat.
54
+ # we should do this before cleaning up the end character, because it's possible to end up with a
55
+ # bad char at the end if you trim too late.
56
+ x = x[0..100] if x.length > 100
57
+
58
+ # get rid of any characters at the end that aren't safe
59
+ while !x.match(/[a-z0-9\?\!\=]$/)
60
+ x.slice!(x.length - 1)
61
+ # if we get down to a nil or an empty string raise an exception!
62
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
63
+ end
64
+
65
+ # if we get down to a nil or an empty string raise an exception!
66
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
67
+
68
+ # let's get rid of characters that don't belong in the 'middle' of the method.
69
+ orig_middle = x[1..(x.length - 2)]
70
+ n_middle = orig_middle.dup
71
+
72
+ ['?', '!', "="].each do |c|
73
+ n_middle.gsub!(c, "_")
74
+ end
75
+
76
+ # the previous gsub can leave us with multiple underscores that need cleaning up.
77
+ n_middle.squeeze!("_")
78
+
79
+ x.gsub!(orig_middle, n_middle)
80
+ x.gsub!("_=", "=")
81
+ x
82
+ end
83
+
84
+ end
@@ -0,0 +1,28 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ configatron.foo.bar = :bar
4
+ configatron.a.b.c.d = 'D'
5
+
6
+ class Foo
7
+ end
8
+
9
+ module A
10
+ module B
11
+ class C
12
+ end
13
+ end
14
+ end
15
+
16
+ describe Class do
17
+
18
+ describe 'to_configatron' do
19
+
20
+ it 'should return a Configatron::Store object based on the name of the class' do
21
+ Foo.to_configatron.should be_kind_of(Configatron::Store)
22
+ Foo.to_configatron.bar.should == :bar
23
+ A::B::C.to_configatron.d.should == 'D'
24
+ end
25
+
26
+ end
27
+
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configatron
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-02 00:00:00 -05:00
12
+ date: 2009-01-16 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,11 +23,15 @@ extra_rdoc_files:
23
23
  - README
24
24
  files:
25
25
  - lib/configatron/configatron.rb
26
+ - lib/configatron/core_ext/class.rb
27
+ - lib/configatron/core_ext/kernel.rb
28
+ - lib/configatron/core_ext/object.rb
29
+ - lib/configatron/core_ext/string.rb
26
30
  - lib/configatron/errors.rb
27
- - lib/configatron/kernel.rb
28
31
  - lib/configatron/store.rb
29
32
  - lib/configatron.rb
30
33
  - README
34
+ - doc/classes/Class.html
31
35
  - doc/classes/Configatron/LockedNamespace.html
32
36
  - doc/classes/Configatron/ProtectedParameter.html
33
37
  - doc/classes/Configatron/Store.html
@@ -35,8 +39,11 @@ files:
35
39
  - doc/classes/Kernel.html
36
40
  - doc/created.rid
37
41
  - doc/files/lib/configatron/configatron_rb.html
42
+ - doc/files/lib/configatron/core_ext/class_rb.html
43
+ - doc/files/lib/configatron/core_ext/kernel_rb.html
44
+ - doc/files/lib/configatron/core_ext/object_rb.html
45
+ - doc/files/lib/configatron/core_ext/string_rb.html
38
46
  - doc/files/lib/configatron/errors_rb.html
39
- - doc/files/lib/configatron/kernel_rb.html
40
47
  - doc/files/lib/configatron/store_rb.html
41
48
  - doc/files/lib/configatron_rb.html
42
49
  - doc/files/README.html
@@ -74,6 +81,7 @@ specification_version: 2
74
81
  summary: A powerful Ruby configuration system.
75
82
  test_files:
76
83
  - spec/lib
84
+ - spec/lib/class_spec.rb
77
85
  - spec/lib/configatron_spec.rb
78
86
  - spec/lib/futurama.yml
79
87
  - spec/lib/lost.yml