codeine 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,11 +21,13 @@ module BarModule
21
21
  end
22
22
 
23
23
 
24
- container_foo = Codeine.container_for(FooModule)
25
- container_foo.register(:logger){"the fancy logger for FooModule"}
24
+ Codeine.configure(FooModule) do |c|
25
+ c.register(:logger){"the fancy logger for FooModule"}
26
+ end
26
27
 
27
- container_bar = Codeine.container_for(BarModule)
28
- container_bar.register(:logger){"the simple logger for BarModule"}
28
+ Codeine.configure(BarModule) do |c|
29
+ c.register(:logger){"the simple logger for BarModule"}
30
+ end
29
31
 
30
32
 
31
33
  FooModule::A.new
@@ -2,7 +2,9 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
2
  require 'codeine'
3
3
 
4
4
  Codeine.activate
5
- Codeine.register(:logger){"some logger"}
5
+ Codeine.configure do |c|
6
+ c.register(:logger){"some logger"}
7
+ end
6
8
 
7
9
  module FooModule
8
10
  class A
@@ -29,6 +29,14 @@ module Codeine
29
29
  end
30
30
  alias :[] :get
31
31
 
32
+ def configure(mod=nil, &block)
33
+ if mod.nil?
34
+ default_container.configure(&block)
35
+ else
36
+ container_for(mod).configure(&block)
37
+ end
38
+ end
39
+
32
40
  end
33
41
 
34
42
  module Utility
@@ -29,5 +29,9 @@ module Codeine
29
29
  def filter(pattern, &block)
30
30
  filters.push :pattern => pattern, :block => block
31
31
  end
32
+
33
+ def configure
34
+ yield self
35
+ end
32
36
  end
33
37
  end
@@ -1,3 +1,3 @@
1
1
  module Codeine
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,44 +3,53 @@ Codeine
3
3
 
4
4
  A simple dependency injector for ruby projects.
5
5
 
6
- Despite how flexible ruby is, and despite the insistence of a large part of the community, I frequently run into the need on larger projects to centrally manage my dependencies. I end up building something like Codeine for each one, so I decided to build it one more time and re-use it.
6
+ Despite how flexible ruby is, and the insistence of [some seriously smart people](http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming), I frequently run into the need on larger projects to centrally manage my dependencies. I end up building something like Codeine for each one, so I decided to build it one more time and re-use it.
7
+
8
+ It turns out I agree with basically [everything Alexey Petrushin wrote here](http://ruby-lang.info/blog/you-underestimate-the-power-of-ioc-3fh)
9
+
7
10
 
8
11
  Usage
9
12
  -----
10
13
 
11
14
  Codeine can operate as either a global or module-local dependency container. Using it globally is the least flexible, but presents the simplest syntax.
12
15
 
13
- require 'codeine'
14
- Codeine.register(:logger){Logger.new}
16
+ ```ruby
17
+ require 'codeine'
18
+ Codeine.register(:logger){Logger.new}
15
19
 
16
- class Foo
17
- codeine_inject :logger
18
-
19
- def initialize
20
- logger.log "Initialized..."
21
- end
22
- end
20
+ class Foo
21
+ codeine_inject :logger
22
+
23
+ def initialize
24
+ logger.log "Initialized..."
25
+ end
26
+ end
27
+ ```
23
28
 
24
29
 
25
30
  For library code, and for larger projects, it's probably a better idea to segregate the containers.
26
31
 
27
- require 'codeine'
32
+ ```ruby
33
+ require 'codeine'
28
34
 
29
35
 
30
- module ProjectA
31
- class Foo
32
- codeine_inject :logger
36
+ module ProjectA
37
+ class Foo
38
+ codeine_inject :logger
33
39
 
34
- def initialize
35
- logger.log "Initialized..."
36
- end
37
- end
40
+ def initialize
41
+ logger.log "Initialized..."
38
42
  end
43
+ end
44
+ end
45
+
46
+ Codeine.configure(ProjectA) do
47
+ c.register(:logger){Logger.new}
48
+ end
39
49
 
40
- container = Codeine.container_for(ProjectA)
41
- container.register(:logger){Logger.new}
50
+ foo = ProjectA::Foo.new
51
+ ```
42
52
 
43
- foo = ProjectA::Foo.new
53
+ The container bound to the ProjectA module will only service injection requests from classes/modules that reside within it
44
54
 
45
55
 
46
- The the container bound to the ProjectA module will only service injection requests from classes/modules that reside within it
@@ -52,4 +52,13 @@ describe Codeine::Container do
52
52
  end
53
53
  end
54
54
 
55
+ describe "#configure" do
56
+ it "should accept a block and yield self" do
57
+ container.configure do |c|
58
+ c.register(:foo) {"bar"}
59
+ end
60
+ container.get(:foo).should == "bar"
61
+ end
62
+ end
63
+
55
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-08 00:00:00.000000000Z
12
+ date: 2011-10-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &13428680 !ruby/object:Gem::Requirement
16
+ requirement: &16349240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *13428680
24
+ version_requirements: *16349240
25
25
  description: A really simple, partitionable, ruby-flavored dependency injector
26
26
  email:
27
27
  - tim.ariyeh@gmail.com