scorpion-ioc 0.3.1 → 0.4.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.rspec +2 -1
- data/README.md +111 -44
- data/lib/scorpion/attribute.rb +0 -1
- data/lib/scorpion/attribute_set.rb +15 -7
- data/lib/scorpion/dependency/argument_dependency.rb +25 -0
- data/lib/scorpion/{prey/builder_prey.rb → dependency/builder_dependency.rb} +8 -8
- data/lib/scorpion/dependency/captured_dependency.rb +44 -0
- data/lib/scorpion/dependency/class_dependency.rb +25 -0
- data/lib/scorpion/dependency/module_dependency.rb +14 -0
- data/lib/scorpion/dependency.rb +137 -0
- data/lib/scorpion/dependency_map.rb +135 -0
- data/lib/scorpion/hunt.rb +158 -0
- data/lib/scorpion/hunter.rb +21 -20
- data/lib/scorpion/locale/en.yml +5 -1
- data/lib/scorpion/{king.rb → object.rb} +72 -53
- data/lib/scorpion/object_constructor.rb +55 -0
- data/lib/scorpion/rails/active_record/association.rb +65 -0
- data/lib/scorpion/rails/active_record/model.rb +28 -0
- data/lib/scorpion/rails/active_record/relation.rb +66 -0
- data/lib/scorpion/rails/active_record.rb +21 -0
- data/lib/scorpion/rails/controller.rb +22 -62
- data/lib/scorpion/rails/job.rb +30 -0
- data/lib/scorpion/rails/nest.rb +86 -0
- data/lib/scorpion/rails/railtie.rb +16 -0
- data/lib/scorpion/rails.rb +4 -0
- data/lib/scorpion/rspec/helper.rb +25 -0
- data/lib/scorpion/rspec.rb +17 -0
- data/lib/scorpion/stinger.rb +69 -0
- data/lib/scorpion/version.rb +1 -1
- data/lib/scorpion.rb +91 -44
- data/scorpion.gemspec +1 -1
- data/spec/internal/app/models/author.rb +17 -0
- data/spec/internal/app/models/todo.rb +14 -0
- data/spec/internal/db/schema.rb +12 -1
- data/spec/lib/scorpion/dependency/argument_dependency_spec.rb +18 -0
- data/spec/lib/scorpion/dependency/builder_dependency_spec.rb +41 -0
- data/spec/lib/scorpion/dependency/module_dependency_spec.rb +16 -0
- data/spec/lib/scorpion/dependency_map_spec.rb +108 -0
- data/spec/lib/scorpion/dependency_spec.rb +131 -0
- data/spec/lib/scorpion/hunt_spec.rb +93 -0
- data/spec/lib/scorpion/hunter_spec.rb +53 -14
- data/spec/lib/scorpion/object_constructor_spec.rb +49 -0
- data/spec/lib/scorpion/object_spec.rb +214 -0
- data/spec/lib/scorpion/rails/active_record/association_spec.rb +26 -0
- data/spec/lib/scorpion/rails/active_record/model_spec.rb +33 -0
- data/spec/lib/scorpion/rails/active_record/relation_spec.rb +72 -0
- data/spec/lib/scorpion/rails/controller_spec.rb +9 -9
- data/spec/lib/scorpion/rails/job_spec.rb +34 -0
- data/spec/lib/scorpion/rspec/helper_spec.rb +44 -0
- data/spec/lib/scorpion_spec.rb +0 -35
- data/spec/spec_helper.rb +1 -0
- metadata +54 -26
- data/lib/scorpion/hunting_map.rb +0 -139
- data/lib/scorpion/prey/captured_prey.rb +0 -44
- data/lib/scorpion/prey/class_prey.rb +0 -13
- data/lib/scorpion/prey/hunted_prey.rb +0 -14
- data/lib/scorpion/prey/module_prey.rb +0 -14
- data/lib/scorpion/prey.rb +0 -94
- data/spec/internal/db/combustion_test.sqlite +0 -0
- data/spec/lib/scorpion/hunting_map_spec.rb +0 -126
- data/spec/lib/scorpion/instance_spec.rb +0 -5
- data/spec/lib/scorpion/king_spec.rb +0 -198
- data/spec/lib/scorpion/prey/builder_prey_spec.rb +0 -42
- data/spec/lib/scorpion/prey/module_prey_spec.rb +0 -16
- data/spec/lib/scorpion/prey_spec.rb +0 -76
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'scorpion/rspec'
|
3
|
+
load 'scorpion/rspec/helper.rb'
|
4
|
+
|
5
|
+
Scorpion::Rspec.prepare do
|
6
|
+
hunt_for Numeric, return: 42
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Scorpion::Rspec::Helper do
|
10
|
+
include Scorpion::Rspec::Helper
|
11
|
+
|
12
|
+
scorpion do
|
13
|
+
hunt_for String, return: "Shazam!"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "provides a scorpion" do
|
17
|
+
expect( scorpion ).not_to be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is configurable" do
|
21
|
+
expect( scorpion.fetch String ).to eq "Shazam!"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "inherits global config" do
|
25
|
+
expect( scorpion.fetch Numeric ).to eq 42
|
26
|
+
end
|
27
|
+
|
28
|
+
context "child context" do
|
29
|
+
it "inherits" do
|
30
|
+
expect( scorpion.fetch String ).to eq "Shazam!"
|
31
|
+
end
|
32
|
+
|
33
|
+
context "overrides" do
|
34
|
+
|
35
|
+
scorpion do
|
36
|
+
hunt_for String, return: "KaPow"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "overrides" do
|
40
|
+
expect( scorpion.fetch String ).to eq "KaPow"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/lib/scorpion_spec.rb
CHANGED
@@ -1,40 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
|
3
|
-
module Test
|
4
|
-
module Scorpion
|
5
|
-
class Logger; end
|
6
|
-
|
7
|
-
class Target
|
8
|
-
include ::Scorpion::King
|
9
|
-
|
10
|
-
feed_on do
|
11
|
-
logger Logger, public: true
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
2
|
describe Scorpion do
|
18
|
-
let( :scorpion ){ Scorpion::Hunter.new }
|
19
|
-
let( :target ) do
|
20
|
-
Test::Scorpion::Target.new.tap do |target|
|
21
|
-
target.instance_variable_set :@scorpion, scorpion
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "#feed" do
|
26
|
-
it "injects attributes" do
|
27
|
-
scorpion.feed target
|
28
|
-
|
29
|
-
expect( target.logger ).to be_a Test::Scorpion::Logger
|
30
|
-
end
|
31
3
|
|
32
|
-
it "does not overwrite existing attributes" do
|
33
|
-
logger = Test::Scorpion::Logger.new
|
34
|
-
target.logger = logger
|
35
|
-
scorpion.feed target
|
36
4
|
|
37
|
-
expect( target.logger ).to be logger
|
38
|
-
end
|
39
|
-
end
|
40
5
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scorpion-ioc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Alexander
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -120,39 +120,59 @@ files:
|
|
120
120
|
- lib/scorpion.rb
|
121
121
|
- lib/scorpion/attribute.rb
|
122
122
|
- lib/scorpion/attribute_set.rb
|
123
|
+
- lib/scorpion/dependency.rb
|
124
|
+
- lib/scorpion/dependency/argument_dependency.rb
|
125
|
+
- lib/scorpion/dependency/builder_dependency.rb
|
126
|
+
- lib/scorpion/dependency/captured_dependency.rb
|
127
|
+
- lib/scorpion/dependency/class_dependency.rb
|
128
|
+
- lib/scorpion/dependency/module_dependency.rb
|
129
|
+
- lib/scorpion/dependency_map.rb
|
123
130
|
- lib/scorpion/error.rb
|
131
|
+
- lib/scorpion/hunt.rb
|
124
132
|
- lib/scorpion/hunter.rb
|
125
|
-
- lib/scorpion/hunting_map.rb
|
126
|
-
- lib/scorpion/king.rb
|
127
133
|
- lib/scorpion/locale/en.yml
|
128
134
|
- lib/scorpion/nest.rb
|
129
|
-
- lib/scorpion/
|
130
|
-
- lib/scorpion/
|
131
|
-
- lib/scorpion/prey/captured_prey.rb
|
132
|
-
- lib/scorpion/prey/class_prey.rb
|
133
|
-
- lib/scorpion/prey/hunted_prey.rb
|
134
|
-
- lib/scorpion/prey/module_prey.rb
|
135
|
+
- lib/scorpion/object.rb
|
136
|
+
- lib/scorpion/object_constructor.rb
|
135
137
|
- lib/scorpion/rails.rb
|
138
|
+
- lib/scorpion/rails/active_record.rb
|
139
|
+
- lib/scorpion/rails/active_record/association.rb
|
140
|
+
- lib/scorpion/rails/active_record/model.rb
|
141
|
+
- lib/scorpion/rails/active_record/relation.rb
|
136
142
|
- lib/scorpion/rails/controller.rb
|
143
|
+
- lib/scorpion/rails/job.rb
|
144
|
+
- lib/scorpion/rails/nest.rb
|
145
|
+
- lib/scorpion/rails/railtie.rb
|
146
|
+
- lib/scorpion/rspec.rb
|
147
|
+
- lib/scorpion/rspec/helper.rb
|
148
|
+
- lib/scorpion/stinger.rb
|
137
149
|
- lib/scorpion/version.rb
|
138
150
|
- scorpion.gemspec
|
151
|
+
- spec/internal/app/models/author.rb
|
152
|
+
- spec/internal/app/models/todo.rb
|
139
153
|
- spec/internal/config/database.yml
|
140
154
|
- spec/internal/config/routes.rb
|
141
|
-
- spec/internal/db/combustion_test.sqlite
|
142
155
|
- spec/internal/db/schema.rb
|
143
156
|
- spec/internal/log/.gitignore
|
144
157
|
- spec/internal/public/favicon.ico
|
145
158
|
- spec/lib/scorpion/attribute_set_spec.rb
|
146
159
|
- spec/lib/scorpion/attribute_spec.rb
|
160
|
+
- spec/lib/scorpion/dependency/argument_dependency_spec.rb
|
161
|
+
- spec/lib/scorpion/dependency/builder_dependency_spec.rb
|
162
|
+
- spec/lib/scorpion/dependency/module_dependency_spec.rb
|
163
|
+
- spec/lib/scorpion/dependency_map_spec.rb
|
164
|
+
- spec/lib/scorpion/dependency_spec.rb
|
147
165
|
- spec/lib/scorpion/error_spec.rb
|
166
|
+
- spec/lib/scorpion/hunt_spec.rb
|
148
167
|
- spec/lib/scorpion/hunter_spec.rb
|
149
|
-
- spec/lib/scorpion/
|
150
|
-
- spec/lib/scorpion/
|
151
|
-
- spec/lib/scorpion/
|
152
|
-
- spec/lib/scorpion/
|
153
|
-
- spec/lib/scorpion/
|
154
|
-
- spec/lib/scorpion/prey_spec.rb
|
168
|
+
- spec/lib/scorpion/object_constructor_spec.rb
|
169
|
+
- spec/lib/scorpion/object_spec.rb
|
170
|
+
- spec/lib/scorpion/rails/active_record/association_spec.rb
|
171
|
+
- spec/lib/scorpion/rails/active_record/model_spec.rb
|
172
|
+
- spec/lib/scorpion/rails/active_record/relation_spec.rb
|
155
173
|
- spec/lib/scorpion/rails/controller_spec.rb
|
174
|
+
- spec/lib/scorpion/rails/job_spec.rb
|
175
|
+
- spec/lib/scorpion/rspec/helper_spec.rb
|
156
176
|
- spec/lib/scorpion_spec.rb
|
157
177
|
- spec/spec_helper.rb
|
158
178
|
homepage: https://github.com/phallguy/scorpion
|
@@ -167,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
187
|
requirements:
|
168
188
|
- - ">="
|
169
189
|
- !ruby/object:Gem::Version
|
170
|
-
version:
|
190
|
+
version: 2.0.0
|
171
191
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
192
|
requirements:
|
173
193
|
- - ">="
|
@@ -175,28 +195,36 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
195
|
version: '0'
|
176
196
|
requirements: []
|
177
197
|
rubyforge_project:
|
178
|
-
rubygems_version: 2.4.
|
198
|
+
rubygems_version: 2.4.6
|
179
199
|
signing_key:
|
180
200
|
specification_version: 4
|
181
201
|
summary: Add IoC to rails with minimal fuss and ceremony
|
182
202
|
test_files:
|
203
|
+
- spec/internal/app/models/author.rb
|
204
|
+
- spec/internal/app/models/todo.rb
|
183
205
|
- spec/internal/config/database.yml
|
184
206
|
- spec/internal/config/routes.rb
|
185
|
-
- spec/internal/db/combustion_test.sqlite
|
186
207
|
- spec/internal/db/schema.rb
|
187
208
|
- spec/internal/log/.gitignore
|
188
209
|
- spec/internal/public/favicon.ico
|
189
210
|
- spec/lib/scorpion/attribute_set_spec.rb
|
190
211
|
- spec/lib/scorpion/attribute_spec.rb
|
212
|
+
- spec/lib/scorpion/dependency/argument_dependency_spec.rb
|
213
|
+
- spec/lib/scorpion/dependency/builder_dependency_spec.rb
|
214
|
+
- spec/lib/scorpion/dependency/module_dependency_spec.rb
|
215
|
+
- spec/lib/scorpion/dependency_map_spec.rb
|
216
|
+
- spec/lib/scorpion/dependency_spec.rb
|
191
217
|
- spec/lib/scorpion/error_spec.rb
|
218
|
+
- spec/lib/scorpion/hunt_spec.rb
|
192
219
|
- spec/lib/scorpion/hunter_spec.rb
|
193
|
-
- spec/lib/scorpion/
|
194
|
-
- spec/lib/scorpion/
|
195
|
-
- spec/lib/scorpion/
|
196
|
-
- spec/lib/scorpion/
|
197
|
-
- spec/lib/scorpion/
|
198
|
-
- spec/lib/scorpion/prey_spec.rb
|
220
|
+
- spec/lib/scorpion/object_constructor_spec.rb
|
221
|
+
- spec/lib/scorpion/object_spec.rb
|
222
|
+
- spec/lib/scorpion/rails/active_record/association_spec.rb
|
223
|
+
- spec/lib/scorpion/rails/active_record/model_spec.rb
|
224
|
+
- spec/lib/scorpion/rails/active_record/relation_spec.rb
|
199
225
|
- spec/lib/scorpion/rails/controller_spec.rb
|
226
|
+
- spec/lib/scorpion/rails/job_spec.rb
|
227
|
+
- spec/lib/scorpion/rspec/helper_spec.rb
|
200
228
|
- spec/lib/scorpion_spec.rb
|
201
229
|
- spec/spec_helper.rb
|
202
230
|
has_rdoc:
|
data/lib/scorpion/hunting_map.rb
DELETED
@@ -1,139 +0,0 @@
|
|
1
|
-
module Scorpion
|
2
|
-
# {#chart} available {Prey} and {#find} them based on desired
|
3
|
-
# {#{Scorpion::Attribute attributes}.
|
4
|
-
class HuntingMap
|
5
|
-
include Enumerable
|
6
|
-
extend Forwardable
|
7
|
-
|
8
|
-
# ============================================================================
|
9
|
-
# @!group Attributes
|
10
|
-
#
|
11
|
-
|
12
|
-
# @return [Scorpion] the scorpion that created the map.
|
13
|
-
attr_reader :scorpion
|
14
|
-
|
15
|
-
# @return [Set] the set of prey charted on this map.
|
16
|
-
attr_reader :prey_set
|
17
|
-
private :prey_set
|
18
|
-
|
19
|
-
# @return [Set] the set of prey charted on this map that is shared with all
|
20
|
-
# child prey.
|
21
|
-
attr_reader :shared_prey_set
|
22
|
-
private :shared_prey_set
|
23
|
-
|
24
|
-
|
25
|
-
# @return [Set] the active prey set either {#prey_set} or {#shared_prey_set}
|
26
|
-
attr_reader :active_prey_set
|
27
|
-
private :active_prey_set
|
28
|
-
|
29
|
-
#
|
30
|
-
# @!endgroup Attributes
|
31
|
-
|
32
|
-
def initialize( scorpion )
|
33
|
-
@scorpion = scorpion
|
34
|
-
@prey_set = @active_prey_set = []
|
35
|
-
@shared_prey_set = []
|
36
|
-
end
|
37
|
-
|
38
|
-
# Find {Prey} that matches the requested `contract` and `traits`.
|
39
|
-
# @param [Class,Module,Symbol] contract describing the desired behavior of the prey.
|
40
|
-
# @param [Array<Symbol>] traits found on the {Prey}.
|
41
|
-
# @return [Prey] the prey matching the attribute.
|
42
|
-
def find( contract, traits = nil )
|
43
|
-
prey_set.find{ |p| p.satisfies?( contract, traits ) } ||
|
44
|
-
shared_prey_set.find{ |p| p.satisfies?( contract, traits ) }
|
45
|
-
end
|
46
|
-
|
47
|
-
# Chart the {Prey} that this hunting map can {#find}.
|
48
|
-
def chart( &block )
|
49
|
-
return unless block_given?
|
50
|
-
|
51
|
-
if block.arity == 1
|
52
|
-
yield self
|
53
|
-
else
|
54
|
-
instance_eval &block
|
55
|
-
end
|
56
|
-
|
57
|
-
self
|
58
|
-
end
|
59
|
-
|
60
|
-
# Define {Prey} that can be found on this map by `contract` and `traits`.
|
61
|
-
#
|
62
|
-
# If a block is given, it will be used build the actual instances of the
|
63
|
-
# prey for the {Scorpion}.
|
64
|
-
#
|
65
|
-
# @param [Class,Module,Symbol] contract describing the desired behavior of the prey.
|
66
|
-
# @param [Array<Symbol>] traits found on the {Prey}.
|
67
|
-
# @return [Scorpion::Prey] the prey to be hunted for.
|
68
|
-
def hunt_for( contract, traits = nil, &builder )
|
69
|
-
active_prey_set.unshift define_prey( contract, traits, &builder )
|
70
|
-
end
|
71
|
-
alias_method :offer, :hunt_for
|
72
|
-
|
73
|
-
# Captures a single prey and returns the same instance fore each request
|
74
|
-
# for the resource.
|
75
|
-
# @see #hunt_for
|
76
|
-
def capture( contract, traits = nil, &builder )
|
77
|
-
active_prey_set.unshift Scorpion::Prey::CapturedPrey.new( define_prey( contract, traits, &builder ) )
|
78
|
-
end
|
79
|
-
alias_method :singleton, :capture
|
80
|
-
|
81
|
-
# Share captured prey defined within the block with all child scorpions.
|
82
|
-
def share( &block )
|
83
|
-
old_set = active_prey_set
|
84
|
-
@active_prey_set = shared_prey_set
|
85
|
-
yield
|
86
|
-
ensure
|
87
|
-
@active_prey_set = old_set
|
88
|
-
end
|
89
|
-
|
90
|
-
def each( &block )
|
91
|
-
prey_set.each &block
|
92
|
-
end
|
93
|
-
delegate [ :empty?, :blank?, :present? ] => :prey_set
|
94
|
-
|
95
|
-
# Replicates the prey in `other_map` into this map.
|
96
|
-
# @param [Scorpion::HuntingMap] other_map to replicate from.
|
97
|
-
def replicate_from( other_map )
|
98
|
-
other_map.each do |prey|
|
99
|
-
if replica = prey.replicate
|
100
|
-
prey_set << replica
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
|
107
|
-
def define_prey( contract, traits, &builder )
|
108
|
-
options, traits = extract_options!( traits )
|
109
|
-
|
110
|
-
if with = options[:with]
|
111
|
-
Scorpion::Prey::BuilderPrey.new( contract, traits, with )
|
112
|
-
elsif block_given?
|
113
|
-
Scorpion::Prey::BuilderPrey.new( contract, traits, builder)
|
114
|
-
else
|
115
|
-
prey_class( contract ).new( contract, traits, &builder )
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def extract_options!( traits )
|
120
|
-
case traits
|
121
|
-
when Hash then return [ traits, nil ]
|
122
|
-
when Array then
|
123
|
-
if traits.last.is_a? Hash
|
124
|
-
return [ traits.pop, traits ]
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
[ {}, traits]
|
129
|
-
end
|
130
|
-
|
131
|
-
def prey_class( contract, &builder )
|
132
|
-
return Scorpion::Prey::HuntedPrey if contract.respond_to? :hunt
|
133
|
-
return Scorpion::Prey::ClassPrey if contract.is_a? Class
|
134
|
-
return Scorpion::Prey::ClassPrey if contract.is_a? Module
|
135
|
-
|
136
|
-
raise Scorpion::BuilderRequiredError
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'scorpion/prey'
|
2
|
-
|
3
|
-
module Scorpion
|
4
|
-
class Prey
|
5
|
-
class CapturedPrey < Scorpion::Prey
|
6
|
-
extend Forwardable
|
7
|
-
|
8
|
-
# ============================================================================
|
9
|
-
# @!group Attributes
|
10
|
-
#
|
11
|
-
|
12
|
-
# @!attribute
|
13
|
-
# @return [Object] the instance that was captured.
|
14
|
-
attr_reader :instance
|
15
|
-
|
16
|
-
# @!attribute
|
17
|
-
# @return [Scorpion::Prey] the actual prey to hunt. Used to fetch the
|
18
|
-
# single {#instance}.
|
19
|
-
attr_reader :specific_prey
|
20
|
-
private :specific_prey
|
21
|
-
|
22
|
-
|
23
|
-
delegate [:contract,:traits,:satisfies?] => :specific_prey
|
24
|
-
|
25
|
-
#
|
26
|
-
# @!endgroup Attributes
|
27
|
-
|
28
|
-
def initialize( specific_prey )
|
29
|
-
@specific_prey = specific_prey
|
30
|
-
end
|
31
|
-
|
32
|
-
# @see Prey#fetch
|
33
|
-
def fetch( scorpion, *args, &block )
|
34
|
-
@instance ||= specific_prey.fetch( scorpion, *args, &block )
|
35
|
-
end
|
36
|
-
|
37
|
-
# @see Prey#release
|
38
|
-
def release
|
39
|
-
@instance = nil
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'scorpion/prey'
|
2
|
-
|
3
|
-
module Scorpion
|
4
|
-
class Prey
|
5
|
-
# {Prey} for a contract that implements #hunt
|
6
|
-
class HuntedPrey < Scorpion::Prey
|
7
|
-
|
8
|
-
# @see Scorpion::Prey#fetch
|
9
|
-
def fetch( scorpion, *args, &block )
|
10
|
-
contract.hunt( scorpion, *args, &block )
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
data/lib/scorpion/prey.rb
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
module Scorpion
|
2
|
-
# Prey that can be fed to a {Scorpion::King} by a {Scorpion}.
|
3
|
-
class Prey
|
4
|
-
|
5
|
-
require 'scorpion/prey/captured_prey'
|
6
|
-
require 'scorpion/prey/class_prey'
|
7
|
-
require 'scorpion/prey/module_prey'
|
8
|
-
require 'scorpion/prey/builder_prey'
|
9
|
-
require 'scorpion/prey/hunted_prey'
|
10
|
-
|
11
|
-
# ============================================================================
|
12
|
-
# @!group Attributes
|
13
|
-
#
|
14
|
-
|
15
|
-
# @!attribute
|
16
|
-
# @return [Class,Module,Symbol] contract describing the desired behavior of the prey.
|
17
|
-
attr_reader :contract
|
18
|
-
|
19
|
-
# @!attribute
|
20
|
-
# @return [Array<Symbol>] the traits available on the prey.
|
21
|
-
attr_reader :traits
|
22
|
-
|
23
|
-
#
|
24
|
-
# @!endgroup Attributes
|
25
|
-
|
26
|
-
def initialize( contract, traits = nil )
|
27
|
-
@contract = contract
|
28
|
-
@traits = Set.new( Array( traits ) )
|
29
|
-
end
|
30
|
-
|
31
|
-
# @return [Boolean] if the prey satisfies the required contract and traits.
|
32
|
-
def satisfies?( contract, traits = nil )
|
33
|
-
satisfies_contract?( contract ) && satisfies_traits?( traits )
|
34
|
-
end
|
35
|
-
|
36
|
-
# Fetch an instance of the prey.
|
37
|
-
# @param [Scorpion] scorpion hunting for the prey.
|
38
|
-
# @param [Array<Object>] arguments to the consructor of the prey.
|
39
|
-
# @param [#call] block to pass to constructor.
|
40
|
-
# @return [Object] the hunted prey.
|
41
|
-
def fetch( scorpion, *args, &block )
|
42
|
-
fail "Not Implemented"
|
43
|
-
end
|
44
|
-
|
45
|
-
# Release the prey, freeing up any long held resources.
|
46
|
-
def release
|
47
|
-
end
|
48
|
-
|
49
|
-
# Replicate the Prey.
|
50
|
-
# @return [Prey] a replication of the prey.
|
51
|
-
def replicate
|
52
|
-
dup
|
53
|
-
end
|
54
|
-
|
55
|
-
def ==( other )
|
56
|
-
return unless other
|
57
|
-
self.class == other.class &&
|
58
|
-
contract == other.contract &&
|
59
|
-
traits == other.traits
|
60
|
-
end
|
61
|
-
alias_method :eql?, :==
|
62
|
-
|
63
|
-
def hash
|
64
|
-
self.class.hash ^
|
65
|
-
contract.hash ^
|
66
|
-
traits.hash
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
# @return [Boolean] true if the pray satisfies the given contract.
|
72
|
-
def satisfies_contract?( contract )
|
73
|
-
if self.contract.is_a? Symbol
|
74
|
-
self.contract == contract
|
75
|
-
else
|
76
|
-
self.contract <= contract
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# @return [Boolean] true if the pray satisfies the given contract.
|
81
|
-
def satisfies_traits?( traits )
|
82
|
-
return true if traits.blank?
|
83
|
-
|
84
|
-
Array( traits ).all? do |trait|
|
85
|
-
case trait
|
86
|
-
when Symbol then self.traits.include? trait
|
87
|
-
when Module then self.contract <= trait
|
88
|
-
else fail ArgumentError, "Unsupported trait"
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
end
|
Binary file
|
@@ -1,126 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Test
|
4
|
-
module HuntingMap
|
5
|
-
class Weapon; end
|
6
|
-
class Armor; end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
describe Scorpion::HuntingMap do
|
11
|
-
let( :scorpion ){ double Scorpion }
|
12
|
-
let( :map ){ Scorpion::HuntingMap.new scorpion }
|
13
|
-
|
14
|
-
describe "#chart" do
|
15
|
-
it "yields itself when arg expected" do
|
16
|
-
map.chart do |itself|
|
17
|
-
expect( map ).to be itself
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#find" do
|
23
|
-
it "returns a match" do
|
24
|
-
map.chart do
|
25
|
-
hunt_for Test::HuntingMap::Weapon
|
26
|
-
hunt_for Test::HuntingMap::Armor
|
27
|
-
end
|
28
|
-
|
29
|
-
expect( map.find( Test::HuntingMap::Armor ).contract ).to eq Test::HuntingMap::Armor
|
30
|
-
end
|
31
|
-
|
32
|
-
it "returns nil when no match" do
|
33
|
-
map.chart do
|
34
|
-
hunt_for Test::HuntingMap::Weapon
|
35
|
-
end
|
36
|
-
|
37
|
-
expect( map.find( Test::HuntingMap::Armor ) ).to be_nil
|
38
|
-
end
|
39
|
-
|
40
|
-
it "returns nil when no match" do
|
41
|
-
map.chart do
|
42
|
-
hunt_for Test::HuntingMap::Weapon
|
43
|
-
end
|
44
|
-
|
45
|
-
expect( map.find( Test::HuntingMap::Armor ) ).to be_nil
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
context "multiple possible matches" do
|
50
|
-
before( :each ) do
|
51
|
-
map.chart do
|
52
|
-
hunt_for Test::HuntingMap::Weapon, [ :sharp, :one_handed ]
|
53
|
-
hunt_for Test::HuntingMap::Weapon, [ :blunt, :one_handed ]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it "returns the last prey that matches one trait" do
|
58
|
-
expect( map.find( Test::HuntingMap::Weapon, :one_handed ).traits ).to include :blunt
|
59
|
-
end
|
60
|
-
|
61
|
-
it "returns the last prey that matches all of the traits" do
|
62
|
-
expect( map.find( Test::HuntingMap::Weapon, [ :one_handed, :blunt ] ).traits ).to include :blunt
|
63
|
-
end
|
64
|
-
|
65
|
-
it "returns the last prey that matches a unique trait" do
|
66
|
-
expect( map.find( Test::HuntingMap::Weapon, :blunt ).traits ).to include :blunt
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe "#hunt_for" do
|
72
|
-
it "adds a Prey" do
|
73
|
-
expect( map ).to be_empty
|
74
|
-
|
75
|
-
map.chart do
|
76
|
-
hunt_for Test::HuntingMap::Weapon
|
77
|
-
end
|
78
|
-
|
79
|
-
expect( map.first ).to be_a Scorpion::Prey
|
80
|
-
end
|
81
|
-
|
82
|
-
it "adds a BuilderPrey for block hunts" do
|
83
|
-
map.chart do
|
84
|
-
hunt_for String do
|
85
|
-
"YASS"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
expect( map.first ).to be_a Scorpion::Prey::BuilderPrey
|
90
|
-
end
|
91
|
-
|
92
|
-
it "adds a BuilderPrey for with: option" do
|
93
|
-
map.chart do
|
94
|
-
hunt_for String, with: ->(scorpion,*args,&block){ "YASSS" }
|
95
|
-
end
|
96
|
-
|
97
|
-
expect( map.first ).to be_a Scorpion::Prey::BuilderPrey
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe "#replicate_from" do
|
102
|
-
it "does not dup shared prey" do
|
103
|
-
map.chart do
|
104
|
-
share do
|
105
|
-
capture Test::HuntingMap::Weapon
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
replica = Scorpion::HuntingMap.new scorpion
|
110
|
-
replica.replicate_from( map )
|
111
|
-
|
112
|
-
expect( replica ).to be_empty
|
113
|
-
end
|
114
|
-
|
115
|
-
it "dups captured prey" do
|
116
|
-
map.chart do
|
117
|
-
capture Test::HuntingMap::Weapon
|
118
|
-
end
|
119
|
-
|
120
|
-
replica = Scorpion::HuntingMap.new scorpion
|
121
|
-
replica.replicate_from( map )
|
122
|
-
|
123
|
-
expect( replica ).not_to be_empty
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|