conject 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +21 -0
- data/README.md +33 -1
- data/TODO +3 -0
- data/lib/conject/class_ext_construct_with.rb +3 -2
- data/lib/conject/class_finder.rb +21 -3
- data/lib/conject/utilities.rb +4 -0
- data/lib/conject/version.rb +1 -1
- data/spec/acceptance/regression/module_scoping_spec.rb +45 -0
- data/spec/conject/class_finder_spec.rb +25 -2
- data/spec/conject/utilities_spec.rb +20 -0
- data/spec/test_data/namespace/chart/presenter.rb +1 -3
- data/spec/test_data/namespace/somewhere/deep/inside/the/earth.rb +10 -0
- metadata +14 -10
data/CHANGELOG
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Conject v0.0.7
|
2
|
+
(re-release due to release automation fail on my part)
|
3
|
+
Conject v0.0.6
|
4
|
+
* .construct_with now suppost modules - lets you references module-scoped classes like Deeply::Nested::Bird as 'deeply/nested/bird'
|
5
|
+
|
6
|
+
Conject v0.0.5
|
7
|
+
* ObjectContext.configure_objects
|
8
|
+
* ... support :cache => false for objects, causing them to be non-singletons in context
|
9
|
+
* ... support :construct => lambda do |name, context| Something.new end
|
10
|
+
|
11
|
+
Conject v0.0.4
|
12
|
+
* Bug fixes / support for dealing with inheritance, ie, objects that inherit
|
13
|
+
from Conjected objects etc
|
14
|
+
|
15
|
+
Conject v0.0.3
|
16
|
+
* ObjectContext now caches self reference with key 'this_object_context'
|
17
|
+
allowing context injection into objects
|
18
|
+
|
19
|
+
Conject v0.0.1
|
20
|
+
* Automatic Type 1 object construction
|
21
|
+
* Nested ObjectContexts
|
data/README.md
CHANGED
@@ -2,7 +2,39 @@
|
|
2
2
|
|
3
3
|
Retrieve and relate objects within contexts. Provides dependency injection convenience inspired by the simplicity of Google's Guice.
|
4
4
|
|
5
|
-
#
|
5
|
+
# Example: Basic composition #
|
6
6
|
|
7
|
+
require 'conject'
|
7
8
|
|
9
|
+
class Wood
|
10
|
+
def to_s; "Wood"; end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Nails
|
14
|
+
def to_s; "Nails"; end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Fence
|
18
|
+
construct_with :wood, :nails
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
"I'm made of #{wood} and #{nails}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
fence = Conject.default_object_context.get(:fence)
|
26
|
+
puts fence
|
27
|
+
#=> "I'm made of Wood and Nails"
|
28
|
+
|
29
|
+
# Example: Modules as namespaces #
|
30
|
+
|
31
|
+
module Chart
|
32
|
+
class Presenter
|
33
|
+
construct_with 'chart/model', 'chart/view'
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
"I'm a Chart::Presenter composed of a #{model} and a #{view}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
8
40
|
|
data/TODO
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require 'conject/utilities'
|
2
2
|
class Class
|
3
3
|
|
4
4
|
def construct_with(*syms)
|
@@ -38,7 +38,8 @@ class Class
|
|
38
38
|
|
39
39
|
# Define an internal reader method per dependency:
|
40
40
|
syms.each do |object_name|
|
41
|
-
|
41
|
+
mname = Conject::Utilities.generate_accessor_method_name(object_name)
|
42
|
+
class_def_private mname do
|
42
43
|
components[object_name]
|
43
44
|
end
|
44
45
|
end
|
data/lib/conject/class_finder.rb
CHANGED
@@ -3,11 +3,29 @@ module Conject
|
|
3
3
|
class ClassFinder
|
4
4
|
def find_class(name)
|
5
5
|
cname = name.to_s.camelize
|
6
|
-
|
7
|
-
|
6
|
+
cname_components = cname.split("::")
|
7
|
+
dig_for_class Object, cname_components
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def dig_for_class(within, steps)
|
13
|
+
a = steps.shift
|
14
|
+
const = get_constant(within, a)
|
15
|
+
if steps.nil? or steps.empty?
|
16
|
+
const
|
17
|
+
else
|
18
|
+
dig_for_class(const, steps)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_constant(within, name)
|
23
|
+
if within.const_defined?(name)
|
24
|
+
within.const_get(name)
|
8
25
|
else
|
9
|
-
raise "Could not find class
|
26
|
+
raise "Could not find class or module named #{name} within #{within}"
|
10
27
|
end
|
11
28
|
end
|
29
|
+
|
12
30
|
end
|
13
31
|
end
|
data/lib/conject/utilities.rb
CHANGED
@@ -5,6 +5,10 @@ module Conject
|
|
5
5
|
init_arity = klass.instance_method(:initialize).arity
|
6
6
|
init_arity == 0 or (RUBY_VERSION <= "1.9.2" and init_arity == -1)
|
7
7
|
end
|
8
|
+
|
9
|
+
def generate_accessor_method_name(object_name)
|
10
|
+
object_name.to_s.split("/").last.to_sym
|
11
|
+
end
|
8
12
|
end
|
9
13
|
end
|
10
14
|
end
|
data/lib/conject/version.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
2
|
+
|
3
|
+
|
4
|
+
describe "module scoping" do
|
5
|
+
subject { new_object_context }
|
6
|
+
|
7
|
+
before do
|
8
|
+
append_test_load_path "namespace"
|
9
|
+
require 'chart/model'
|
10
|
+
require 'chart/presenter'
|
11
|
+
require 'chart/view'
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
restore_load_path
|
16
|
+
end
|
17
|
+
|
18
|
+
it "constructs objects with module-scoped classes" do
|
19
|
+
obj = subject.get('chart/model')
|
20
|
+
obj.should_not be_nil
|
21
|
+
obj.class.should == Chart::Model
|
22
|
+
end
|
23
|
+
|
24
|
+
it "supports symbols as keys" do
|
25
|
+
obj = subject.get(:'chart/model')
|
26
|
+
obj.should_not be_nil
|
27
|
+
obj.class.should == Chart::Model
|
28
|
+
end
|
29
|
+
|
30
|
+
it "lets objects depend on module-namespaced components" do
|
31
|
+
obj = subject.get('chart/presenter')
|
32
|
+
obj.should_not be_nil
|
33
|
+
obj.class.should == Chart::Presenter
|
34
|
+
|
35
|
+
model = obj.send(:model)
|
36
|
+
model.should be
|
37
|
+
model.class.should == Chart::Model
|
38
|
+
|
39
|
+
view = obj.send(:view)
|
40
|
+
view.should be
|
41
|
+
view.class.should == Chart::View
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
@@ -3,7 +3,10 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
|
3
3
|
describe Conject::ClassFinder do
|
4
4
|
before do
|
5
5
|
append_test_load_path "simple_stuff"
|
6
|
+
append_test_load_path "namespace"
|
6
7
|
require 'some_random_class'
|
8
|
+
require 'chart/model'
|
9
|
+
require 'somewhere/deep/inside/the/earth'
|
7
10
|
end
|
8
11
|
|
9
12
|
after do
|
@@ -25,12 +28,32 @@ describe Conject::ClassFinder do
|
|
25
28
|
it "raises an error if the name doesn't imply a regular class in the current runtime" do
|
26
29
|
lambda do
|
27
30
|
subject.find_class('something_undefined')
|
28
|
-
end.should raise_error(/could not find class.*
|
31
|
+
end.should raise_error(/could not find class.*SomethingUndefined/i)
|
29
32
|
end
|
30
33
|
|
31
34
|
it "raises an error for nil input" do
|
32
35
|
lambda do
|
33
36
|
subject.find_class('something_undefined')
|
34
|
-
end.should raise_error(/could not find class.*
|
37
|
+
end.should raise_error(/could not find class.*SomethingUndefined/i)
|
38
|
+
end
|
39
|
+
|
40
|
+
context "namespaced" do
|
41
|
+
it "can find namespaced classes for objects with / in name" do
|
42
|
+
c = subject.find_class("chart/model")
|
43
|
+
c.should_not be_nil
|
44
|
+
c.should == Chart::Model
|
45
|
+
end
|
46
|
+
|
47
|
+
it "can find deeply namespaced classes for objects with / in name" do
|
48
|
+
c = subject.find_class("somewhere/deep/inside/the/earth")
|
49
|
+
c.should_not be_nil
|
50
|
+
c.should == Somewhere::Deep::Inside::The::Earth
|
51
|
+
end
|
52
|
+
|
53
|
+
it "raises an error for a misstep along the way" do
|
54
|
+
lambda do
|
55
|
+
subject.find_class("somewhere/deep/above/the/earth")
|
56
|
+
end.should raise_error(/could not find.*Above within Somewhere::Deep/i)
|
57
|
+
end
|
35
58
|
end
|
36
59
|
end
|
@@ -27,4 +27,24 @@ describe Conject::Utilities do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
describe ".generate_accessor_method_name" do
|
31
|
+
context "simple object names" do
|
32
|
+
it "returns symbol names straight up" do
|
33
|
+
subject.generate_accessor_method_name(:another_item).should == :another_item
|
34
|
+
end
|
35
|
+
it "converts strings to symbols" do
|
36
|
+
subject.generate_accessor_method_name("the_object").should == :the_object
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "namespaced object names" do
|
41
|
+
it "returns only the last step in the name" do
|
42
|
+
subject.generate_accessor_method_name(:'one/two/three').should == :three
|
43
|
+
end
|
44
|
+
it "converts strings to symbols" do
|
45
|
+
subject.generate_accessor_method_name("/aye/bee/cee").should == :cee
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
30
50
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: conject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
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: 2012-04-
|
12
|
+
date: 2012-04-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70218584593060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70218584593060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70218584592640 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70218584592640
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: simplecov
|
38
|
-
requirement: &
|
38
|
+
requirement: &70218584592140 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70218584592140
|
47
47
|
description: Enable Guice-like dependency injection and contextual object interactions.
|
48
48
|
email:
|
49
49
|
- david.crosby@atomicobject.com
|
@@ -53,8 +53,8 @@ extra_rdoc_files: []
|
|
53
53
|
files:
|
54
54
|
- .gitignore
|
55
55
|
- .rvmrc
|
56
|
+
- CHANGELOG
|
56
57
|
- Gemfile
|
57
|
-
- Gemfile.lock
|
58
58
|
- NOTES.txt
|
59
59
|
- README.md
|
60
60
|
- Rakefile
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- spec/acceptance/regression/default_object_context_spec.rb
|
95
95
|
- spec/acceptance/regression/inherited_dependencies_spec.rb
|
96
96
|
- spec/acceptance/regression/inject_object_context_spec.rb
|
97
|
+
- spec/acceptance/regression/module_scoping_spec.rb
|
97
98
|
- spec/acceptance/regression/nested_contexts_spec.rb
|
98
99
|
- spec/acceptance/regression/non_singleton_spec.rb
|
99
100
|
- spec/conject/borrowed_active_support_inflector_spec.rb
|
@@ -130,6 +131,7 @@ files:
|
|
130
131
|
- spec/test_data/namespace/chart/model.rb
|
131
132
|
- spec/test_data/namespace/chart/presenter.rb
|
132
133
|
- spec/test_data/namespace/chart/view.rb
|
134
|
+
- spec/test_data/namespace/somewhere/deep/inside/the/earth.rb
|
133
135
|
- spec/test_data/simple_stuff/some_random_class.rb
|
134
136
|
- src/user_model.rb
|
135
137
|
- src/user_presenter.rb
|
@@ -154,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
156
|
version: '0'
|
155
157
|
requirements: []
|
156
158
|
rubyforge_project:
|
157
|
-
rubygems_version: 1.8.
|
159
|
+
rubygems_version: 1.8.10
|
158
160
|
signing_key:
|
159
161
|
specification_version: 3
|
160
162
|
summary: Enable Guice-like dependency injection and contextual object interactions.
|
@@ -167,6 +169,7 @@ test_files:
|
|
167
169
|
- spec/acceptance/regression/default_object_context_spec.rb
|
168
170
|
- spec/acceptance/regression/inherited_dependencies_spec.rb
|
169
171
|
- spec/acceptance/regression/inject_object_context_spec.rb
|
172
|
+
- spec/acceptance/regression/module_scoping_spec.rb
|
170
173
|
- spec/acceptance/regression/nested_contexts_spec.rb
|
171
174
|
- spec/acceptance/regression/non_singleton_spec.rb
|
172
175
|
- spec/conject/borrowed_active_support_inflector_spec.rb
|
@@ -203,4 +206,5 @@ test_files:
|
|
203
206
|
- spec/test_data/namespace/chart/model.rb
|
204
207
|
- spec/test_data/namespace/chart/presenter.rb
|
205
208
|
- spec/test_data/namespace/chart/view.rb
|
209
|
+
- spec/test_data/namespace/somewhere/deep/inside/the/earth.rb
|
206
210
|
- spec/test_data/simple_stuff/some_random_class.rb
|