configurer 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/configurer.rb +25 -12
  3. data/test/test_configurer.rb +14 -1
  4. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
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
- class Hashwrapper < Struct.new(:hash)
5
- def method_missing(meth, *args, &blk); hash[meth] = blk; end
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 = Hashwrapper.new({})
19
+ ::WORLDWIDE = Config.new([{}])
20
+ CONFIGS = Hash.new{ |h,k| h[k] = Config.new([{},::WORLDWIDE.first,{}]) }
9
21
 
10
- def config *syms, &blk
11
- return Hashwrapper.new(@class_overides) if syms.empty?
12
- @class_defaults ||= {}
13
- configs = @class_overides ||= Hash.new{|h,k| ::WORLDWIDE.hash[k] || @class_defaults[k]}
14
- syms.each do |sym|
15
- @class_defaults[sym] = blk if blk
16
- define_method(sym){ self.class.instance_exec &configs[sym] }
17
- (class << self;self;end).send(:define_method, sym){ instance_exec &configs[sym] }
18
- end
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
@@ -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
- - 3
8
- - 1
9
- version: 0.3.1
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-15 00:00:00 -07:00
17
+ date: 2010-09-26 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20