stack_commander 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +5 -1
- data/README.md +2 -2
- data/Rakefile +7 -0
- data/lib/stack_commander/stack.rb +14 -9
- data/lib/stack_commander/version.rb +1 -1
- data/spec/stack_spec.rb +22 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88727f317aeb1c15a2cf5125608b3307f006453d
|
4
|
+
data.tar.gz: d1112eadfc48d7f147da59051aa503bc82622e2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 838fbcc1b518d5c11d54be5bb408a5376c8baefac8c048d1089d901e18d4da3f892d17e89638e4d8b2c5b22935e8c1f59b132a2543661e97093a7723027f397e
|
7
|
+
data.tar.gz: 406882fa923046d3018e9a1119180e9e5e424157f05ad476ac1fd7b39fe0b7da9bc66172d26e8bfd5807363ef7419fc06049d79fc06523630285dcaee027390f
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# StackCommander
|
1
|
+
# StackCommander [![Build Status](https://travis-ci.org/mikz/stack_commander.svg?branch=master)](https://travis-ci.org/mikz/stack_commander)
|
2
2
|
|
3
3
|
StackCommander takes fresh approach to Command Pattern in Ruby.
|
4
4
|
We needed chaining of Commands with benefits of `rescue` and `ensure` which was hard to do without execution stack.
|
@@ -15,7 +15,7 @@ We tried: passing state to the `action` method, passing state to the `initialize
|
|
15
15
|
* `initialize` is called by the stack with explicit parameters instead of whole state object
|
16
16
|
* for easier testing
|
17
17
|
* and not to break Law of Demeter
|
18
|
-
* command's `action`, `
|
18
|
+
* command's `action`, `recover` and `insurance` are called without state parameter
|
19
19
|
* command has internal variable `@scope`
|
20
20
|
|
21
21
|
It is not final, so constructive feedback is very much appreciated.
|
data/Rakefile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require '
|
1
|
+
require 'stack_commander'
|
2
2
|
|
3
3
|
module StackCommander
|
4
4
|
class Stack
|
5
5
|
def initialize(scope)
|
6
|
-
@queue =
|
6
|
+
@queue = []
|
7
7
|
@scope = scope
|
8
8
|
end
|
9
9
|
|
@@ -18,21 +18,26 @@ module StackCommander
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def call
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
run_command(command_class)
|
21
|
+
if command_class = @queue.shift
|
22
|
+
run_command(command_class)
|
23
|
+
end
|
25
24
|
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
def run_command(klass)
|
26
|
+
# TODO: maybe this should be responsibility of the command? it would make testing easier
|
27
|
+
def initialize_command(klass)
|
30
28
|
parameters = StackCommander::DependencyInjection.new(klass).extract(@scope)
|
31
29
|
|
32
30
|
command = klass.allocate
|
33
31
|
command.instance_variable_set :@scope, @scope
|
34
32
|
command.__send__(:initialize, *parameters)
|
35
33
|
|
34
|
+
command
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def run_command(klass)
|
40
|
+
command = initialize_command(klass)
|
36
41
|
command.call(self)
|
37
42
|
end
|
38
43
|
end
|
data/spec/stack_spec.rb
CHANGED
@@ -23,20 +23,39 @@ describe StackCommander::Stack do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'with commands' do
|
26
|
-
let(:command) {
|
26
|
+
let(:command) { Class.new(StackCommander::BaseCommand) }
|
27
27
|
|
28
28
|
before do
|
29
29
|
stack << command
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'runs the commands' do
|
33
|
-
|
33
|
+
expect_any_instance_of(command).to receive(:call).with(stack).and_call_original
|
34
|
+
expect_any_instance_of(command).to receive(:action)
|
35
|
+
|
34
36
|
stack.call
|
35
37
|
end
|
36
38
|
|
37
39
|
it 'initializes the command' do
|
38
|
-
|
40
|
+
# TODO: this could be responsibility of the command
|
41
|
+
expect(stack).to receive(:initialize_command).with(command).and_call_original
|
39
42
|
stack.call
|
40
43
|
end
|
44
|
+
|
45
|
+
context 'stacked' do
|
46
|
+
let(:other_command) { Class.new(StackCommander::BaseCommand) }
|
47
|
+
|
48
|
+
before do
|
49
|
+
stack << other_command
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'calls action in stack manner' do
|
53
|
+
expect_any_instance_of(command).to receive(:action) do
|
54
|
+
expect_any_instance_of(other_command).to receive(:action)
|
55
|
+
end
|
56
|
+
|
57
|
+
stack.call
|
58
|
+
end
|
59
|
+
end
|
41
60
|
end
|
42
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stack_commander
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michal Cichra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|