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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae92cb3f3a8d18e64d2db4c240119b96e1062e80
4
- data.tar.gz: d5294e75db6fb3afb27f1d56db44270c38be8840
3
+ metadata.gz: 88727f317aeb1c15a2cf5125608b3307f006453d
4
+ data.tar.gz: d1112eadfc48d7f147da59051aa503bc82622e2b
5
5
  SHA512:
6
- metadata.gz: cccf36d9c8c16c035f3f43aef5d88daf0cb75bd6d0f95a58fd4f0dd3d3817f10fd1b23199f11dba4b8b4163c0b5a0a9475f282fd66dc0a47c8ed8a77d7e5cee5
7
- data.tar.gz: d93ec0f9f0f91166cac8099d6362ec66bf6d6b32e08a5065df8e8213a18ec9f7b03e900457499bbac34e3e148ca2056fc65a1ea9b91e6ceb8de2fd0ca43d5f89
6
+ metadata.gz: 838fbcc1b518d5c11d54be5bb408a5376c8baefac8c048d1089d901e18d4da3f892d17e89638e4d8b2c5b22935e8c1f59b132a2543661e97093a7723027f397e
7
+ data.tar.gz: 406882fa923046d3018e9a1119180e9e5e424157f05ad476ac1fd7b39fe0b7da9bc66172d26e8bfd5807363ef7419fc06049d79fc06523630285dcaee027390f
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .ruby-version
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
data/Gemfile CHANGED
@@ -3,6 +3,10 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in stack_commander.gemspec
4
4
  gemspec
5
5
 
6
- gem 'yard'
7
6
  gem 'redcarpet'
8
7
  gem 'github-markup'
8
+
9
+ group :test do
10
+ gem 'pry-debugger'
11
+ gem 'pry-stack_explorer'
12
+ end
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`, `rescue` and `insurance` are called without state parameter
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,2 +1,9 @@
1
1
  require 'bundler/gem_tasks'
2
2
 
3
+ task :default do
4
+ exec 'rspec'
5
+ end
6
+
7
+ task :console do
8
+ exec 'irb', '-Ilib', '-rstack_commander'
9
+ end
@@ -1,9 +1,9 @@
1
- require 'thread'
1
+ require 'stack_commander'
2
2
 
3
3
  module StackCommander
4
4
  class Stack
5
5
  def initialize(scope)
6
- @queue = Queue.new
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
- return if @queue.empty?
22
- command_class = @queue.pop
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
- private
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
@@ -1,3 +1,3 @@
1
1
  module StackCommander
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  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) { double('command').as_null_object }
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
- expect(command).to receive(:call).with(stack)
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
- expect(command).to receive(:new).with
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.1
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-08 00:00:00.000000000 Z
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler