configurer 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/configurer.rb +25 -12
- data/test/test_configurer.rb +14 -1
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/configurer.rb
CHANGED
@@ -1,20 +1,33 @@
|
|
1
1
|
# a new take on ruby cross-class configuration
|
2
2
|
|
3
3
|
module Configurer
|
4
|
-
|
5
|
-
|
4
|
+
def eigenclass; (class<<self;self;end); end
|
5
|
+
|
6
|
+
class Config < Array
|
7
|
+
def method_missing(meth, *args, &blk); first[meth] = blk; end
|
8
|
+
def mod; @module ||= Module.new; end
|
9
|
+
def []=(sym, blk)
|
10
|
+
me = self
|
11
|
+
last[sym] = blk
|
12
|
+
mod.send(:define_method, sym) do
|
13
|
+
frame = Module===self ? self : self.class
|
14
|
+
frame.instance_exec(&me.inject(nil){ |m, e| m || e[sym] })
|
15
|
+
end
|
16
|
+
end
|
6
17
|
end
|
7
18
|
|
8
|
-
::WORLDWIDE =
|
19
|
+
::WORLDWIDE = Config.new([{}])
|
20
|
+
CONFIGS = Hash.new{ |h,k| h[k] = Config.new([{},::WORLDWIDE.first,{}]) }
|
9
21
|
|
10
|
-
def config
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
22
|
+
def config sym = nil, &blk
|
23
|
+
!sym and CONFIGS[self] or CONFIGS[self][sym] = blk
|
24
|
+
end
|
25
|
+
|
26
|
+
def config_from klass
|
27
|
+
[self, eigenclass].each{ |x| x.send :include, CONFIGS[klass].mod }
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.extend_object klass
|
31
|
+
super; klass.config_from klass
|
19
32
|
end
|
20
33
|
end
|
data/test/test_configurer.rb
CHANGED
@@ -2,7 +2,6 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestConfigable < Test::Unit::TestCase
|
4
4
|
should "set a global config" do
|
5
|
-
|
6
5
|
THING = []
|
7
6
|
|
8
7
|
class Foo
|
@@ -49,4 +48,18 @@ class TestConfigable < Test::Unit::TestCase
|
|
49
48
|
assert_equal 'B', B.new.f
|
50
49
|
end
|
51
50
|
|
51
|
+
should "be transitive via included modules" do
|
52
|
+
module ::C
|
53
|
+
extend Configurer
|
54
|
+
config :f do "A"; end
|
55
|
+
config :g do f; end
|
56
|
+
end
|
57
|
+
class ::D
|
58
|
+
extend Configurer
|
59
|
+
config_from ::C
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_equal 'A', D.new.g
|
63
|
+
end
|
64
|
+
|
52
65
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joe Edelman
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-26 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|