scorpion-ioc 0.6.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +144 -30
  3. data/Gemfile +17 -13
  4. data/README.md +24 -69
  5. data/Rakefile +12 -0
  6. data/app/README +1 -0
  7. data/bin/rspec +2 -1
  8. data/config.ru +2 -2
  9. data/config/environment.rb +1 -0
  10. data/lib/scorpion-ioc.rb +3 -1
  11. data/lib/scorpion.rb +32 -45
  12. data/lib/scorpion/attribute.rb +14 -34
  13. data/lib/scorpion/attribute_set.rb +11 -13
  14. data/lib/scorpion/chain_hunter.rb +4 -4
  15. data/lib/scorpion/dependency.rb +22 -58
  16. data/lib/scorpion/dependency/argument_dependency.rb +4 -4
  17. data/lib/scorpion/dependency/builder_dependency.rb +5 -5
  18. data/lib/scorpion/dependency/captured_dependency.rb +4 -6
  19. data/lib/scorpion/dependency/class_dependency.rb +3 -17
  20. data/lib/scorpion/dependency/module_dependency.rb +1 -1
  21. data/lib/scorpion/dependency_map.rb +16 -16
  22. data/lib/scorpion/error.rb +12 -9
  23. data/lib/scorpion/hunt.rb +33 -34
  24. data/lib/scorpion/hunter.rb +6 -6
  25. data/lib/scorpion/locale/en.yml +2 -2
  26. data/lib/scorpion/method.rb +11 -1
  27. data/lib/scorpion/object.rb +16 -31
  28. data/lib/scorpion/rack.rb +1 -1
  29. data/lib/scorpion/rack/middleware.rb +2 -1
  30. data/lib/scorpion/rails.rb +6 -6
  31. data/lib/scorpion/rails/active_record.rb +4 -4
  32. data/lib/scorpion/rails/active_record/association.rb +3 -3
  33. data/lib/scorpion/rails/active_record/relation.rb +3 -2
  34. data/lib/scorpion/rails/controller.rb +2 -2
  35. data/lib/scorpion/rails/job.rb +1 -1
  36. data/lib/scorpion/rails/mailer.rb +1 -1
  37. data/lib/scorpion/rails/nest.rb +11 -11
  38. data/lib/scorpion/rails/railtie.rb +2 -2
  39. data/lib/scorpion/rspec.rb +2 -2
  40. data/lib/scorpion/rspec/helper.rb +3 -3
  41. data/lib/scorpion/stinger.rb +19 -18
  42. data/lib/scorpion/version.rb +3 -3
  43. data/scorpion.gemspec +13 -13
  44. data/spec/internal/db/schema.rb +1 -1
  45. data/spec/lib/scorpion/attribute_set_spec.rb +4 -22
  46. data/spec/lib/scorpion/attribute_spec.rb +3 -8
  47. data/spec/lib/scorpion/chain_hunter_spec.rb +1 -1
  48. data/spec/lib/scorpion/dependency/argument_dependency_spec.rb +3 -7
  49. data/spec/lib/scorpion/dependency/builder_dependency_spec.rb +7 -7
  50. data/spec/lib/scorpion/dependency/module_dependency_spec.rb +3 -3
  51. data/spec/lib/scorpion/dependency_map_spec.rb +4 -25
  52. data/spec/lib/scorpion/dependency_spec.rb +18 -45
  53. data/spec/lib/scorpion/error_spec.rb +1 -1
  54. data/spec/lib/scorpion/hunt_spec.rb +16 -28
  55. data/spec/lib/scorpion/hunter_spec.rb +29 -21
  56. data/spec/lib/scorpion/object_spec.rb +20 -19
  57. data/spec/lib/scorpion/rack/middleware_spec.rb +4 -4
  58. data/spec/lib/scorpion/rack_spec.rb +1 -1
  59. data/spec/lib/scorpion/rails/active_record/association_spec.rb +3 -3
  60. data/spec/lib/scorpion/rails/active_record/model_spec.rb +3 -3
  61. data/spec/lib/scorpion/rails/active_record/relation_spec.rb +3 -3
  62. data/spec/lib/scorpion/rails/controller_spec.rb +8 -8
  63. data/spec/lib/scorpion/rails/job_spec.rb +1 -1
  64. data/spec/lib/scorpion/rspec/helper_spec.rb +11 -11
  65. data/spec/lib/scorpion_spec.rb +1 -1
  66. data/spec/spec_helper.rb +8 -10
  67. metadata +6 -6
  68. data/lib/scorpion/object_constructor.rb +0 -79
  69. data/spec/lib/scorpion/object_constructor_spec.rb +0 -124
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  module Test
4
4
  module Dependency
@@ -19,70 +19,43 @@ end
19
19
 
20
20
  describe Scorpion::Dependency do
21
21
  context "inheritance" do
22
- let( :dependency ){ Scorpion::Dependency.new( Test::Dependency::Derived ) }
22
+ let( :dependency ) { Scorpion::Dependency.new( Test::Dependency::Derived ) }
23
23
 
24
24
  it "matches more derived class when looking for base class" do
25
- expect( dependency.satisfies? Test::Dependency::Base ).to be_truthy
25
+ expect( dependency.satisfies?(Test::Dependency::Base) ).to be_truthy
26
26
  end
27
27
 
28
28
  it "matches same class when looking for base class" do
29
- expect( dependency.satisfies? Test::Dependency::Derived ).to be_truthy
29
+ expect( dependency.satisfies?(Test::Dependency::Derived) ).to be_truthy
30
30
  end
31
31
 
32
32
  it "does not inherit symbols" do
33
- expect( Scorpion::Dependency.new( :a ).satisfies? :b ).to be_falsy
33
+ expect( Scorpion::Dependency.new( :a ).satisfies?(:b) ).to be_falsy
34
34
  end
35
35
 
36
36
  it "can satisfy a module with a class" do
37
- expect( dependency.satisfies? Test::Dependency::Mod ).to be_truthy
38
- end
39
-
40
- end
41
-
42
- describe "traits" do
43
-
44
- context "symbolic" do
45
- let( :dependency ){ Scorpion::Dependency.new Test::Dependency::Base, :apples }
46
- it "satisfies matched traits" do
47
- expect( dependency.satisfies? Test::Dependency::Base, :apples ).to be_truthy
48
- end
49
-
50
- it "doesn't satisfy mis-matched traits" do
51
- expect( dependency.satisfies? Test::Dependency::Base, :oranges ).to be_falsy
52
- end
53
- end
54
-
55
- context "module" do
56
- let( :dependency ){ Scorpion::Dependency.new Test::Dependency::Derived }
57
-
58
- it "satisfies module traits" do
59
- expect( dependency.satisfies? Test::Dependency::Base, Test::Dependency::Derived ).to be_truthy
60
- end
37
+ expect( dependency.satisfies?(Test::Dependency::Mod) ).to be_truthy
61
38
  end
62
39
 
63
40
  end
64
41
 
65
42
  it "can satisfy symbol contracts" do
66
- expect( Scorpion::Dependency.new( :symbol ).satisfies? :symbol ).to be_truthy
67
- end
68
-
69
- it "satisfies ignores tail hash traits" do
70
- expect( Scorpion::Dependency.new( Test::Dependency::Base ).satisfies?( Test::Dependency::Base, ) )
43
+ expect( Scorpion::Dependency.new( :symbol ).satisfies?(:symbol) ).to be_truthy
71
44
  end
72
45
 
73
46
  describe "equality" do
74
- let( :dependency ) { Scorpion::Dependency.new( Test::Dependency::Derived ) }
47
+ let( :dependency ) { Scorpion::Dependency.new( Test::Dependency::Derived ) }
75
48
  let( :same ) { Scorpion::Dependency.new( Test::Dependency::Derived ) }
76
49
  let( :different ) { Scorpion::Dependency.new( Test::Dependency::Base ) }
77
50
 
78
- specify{ expect( dependency ).to eq same }
79
- specify{ expect( dependency ).not_to eq different }
80
- specify{ expect( dependency.hash ).to eq same.hash }
51
+ specify { expect( dependency ).to eq same }
52
+ specify { expect( dependency ).not_to eq different }
53
+ specify { expect( dependency.hash ).to eq same.hash }
81
54
  end
82
55
 
83
56
  describe ".define" do
84
- let( :scorpion ){ double Scorpion }
85
- let( :hunt ) { Scorpion::Hunt.new scorpion, String, nil }
57
+ let( :scorpion ) { double Scorpion }
58
+ let( :hunt ) { Scorpion::Hunt.new scorpion, String, nil }
86
59
 
87
60
  it "is a ClassDependency for class hunts" do
88
61
  dependency = Scorpion::Dependency.define String
@@ -98,22 +71,22 @@ describe Scorpion::Dependency do
98
71
  dependency = Scorpion::Dependency.define String, return: "AWESEOME"
99
72
 
100
73
  expect( dependency ).to be_a Scorpion::Dependency::BuilderDependency
101
- expect( dependency.fetch hunt ).to eq "AWESEOME"
74
+ expect( dependency.fetch(hunt) ).to eq "AWESEOME"
102
75
  end
103
76
 
104
77
  it "is a BuilderDependency for block hunts" do
105
78
  dependency = Scorpion::Dependency.define String do
106
79
  "YASS"
107
- end
80
+ end
108
81
 
109
82
  expect( dependency ).to be_a Scorpion::Dependency::BuilderDependency
110
83
  end
111
84
 
112
85
  it "is a BuilderDependency for with: option" do
113
- dependency = Scorpion::Dependency.define String, with: ->(scorpion,*args,&block){ "YASSS" }
86
+ dependency = Scorpion::Dependency.define String, with: ->(_scorpion, *_args) { "YASSS" }
114
87
 
115
88
  expect( dependency ).to be_a Scorpion::Dependency::BuilderDependency
116
- expect( dependency.fetch hunt ).to eq "YASSS"
89
+ expect( dependency.fetch(hunt) ).to eq "YASSS"
117
90
  end
118
91
 
119
92
  it "is a BuilderDependency when hunted class implements #create" do
@@ -128,4 +101,4 @@ describe Scorpion::Dependency do
128
101
 
129
102
  end
130
103
 
131
- end
104
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Scorpion::UnsuccessfulHunt do
4
4
  it "formats the default message" do
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
 
4
4
  module Test
@@ -18,48 +18,29 @@ module Test
18
18
  end
19
19
 
20
20
  describe Scorpion::Hunt do
21
-
22
- let( :scorpion ){ double Scorpion }
23
- let( :hunt ){ Scorpion::Hunt.new scorpion, String, nil }
24
21
 
25
- describe "#fetch_by_traits" do
22
+ let( :scorpion ) { double Scorpion }
23
+ let( :hunt ) { Scorpion::Hunt.new scorpion, String, nil }
24
+
25
+ describe "#fetch" do
26
26
  it "changes context" do
27
27
  expect( scorpion ).to receive( :execute ) do |hunt|
28
28
  expect( hunt.contract ).to eq Regexp
29
29
  end
30
30
 
31
- hunt.fetch_by_traits Regexp, nil
31
+ hunt.fetch Regexp, nil
32
32
  end
33
33
 
34
34
  it "restores context" do
35
35
  expect( scorpion ).to receive( :execute )
36
36
 
37
- hunt.fetch_by_traits Numeric, nil
37
+ hunt.fetch Numeric, nil
38
38
  expect( hunt.contract ).to eq String
39
39
  end
40
-
41
- it "finds matching argument in parent" do
42
- hunt.dependencies[:label] = "Hello"
43
-
44
- expect( hunt.fetch String ).to eq "Hello"
45
- end
46
-
47
- it "finds matching argument in grandparent" do
48
- hunt = Scorpion::Hunt.new scorpion, String, nil, label: "Hello"
49
- hunt.send :push, Regexp, nil, [], {}, nil
50
-
51
- expect( scorpion ).to receive( :execute ) do |hunt|
52
- next if hunt.contract == String
53
-
54
- expect( hunt.fetch String ).to eq "Hello"
55
- end.at_least(:once)
56
-
57
- hunt.fetch Numeric
58
- end
59
40
  end
60
41
 
61
42
  describe "#inject" do
62
- let( :target ) do
43
+ let( :target ) do
63
44
  Test::Hunt::Target.new
64
45
  end
65
46
 
@@ -89,6 +70,13 @@ describe Scorpion::Hunt do
89
70
  expect( target.sailor? ).to be_falsy
90
71
  end
91
72
 
73
+ it "uses the same hunt when lazy fetching" do
74
+ hunt.inject target
75
+
76
+ expect( hunt ).to receive( :fetch ).with( Test::Hunt::Logger )
77
+ target.sailor
78
+ end
79
+
92
80
  it "invokes on_injected" do
93
81
  expect( target ).to receive( :on_injected )
94
82
  hunt.inject target
@@ -96,4 +84,4 @@ describe Scorpion::Hunt do
96
84
 
97
85
  end
98
86
 
99
- end
87
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  module Test
4
4
  module Hunter
@@ -29,9 +29,12 @@ module Test
29
29
 
30
30
  depend_on do
31
31
  city City
32
+ zoo Test::Hunter::Zoo
32
33
  end
33
34
 
34
- initialize( zoo: Test::Hunter::Zoo )
35
+ def initialize( zoo = nil )
36
+ self.zoo = zoo
37
+ end
35
38
  end
36
39
 
37
40
  class City
@@ -39,9 +42,12 @@ module Test
39
42
 
40
43
  depend_on do
41
44
  park Park
45
+ zoo Test::Hunter::Zoo
42
46
  end
43
47
 
44
- initialize( zoo: Test::Hunter::Zoo )
48
+ def initialize( zoo = nil )
49
+ self.zoo = zoo
50
+ end
45
51
  end
46
52
 
47
53
  class Singleton
@@ -54,11 +60,10 @@ describe Scorpion::Hunter do
54
60
 
55
61
  let( :hunter ) do
56
62
  Scorpion::Hunter.new do
57
- capture Test::Hunter::Lion, :tame
63
+ capture Test::Hunter::Lion
58
64
 
59
65
  hunt_for Test::Hunter::Bear
60
- hunt_for Test::Hunter::Lion, :male
61
- hunt_for Test::Hunter::Grizly, :female
66
+ hunt_for Test::Hunter::Grizly
62
67
  hunt_for Test::Hunter::Argumented
63
68
 
64
69
  hunt_for Test::Hunter::Zoo
@@ -70,17 +75,17 @@ describe Scorpion::Hunter do
70
75
  end
71
76
 
72
77
  it "spawns dependency" do
73
- expect( hunter.fetch Test::Hunter::Beast ).to be_a Test::Hunter::Beast
78
+ expect( hunter.fetch(Test::Hunter::Beast) ).to be_a Test::Hunter::Beast
74
79
  end
75
80
 
76
81
  it "spawns a new instance for multiple requests" do
77
82
  first = hunter.fetch Test::Hunter::Beast
78
- expect( hunter.fetch Test::Hunter::Beast ).not_to be first
83
+ expect( hunter.fetch(Test::Hunter::Beast) ).not_to be first
79
84
  end
80
85
 
81
86
  it "spawns the same instance for captured dependency" do
82
- first = hunter.fetch_by_traits Test::Hunter::Beast, :tame
83
- expect( hunter.fetch_by_traits Test::Hunter::Beast, :tame ).to be first
87
+ first = hunter.fetch Test::Hunter::Lion
88
+ expect( hunter.fetch(Test::Hunter::Lion) ).to be first
84
89
  end
85
90
 
86
91
  it "injects nested objects" do
@@ -94,11 +99,7 @@ describe Scorpion::Hunter do
94
99
  end
95
100
 
96
101
  it "implicitly spawns Class contracts" do
97
- expect( hunter.fetch Test::Hunter::Implicit ).to be_a Test::Hunter::Implicit
98
- end
99
-
100
- it "implicitly spawns Class contracts with empty traits" do
101
- expect( hunter.fetch_by_traits Test::Hunter::Implicit, [] ).to be_a Test::Hunter::Implicit
102
+ expect( hunter.fetch(Test::Hunter::Implicit) ).to be_a Test::Hunter::Implicit
102
103
  end
103
104
 
104
105
  it "initialize explicit contracts" do
@@ -109,23 +110,30 @@ describe Scorpion::Hunter do
109
110
 
110
111
  it "delegates hunting definitions" do
111
112
  hunter.hunt_for Test::Hunter::Zoo, return: :nyc
112
- expect( hunter.fetch Test::Hunter::Zoo ).to eq :nyc
113
+ expect( hunter.fetch(Test::Hunter::Zoo) ).to eq :nyc
113
114
  end
114
115
 
115
116
  context "child dependencies" do
116
117
  it "passes initializer args to child dependencies" do
117
- zoo = Test::Hunter::Zoo.new
118
- city = hunter.fetch Test::Hunter::City, zoo: zoo
118
+ zoo = hunter.new Test::Hunter::Zoo
119
+ city = hunter.fetch Test::Hunter::City, zoo
119
120
 
120
121
  expect( city.park.zoo ).to be zoo
121
122
  end
122
123
 
123
124
  it "passes self to child dependencies" do
124
- zoo = Test::Hunter::Zoo.new
125
- city = hunter.fetch Test::Hunter::City, zoo: zoo
125
+ zoo = hunter.new Test::Hunter::Zoo
126
+ city = hunter.fetch Test::Hunter::City, zoo
126
127
 
127
128
  expect( city.park.city ).to be city
128
129
  end
130
+
131
+ it "gets a new instance for the same contract on a different hunt" do
132
+ zoo = hunter.new Test::Hunter::Zoo
133
+ other_zoo = hunter.new Test::Hunter::Zoo
134
+
135
+ expect( zoo ).not_to be other_zoo
136
+ end
129
137
  end
130
138
 
131
139
  describe "#find_dependency" do
@@ -152,4 +160,4 @@ describe Scorpion::Hunter do
152
160
  expect( hunter.inspect ).not_to match /0x/
153
161
  end
154
162
  end
155
- end
163
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  module Test
4
4
  module Object
@@ -10,7 +10,7 @@ module Test
10
10
  class Mamal
11
11
  include Scorpion::Object
12
12
 
13
- def initialize( family, parent = nil, options={}, &block )
13
+ def initialize( family, parent = nil, options = {}, &block )
14
14
  @family = family
15
15
  @parent = parent
16
16
  @options = inject_from! options
@@ -37,8 +37,9 @@ module Test
37
37
  cheese Test::Object::Logger
38
38
  logger Test::Object::BackLogger
39
39
  end
40
- def initialize( options = {} )
41
- super 'mouse', nil, options
40
+
41
+ def initialize( **options )
42
+ super "mouse", nil, options
42
43
  end
43
44
  end
44
45
 
@@ -53,7 +54,7 @@ end
53
54
 
54
55
  describe Scorpion::Object do
55
56
 
56
- let( :scorpion ){ double Scorpion }
57
+ let( :scorpion ) { double Scorpion }
57
58
  let( :hunt ) { double Scorpion::Hunt }
58
59
 
59
60
  before( :each ) do
@@ -74,7 +75,7 @@ describe Scorpion::Object do
74
75
  describe ".spawn" do
75
76
 
76
77
  it "can spawn" do
77
- mamal = Test::Object::Mamal.spawn hunt, 'mouse', 'rodent', name: 'name'
78
+ mamal = Test::Object::Mamal.spawn hunt, "mouse", "rodent", name: "name"
78
79
  expect( mamal ).to be_a Test::Object::Mamal
79
80
  end
80
81
 
@@ -84,14 +85,14 @@ describe Scorpion::Object do
84
85
  end
85
86
 
86
87
  it "can inherit" do
87
- mouse = Test::Object::Mouse.spawn hunt, { name: 'name' }, {}
88
- expect( mouse.family ).to eq 'mouse'
89
- expect( mouse.options ).to include name: 'name'
88
+ mouse = Test::Object::Mouse.spawn hunt, name: "name"
89
+ expect( mouse.family ).to eq "mouse"
90
+ expect( mouse.options ).to include name: "name"
90
91
  end
91
92
 
92
93
  it "yields to constructor" do
93
94
  expect do |b|
94
- Test::Object::Mouse.spawn hunt, name: 'name', &b
95
+ Test::Object::Mouse.spawn hunt, name: "name", &b
95
96
  end.to yield_control
96
97
  end
97
98
 
@@ -99,10 +100,10 @@ describe Scorpion::Object do
99
100
 
100
101
  describe "accessors" do
101
102
  let( :object ) do
102
- Test::Object::Mamal.spawn hunt, 'harry', 'jim', name: 'name', manager: double
103
+ Test::Object::Mamal.spawn hunt, "harry", "jim", name: "name", manager: double
103
104
  end
104
105
 
105
- subject{ object }
106
+ subject { object }
106
107
 
107
108
  it "defines accessors" do
108
109
  expect( object ).to respond_to :user_service
@@ -110,12 +111,12 @@ describe Scorpion::Object do
110
111
  end
111
112
 
112
113
  it "supports private reader" do
113
- expect( object.respond_to? :executive_manager, false ).to be_falsy
114
- expect( object.respond_to? :executive_manager, true ).to be_truthy
114
+ expect( object.respond_to?(:executive_manager, false) ).to be_falsy
115
+ expect( object.respond_to?(:executive_manager, true) ).to be_truthy
115
116
  end
116
117
 
117
118
  it "supports public writer" do
118
- expect( object.respond_to? :manager=, false ).to be_truthy
119
+ expect( object.respond_to?(:manager=, false) ).to be_truthy
119
120
  end
120
121
 
121
122
  describe "#attr_dependency" do
@@ -130,7 +131,7 @@ describe Scorpion::Object do
130
131
  end
131
132
  end
132
133
 
133
- describe "inheritance" do
134
+ describe "inheritance" do
134
135
  let( :object ) do
135
136
  Test::Object::Mouse.spawn hunt
136
137
  end
@@ -144,7 +145,7 @@ describe Scorpion::Object do
144
145
  end
145
146
 
146
147
  it "doesn't effect other classes" do
147
- expect( Test::Object::Bear.spawn( hunt, 'Yogi' ).injected_attributes[:logger].contract ).to be Test::Object::ColorLogger
148
+ expect( Test::Object::Bear.spawn( hunt, "Yogi" ).injected_attributes[:logger].contract ).to be Test::Object::ColorLogger # rubocop:disable Metrics/LineLength
148
149
  end
149
150
 
150
151
  end
@@ -154,7 +155,7 @@ describe Scorpion::Object do
154
155
  describe "feasting" do
155
156
  let( :logger ) { Test::Object::Logger.new }
156
157
  let( :options ) { { manager: logger, color: :red } }
157
- let( :object ) { Test::Object::Mouse.new name: 'mighty' }
158
+ let( :object ) { Test::Object::Mouse.new name: "mighty" }
158
159
 
159
160
 
160
161
  describe "#inject_from" do
@@ -215,4 +216,4 @@ describe Scorpion::Object do
215
216
  end
216
217
  end
217
218
 
218
- end
219
+ end