scorpion-ioc 0.0.1 → 0.1.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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +14 -10
  3. data/README.md +414 -26
  4. data/circle.yml +5 -0
  5. data/config.ru +7 -0
  6. data/lib/scorpion.rb +87 -0
  7. data/lib/scorpion/attribute.rb +71 -0
  8. data/lib/scorpion/attribute_set.rb +96 -0
  9. data/lib/scorpion/error.rb +29 -0
  10. data/lib/scorpion/hunter.rb +54 -0
  11. data/lib/scorpion/hunting_map.rb +115 -0
  12. data/lib/scorpion/king.rb +150 -0
  13. data/lib/scorpion/locale/en.yml +5 -0
  14. data/lib/scorpion/nest.rb +37 -0
  15. data/lib/scorpion/prey.rb +94 -0
  16. data/lib/scorpion/prey/builder_prey.rb +32 -0
  17. data/lib/scorpion/prey/captured_prey.rb +44 -0
  18. data/lib/scorpion/prey/class_prey.rb +13 -0
  19. data/lib/scorpion/prey/hunted_prey.rb +14 -0
  20. data/lib/scorpion/prey/module_prey.rb +14 -0
  21. data/lib/scorpion/rails.rb +1 -1
  22. data/lib/scorpion/rails/controller.rb +89 -0
  23. data/lib/scorpion/version.rb +1 -1
  24. data/scorpion.gemspec +2 -0
  25. data/spec/internal/config/database.yml +3 -0
  26. data/spec/internal/config/routes.rb +3 -0
  27. data/spec/internal/db/combustion_test.sqlite +0 -0
  28. data/spec/internal/db/schema.rb +3 -0
  29. data/spec/internal/log/.gitignore +1 -0
  30. data/spec/internal/public/favicon.ico +0 -0
  31. data/spec/lib/scorpion/attribute_set_spec.rb +89 -0
  32. data/spec/lib/scorpion/attribute_spec.rb +8 -0
  33. data/spec/lib/scorpion/error_spec.rb +8 -0
  34. data/spec/lib/scorpion/hunter_spec.rb +69 -0
  35. data/spec/lib/scorpion/hunting_map_spec.rb +118 -0
  36. data/spec/lib/scorpion/{scorpion.rb → instance_spec.rb} +0 -0
  37. data/spec/lib/scorpion/king_spec.rb +129 -0
  38. data/spec/lib/scorpion/prey/module_prey_spec.rb +16 -0
  39. data/spec/lib/scorpion/prey_spec.rb +76 -0
  40. data/spec/lib/scorpion/rails/controller_spec.rb +111 -0
  41. data/spec/lib/scorpion_spec.rb +0 -1
  42. data/spec/spec_helper.rb +6 -0
  43. metadata +78 -6
  44. data/Procfile +0 -1
  45. data/lib/scorpion/rails/railtie.rb +0 -15
@@ -1,5 +1,5 @@
1
1
  module Scorpion
2
- VERSION_NUMBER = "0.0.1"
2
+ VERSION_NUMBER = "0.1.0"
3
3
  VERSION_SUFFIX = ""
4
4
  VERSION = "#{VERSION_NUMBER}#{VERSION_SUFFIX}"
5
5
  end
data/scorpion.gemspec CHANGED
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.6"
25
25
  spec.add_development_dependency "rake", '~> 10'
26
26
  spec.add_development_dependency "rspec", '~> 3.00'
27
+ spec.add_development_dependency "rspec-rails", '~> 3.00'
28
+ spec.add_development_dependency 'combustion', '~> 0.5.3'
27
29
  end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ #
3
+ end
@@ -0,0 +1,3 @@
1
+ ActiveRecord::Schema.define do
2
+ #
3
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scorpion::AttributeSet do
4
+ let( :set ){ Scorpion::AttributeSet.new }
5
+
6
+ it "yields attributes" do
7
+ set.define do
8
+ apples 'apples'
9
+ end
10
+ expect( set.first ).to be_a Scorpion::Attribute
11
+ end
12
+
13
+ describe "#define" do
14
+ it "yields itself when arg expected" do
15
+ set.define do |itself|
16
+ expect( set ).to be itself
17
+ end
18
+ end
19
+
20
+ it "yields self as context when no arg expeted" do
21
+ expect( set ).to receive( :define_attribute )
22
+ set.define do
23
+ whatever Scorpion
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "define_attribute" do
29
+ it "adds an expected attribute" do
30
+ set.define do
31
+ logger nil
32
+ end
33
+
34
+ expect( set[:logger] ).not_to be_lazy
35
+ end
36
+
37
+ it "adds an allowed attribute" do
38
+ set.define do
39
+ logger nil, lazy: true
40
+ end
41
+
42
+ expect( set[:logger] ).to be_lazy
43
+ end
44
+
45
+ it "parses traits" do
46
+ set.define do
47
+ with_traits nil, :color
48
+ end
49
+
50
+ expect( set[:with_traits].traits ).to eq [:color]
51
+ end
52
+
53
+ it "parses traits and options" do
54
+ set.define do
55
+ both nil, :formatted, lazy: true
56
+ end
57
+
58
+ expect( set[:both] ).to be_formatted
59
+ expect( set[:both] ).to be_lazy
60
+ end
61
+
62
+
63
+ it "parses options" do
64
+ set.define do
65
+ options nil, lazy: true
66
+ end
67
+
68
+ expect( set[:options] ).to be_lazy
69
+ end
70
+ end
71
+
72
+ describe "#merge" do
73
+ it "merges two sets" do
74
+ a = Scorpion::AttributeSet.new.define do
75
+ alpha nil
76
+ end
77
+
78
+ b = Scorpion::AttributeSet.new.define do
79
+ beta nil
80
+ end
81
+
82
+ c = a | b
83
+
84
+ expect( c ).to be_key :alpha
85
+ expect( c ).to be_key :beta
86
+ end
87
+ end
88
+
89
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scorpion::Attribute do
4
+ it "responds to trait? methods" do
5
+ attr = Scorpion::Attribute.new :name, :contract, :formatted
6
+ expect( attr ).to be_formatted
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scorpion::UnsuccessfulHunt do
4
+ it "formats the default message" do
5
+ expect( Scorpion::UnsuccessfulHunt.new( :attr ).message ).to match /builder/
6
+ end
7
+
8
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ module Test
4
+ module Hunter
5
+ class Beast; end
6
+ class Bear < Beast; end
7
+ class Lion < Beast; end
8
+ class Grizly < Bear; end
9
+ class Argumented
10
+ def initialize( arg )
11
+ @arg = arg
12
+ end
13
+ attr_reader :arg
14
+ end
15
+ class Implicit; end
16
+
17
+ class Zoo
18
+ include Scorpion::King
19
+
20
+ feed_on do
21
+ bear Bear
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ describe Scorpion::Hunter do
28
+
29
+ let( :hunter ) do
30
+ Scorpion::Hunter.new do
31
+ hunt_for Test::Hunter::Bear
32
+ hunt_for Test::Hunter::Lion, :male
33
+ hunt_for Test::Hunter::Grizly, :female
34
+ hunt_for Test::Hunter::Argumented
35
+
36
+ capture Test::Hunter::Lion, :tame
37
+ hunt_for Test::Hunter::Zoo
38
+ end
39
+ end
40
+
41
+ it "spawns prey" do
42
+ expect( hunter.hunt! Test::Hunter::Beast ).to be_a Test::Hunter::Bear
43
+ end
44
+
45
+ it "spawns a new instance for multiple requests" do
46
+ first = hunter.hunt! Test::Hunter::Beast
47
+ expect( hunter.hunt! Test::Hunter::Beast ).not_to eq first
48
+ end
49
+
50
+ it "spawns the same instance for captured prey" do
51
+ first = hunter.hunt_by_traits! Test::Hunter::Beast, :tame
52
+ expect( hunter.hunt_by_traits! Test::Hunter::Beast, :tame ).to be first
53
+ end
54
+
55
+ it "injects nested kings" do
56
+ zoo = hunter.hunt! Test::Hunter::Zoo
57
+ expect( zoo.bear ).to be_a Test::Hunter::Bear
58
+ end
59
+
60
+ it "accepts arguments that are passed to constructor" do
61
+ obj = hunter.hunt! Test::Hunter::Argumented, :awesome
62
+ expect( obj.arg ).to eq :awesome
63
+ end
64
+
65
+ it "implicitly spawns Class contracts" do
66
+ expect( hunter.hunt! Test::Hunter::Implicit ).to be_a Test::Hunter::Implicit
67
+ end
68
+
69
+ end
@@ -0,0 +1,118 @@
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 first prey that matches one trait" do
58
+ expect( map.find( Test::HuntingMap::Weapon, :one_handed ).traits ).to include :sharp
59
+ end
60
+
61
+ it "returns the first 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 first 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
+ end
92
+
93
+ describe "#replicate_from" do
94
+ it "does not dup shared prey" do
95
+ map.chart do
96
+ share do
97
+ capture Test::HuntingMap::Weapon
98
+ end
99
+ end
100
+
101
+ replica = Scorpion::HuntingMap.new scorpion
102
+ replica.replicate_from( map )
103
+
104
+ expect( replica ).to be_empty
105
+ end
106
+
107
+ it "dups captured prey" do
108
+ map.chart do
109
+ capture Test::HuntingMap::Weapon
110
+ end
111
+
112
+ replica = Scorpion::HuntingMap.new scorpion
113
+ replica.replicate_from( map )
114
+
115
+ expect( replica ).not_to be_empty
116
+ end
117
+ end
118
+ end
File without changes
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ module Test
4
+ module King
5
+ class UserService; end
6
+ class Logger; end
7
+ class BackLogger; end
8
+ class ColorLogger; end
9
+
10
+ class Mamal
11
+ include Scorpion::King
12
+
13
+ def initialize( family, parent = nil, options={}, &block )
14
+ @family = family
15
+ @parent = parent
16
+ @options = options
17
+
18
+ yield if block_given?
19
+ end
20
+
21
+ feed_on do
22
+ user_service Test::King::UserService
23
+ logger Test::King::Logger
24
+ manager Test::King::Logger, public: true
25
+ executive_manager Test::King::Logger, private: true
26
+ end
27
+
28
+ attr_accessor :family
29
+ attr_accessor :parent
30
+ attr_accessor :options
31
+ end
32
+
33
+ class Mouse < Mamal
34
+
35
+ feed_on do
36
+ cheese Test::King::Logger
37
+ logger Test::King::BackLogger
38
+ end
39
+ def initialize( options = {} )
40
+ super 'mouse', nil, options
41
+ end
42
+ end
43
+
44
+ class Bear < Mamal
45
+ feed_on do
46
+ logger Test::King::ColorLogger
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+
53
+ describe Scorpion::King do
54
+
55
+ let( :scorpion ){ double Scorpion }
56
+
57
+ before( :each ) do
58
+ allow( scorpion ).to receive( :feed! )
59
+ end
60
+
61
+ describe ".spawn" do
62
+
63
+ it "can spawn" do
64
+ mamal = Test::King::Mamal.spawn scorpion, 'mouse', 'rodent', name: 'name'
65
+ expect( mamal ).to be_a Test::King::Mamal
66
+ end
67
+
68
+ it "can inherit" do
69
+ mouse = Test::King::Mouse.spawn scorpion, name: 'name'
70
+ expect( mouse.family ).to eq 'mouse'
71
+ expect( mouse.options ).to include name: 'name'
72
+ end
73
+
74
+ it "yields to constructor" do
75
+ expect do |b|
76
+ Test::King::Mouse.spawn scorpion, name: 'name', &b
77
+ end.to yield_control
78
+ end
79
+
80
+ it "invokes on_fed" do
81
+ expect_any_instance_of( Test::King::Mouse ).to receive( :on_fed )
82
+ Test::King::Mouse.spawn scorpion
83
+ end
84
+ end
85
+
86
+ describe "accessors" do
87
+ let( :king ) do
88
+ Test::King::Mamal.spawn scorpion, 'harry', 'jim', name: 'name', manager: double
89
+ end
90
+
91
+ subject{ king }
92
+
93
+ it "defines accessors" do
94
+ expect( king ).to respond_to :user_service
95
+ expect( king ).not_to respond_to :user_service=
96
+ end
97
+
98
+ it "supports private reader" do
99
+ expect( king.respond_to? :executive_manager, false ).to be_falsy
100
+ expect( king.respond_to? :executive_manager, true ).to be_truthy
101
+ end
102
+
103
+ it "supports public writer" do
104
+ expect( king.respond_to? :manager=, false ).to be_truthy
105
+ end
106
+
107
+ describe "inheritance" do
108
+ let( :king ) do
109
+ Test::King::Mouse.spawn scorpion
110
+ end
111
+
112
+ it "inherits attributes" do
113
+ expect( king.injected_attributes[:user_service].contract ).to be Test::King::UserService
114
+ end
115
+
116
+ it "overrides attributes" do
117
+ expect( king.injected_attributes[:logger].contract ).to be Test::King::BackLogger
118
+ end
119
+
120
+ it "doesn't effect other classes" do
121
+ expect( Test::King::Bear.spawn( scorpion, 'Yogi' ).injected_attributes[:logger].contract ).to be Test::King::ColorLogger
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
128
+
129
+ end