substation 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/.travis.yml +4 -0
  2. data/Changelog.md +92 -2
  3. data/Gemfile.devtools +19 -17
  4. data/README.md +116 -46
  5. data/config/flay.yml +2 -2
  6. data/config/mutant.yml +1 -0
  7. data/config/reek.yml +11 -5
  8. data/config/rubocop.yml +35 -0
  9. data/lib/substation.rb +10 -1
  10. data/lib/substation/chain.rb +108 -64
  11. data/lib/substation/chain/dsl.rb +62 -37
  12. data/lib/substation/dispatcher.rb +3 -1
  13. data/lib/substation/environment.rb +9 -6
  14. data/lib/substation/environment/dsl.rb +4 -3
  15. data/lib/substation/observer.rb +2 -0
  16. data/lib/substation/processor.rb +106 -7
  17. data/lib/substation/processor/evaluator.rb +98 -7
  18. data/lib/substation/processor/transformer.rb +26 -0
  19. data/lib/substation/processor/wrapper.rb +5 -3
  20. data/lib/substation/request.rb +12 -1
  21. data/lib/substation/response.rb +13 -0
  22. data/lib/substation/utils.rb +3 -1
  23. data/lib/substation/version.rb +3 -1
  24. data/spec/integration/substation/dispatcher/call_spec.rb +12 -12
  25. data/spec/spec_helper.rb +39 -32
  26. data/spec/unit/substation/chain/call_spec.rb +205 -29
  27. data/spec/unit/substation/chain/class_methods/failure_response_spec.rb +16 -0
  28. data/spec/unit/substation/chain/dsl/builder/dsl_spec.rb +7 -4
  29. data/spec/unit/substation/chain/dsl/class_methods/build_spec.rb +24 -0
  30. data/spec/unit/substation/chain/dsl/failure_chain_spec.rb +35 -0
  31. data/spec/unit/substation/chain/dsl/processors_spec.rb +8 -6
  32. data/spec/unit/substation/chain/dsl/use_spec.rb +1 -1
  33. data/spec/unit/substation/chain/each_spec.rb +5 -9
  34. data/spec/unit/substation/chain/failure_data/equalizer_spec.rb +46 -0
  35. data/spec/unit/substation/chain/failure_data/hash_spec.rb +13 -0
  36. data/spec/unit/substation/dispatcher/action/call_spec.rb +2 -1
  37. data/spec/unit/substation/dispatcher/action/class_methods/coerce_spec.rb +7 -5
  38. data/spec/unit/substation/dispatcher/call_spec.rb +2 -2
  39. data/spec/unit/substation/dispatcher/class_methods/coerce_spec.rb +6 -6
  40. data/spec/unit/substation/environment/chain_spec.rb +22 -27
  41. data/spec/unit/substation/environment/class_methods/build_spec.rb +11 -4
  42. data/spec/unit/substation/environment/dsl/class_methods/registry_spec.rb +5 -3
  43. data/spec/unit/substation/environment/dsl/register_spec.rb +8 -3
  44. data/spec/unit/substation/environment/dsl/registry_spec.rb +5 -3
  45. data/spec/unit/substation/environment/equalizer_spec.rb +25 -0
  46. data/spec/unit/substation/observer/chain/call_spec.rb +2 -0
  47. data/spec/unit/substation/observer/class_methods/coerce_spec.rb +2 -0
  48. data/spec/unit/substation/observer/null/call_spec.rb +2 -0
  49. data/spec/unit/substation/processor/evaluator/call_spec.rb +20 -10
  50. data/spec/unit/substation/processor/evaluator/class_methods/new_spec.rb +9 -0
  51. data/spec/unit/substation/processor/evaluator/data/call_spec.rb +34 -0
  52. data/spec/unit/substation/processor/evaluator/pivot/call_spec.rb +34 -0
  53. data/spec/unit/substation/processor/evaluator/request/call_spec.rb +34 -0
  54. data/spec/unit/substation/processor/fallible/name_spec.rb +15 -0
  55. data/spec/unit/substation/processor/fallible/with_failure_chain_spec.rb +18 -0
  56. data/spec/unit/substation/processor/incoming/result_spec.rb +25 -0
  57. data/spec/unit/substation/processor/outgoing/call_spec.rb +28 -0
  58. data/spec/unit/substation/processor/outgoing/name_spec.rb +14 -0
  59. data/spec/unit/substation/processor/outgoing/success_predicate_spec.rb +15 -0
  60. data/spec/unit/substation/{chain/outgoing → processor}/result_spec.rb +4 -3
  61. data/spec/unit/substation/processor/success_predicate_spec.rb +22 -0
  62. data/spec/unit/substation/processor/transformer/call_spec.rb +21 -0
  63. data/spec/unit/substation/processor/wrapper/call_spec.rb +9 -7
  64. data/spec/unit/substation/request/env_spec.rb +3 -2
  65. data/spec/unit/substation/request/error_spec.rb +2 -1
  66. data/spec/unit/substation/request/input_spec.rb +3 -2
  67. data/spec/unit/substation/request/name_spec.rb +15 -0
  68. data/spec/unit/substation/request/success_spec.rb +2 -1
  69. data/spec/unit/substation/response/env_spec.rb +3 -2
  70. data/spec/unit/substation/response/failure/success_predicate_spec.rb +2 -1
  71. data/spec/unit/substation/response/input_spec.rb +3 -2
  72. data/spec/unit/substation/response/output_spec.rb +2 -1
  73. data/spec/unit/substation/response/request_spec.rb +3 -2
  74. data/spec/unit/substation/response/success/success_predicate_spec.rb +2 -1
  75. data/spec/unit/substation/response/to_request_spec.rb +19 -0
  76. data/spec/unit/substation/utils/class_methods/coerce_callable_spec.rb +14 -12
  77. data/spec/unit/substation/utils/class_methods/const_get_spec.rb +6 -6
  78. data/spec/unit/substation/utils/class_methods/symbolize_keys_spec.rb +4 -4
  79. metadata +25 -9
  80. data/lib/substation/processor/pivot.rb +0 -25
  81. data/spec/unit/substation/chain/class_methods/build_spec.rb +0 -31
  82. data/spec/unit/substation/chain/dsl/class_methods/processors_spec.rb +0 -23
  83. data/spec/unit/substation/chain/incoming/result_spec.rb +0 -21
  84. data/spec/unit/substation/chain/outgoing/call_spec.rb +0 -25
  85. data/spec/unit/substation/processor/pivot/call_spec.rb +0 -16
@@ -1,17 +1,19 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Substation::Environment::DSL, '#registry' do
4
6
  subject { object.registry }
5
7
 
6
- context "when a block is given" do
8
+ context 'when a block is given' do
7
9
  let(:object) { described_class.new(&block) }
8
- let(:block) { lambda { |_| register :test, Spec::Processor } }
10
+ let(:block) { ->(_) { register :test, Spec::Processor } }
9
11
  let(:expected) { { :test => Spec::Processor } }
10
12
 
11
13
  it { should eql(expected) }
12
14
  end
13
15
 
14
- context "when no block is given" do
16
+ context 'when no block is given' do
15
17
  let(:object) { described_class.new }
16
18
 
17
19
  it { should eql({}) }
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Substation::Environment, 'equalizer behavior' do
6
+ subject { object == other }
7
+
8
+ let(:object) { described_class.new(registry, dsl) }
9
+ let(:other) { described_class.new(other_registry, dsl) }
10
+
11
+ let(:registry) { mock }
12
+ let(:dsl) { mock }
13
+
14
+ context 'with an equal registry' do
15
+ let(:other_registry) { registry }
16
+
17
+ it { should be(true) }
18
+ end
19
+
20
+ context 'with a different registry' do
21
+ let(:other_registry) { mock }
22
+
23
+ it { should be(false) }
24
+ end
25
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Observer::Chain, '#call' do
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Observer, '.coerce' do
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Observer::NULL, '#call' do
@@ -5,21 +5,31 @@ require 'spec_helper'
5
5
  describe Processor::Evaluator, '#call' do
6
6
  subject { object.call(request) }
7
7
 
8
- let(:object) { described_class.new(Spec::Handler::Evaluator.new) }
9
- let(:request) { Request.new(env, input) }
10
- let(:env) { mock }
8
+ let(:object) { klass.new(processor_name, handler, failure_chain) }
9
+ let(:processor_name) { mock }
10
+ let(:klass) { Class.new(described_class) { include Processor::Incoming } }
11
+ let(:failure_chain) { mock(:call => failure_response) }
12
+ let(:failure_response) { mock }
13
+ let(:request) { Request.new(action_name, env, input) }
14
+ let(:action_name) { mock }
15
+ let(:env) { mock }
16
+ let(:input) { mock }
11
17
 
12
- context "when evaluation is successful" do
13
- let(:input) { :success }
14
- let(:response) { Response::Success.new(request, input) }
18
+ context 'with a successful handler' do
19
+ let(:handler) { Spec::Action::Success }
20
+ let(:response) { Response::Success.new(request, Spec.response_data) }
15
21
 
16
22
  it { should eql(response) }
17
23
  end
18
24
 
19
- context "when evaluation is not successful" do
20
- let(:input) { :invalid }
21
- let(:response) { Response::Failure.new(request, :failure) }
25
+ context 'with a failing handler' do
26
+ let(:handler) { Spec::Action::Failure }
27
+ let(:response) { Response::Failure.new(request, Spec.response_data) }
22
28
 
23
- it { should eql(response) }
29
+ before do
30
+ failure_chain.should_receive(:call).with(response).and_return(failure_response)
31
+ end
32
+
33
+ it { should eql(failure_response) }
24
34
  end
25
35
  end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Evaluator, '.new' do
6
+ subject { object.new }
7
+
8
+ it_behaves_like 'an abstract type'
9
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Evaluator::Data, '#call' do
6
+ subject { object.call(request) }
7
+
8
+ let(:object) { described_class.new(processor_name, handler, failure_chain) }
9
+ let(:processor_name) { mock }
10
+ let(:failure_chain) { mock(:call => failure_response) }
11
+ let(:failure_response) { mock }
12
+ let(:handler) { Spec::Handler::Evaluator }
13
+ let(:request) { Request.new(action_name, env, input) }
14
+ let(:action_name) { mock }
15
+ let(:env) { mock }
16
+
17
+ context 'with a successful handler' do
18
+ let(:input) { :success }
19
+ let(:response) { request.success(input) }
20
+
21
+ it { should eql(response) }
22
+ end
23
+
24
+ context 'with a failing handler' do
25
+ let(:input) { :foo }
26
+ let(:response) { request.error(:failure) }
27
+
28
+ before do
29
+ failure_chain.should_receive(:call).with(response).and_return(failure_response)
30
+ end
31
+
32
+ it { should eql(failure_response) }
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Evaluator::Pivot, '#call' do
6
+ subject { object.call(request) }
7
+
8
+ let(:object) { described_class.new(processor_name, handler, failure_chain) }
9
+ let(:processor_name) { mock }
10
+ let(:failure_chain) { mock(:call => failure_response) }
11
+ let(:failure_response) { mock }
12
+ let(:request) { Request.new(action_name, env, input) }
13
+ let(:action_name) { mock }
14
+ let(:env) { mock }
15
+ let(:input) { mock }
16
+
17
+ context 'with a successful handler' do
18
+ let(:handler) { Spec::Action::Success }
19
+ let(:response) { Response::Success.new(request, Spec.response_data) }
20
+
21
+ it { should eql(response) }
22
+ end
23
+
24
+ context 'with a failing handler' do
25
+ let(:handler) { Spec::Action::Failure }
26
+ let(:response) { Response::Failure.new(request, Spec.response_data) }
27
+
28
+ before do
29
+ failure_chain.should_receive(:call).with(response).and_return(failure_response)
30
+ end
31
+
32
+ it { should eql(failure_response) }
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Evaluator::Request, '#call' do
6
+ subject { object.call(request) }
7
+
8
+ let(:object) { described_class.new(processor_name, handler, failure_chain) }
9
+ let(:processor_name) { mock }
10
+ let(:failure_chain) { mock(:call => failure_response) }
11
+ let(:failure_response) { mock }
12
+ let(:request) { Request.new(action_name, env, input) }
13
+ let(:action_name) { mock }
14
+ let(:env) { mock }
15
+ let(:input) { mock }
16
+
17
+ context 'with a successful handler' do
18
+ let(:handler) { Spec::Action::Success }
19
+ let(:response) { Response::Success.new(request, Spec.response_data) }
20
+
21
+ it { should eql(response) }
22
+ end
23
+
24
+ context 'with a failing handler' do
25
+ let(:handler) { Spec::Action::Failure }
26
+ let(:response) { Response::Failure.new(request, Spec.response_data) }
27
+
28
+ before do
29
+ failure_chain.should_receive(:call).with(response).and_return(failure_response)
30
+ end
31
+
32
+ it { should eql(failure_response) }
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Fallible, '#name' do
6
+ subject { object.name }
7
+
8
+ let(:object) { klass.new(name, handler, failure_chain) }
9
+ let(:klass) { Class.new { include Processor::Fallible } }
10
+ let(:name) { mock }
11
+ let(:handler) { mock }
12
+ let(:failure_chain) { mock }
13
+
14
+ it { should be(name) }
15
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Fallible, '#with_failure_chain' do
6
+ subject { object.with_failure_chain(chain) }
7
+
8
+ let(:object) { klass.new(name, handler, failure_chain) }
9
+ let(:klass) { Class.new { include Processor::Fallible } }
10
+ let(:name) { mock }
11
+ let(:handler) { mock }
12
+ let(:failure_chain) { mock }
13
+ let(:chain) { mock }
14
+
15
+ let(:expected) { klass.new(name, handler, chain) }
16
+
17
+ it { should eql(expected) }
18
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Incoming, '#result' do
6
+
7
+ subject { object.result(response) }
8
+
9
+ let(:object) {
10
+ Class.new {
11
+ include Substation::Processor::Incoming
12
+ }.new(processor_name, handler, failure_chain)
13
+ }
14
+
15
+ let(:processor_name) { mock }
16
+ let(:response) { Response::Success.new(request, input) }
17
+ let(:request) { Request.new(action_name, env, input) }
18
+ let(:action_name) { mock }
19
+ let(:env) { mock }
20
+ let(:input) { mock }
21
+ let(:failure_chain) { mock }
22
+ let(:handler) { mock }
23
+
24
+ it { should eql(request) }
25
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Outgoing, '#call' do
6
+
7
+ subject { object.call(request) }
8
+
9
+ let(:object) {
10
+ Class.new {
11
+ include Substation::Processor::Outgoing
12
+ def call(request)
13
+ response = request.success(request.input)
14
+ respond_with(response, :altered)
15
+ end
16
+ }.new(processor_name, handler)
17
+ }
18
+
19
+ let(:processor_name) { mock }
20
+ let(:response) { Response::Success.new(request, :altered) }
21
+ let(:request) { Request.new(action_name, env, input) }
22
+ let(:action_name) { mock }
23
+ let(:env) { mock }
24
+ let(:input) { mock }
25
+ let(:handler) { mock }
26
+
27
+ it { should eql(response) }
28
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Outgoing, '#name' do
6
+ subject { object.name }
7
+
8
+ let(:object) { klass.new(name, handler) }
9
+ let(:klass) { Class.new { include Processor::Outgoing } }
10
+ let(:name) { mock }
11
+ let(:handler) { mock }
12
+
13
+ it { should be(name) }
14
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Outgoing, '#success?' do
6
+ subject { object.success?(response) }
7
+
8
+ let(:object) { klass.new(name, handler) }
9
+ let(:klass) { Class.new { include Processor::Outgoing } }
10
+ let(:name) { mock }
11
+ let(:handler) { mock }
12
+ let(:response) { mock }
13
+
14
+ it { should be(true) }
15
+ end
@@ -2,18 +2,19 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Chain::Outgoing, '#result' do
5
+ describe Processor, '#result' do
6
6
 
7
7
  subject { object.result(response) }
8
8
 
9
9
  let(:object) {
10
10
  Class.new {
11
- include Substation::Chain::Outgoing
11
+ include Substation::Processor
12
12
  }.new
13
13
  }
14
14
 
15
15
  let(:response) { Response::Success.new(request, input) }
16
- let(:request) { Request.new(env, input) }
16
+ let(:request) { Request.new(name, env, input) }
17
+ let(:name) { mock }
17
18
  let(:env) { mock }
18
19
  let(:input) { mock }
19
20
 
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor, '#success?' do
6
+ subject { object.success?(response) }
7
+
8
+ let(:object) { klass.new }
9
+ let(:klass) { Class.new { include Processor } }
10
+
11
+ context 'with a successful response' do
12
+ let(:response) { mock(:success? => true) }
13
+
14
+ it { should be(true) }
15
+ end
16
+
17
+ context 'with a failure response' do
18
+ let(:response) { mock(:success? => false) }
19
+
20
+ it { should be(false) }
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Processor::Transformer, '#call' do
6
+ subject { object.call(response) }
7
+
8
+ let(:object) { described_class.new(processor_name, Spec::Transformer) }
9
+ let(:processor_name) { mock }
10
+ let(:response) { Response::Success.new(request, output) }
11
+ let(:request) { Request.new(action_name, env, input) }
12
+ let(:action_name) { mock }
13
+ let(:env) { mock }
14
+ let(:input) { mock }
15
+ let(:output) { mock }
16
+
17
+ let(:transformed) { Response::Success.new(request, data) }
18
+ let(:data) { Spec::Transformer.call(response) }
19
+
20
+ it { should eql(transformed) }
21
+ end
@@ -5,15 +5,17 @@ require 'spec_helper'
5
5
  describe Processor::Wrapper, '#call' do
6
6
  subject { object.call(response) }
7
7
 
8
- let(:object) { described_class.new(Spec::Handler::Wrapper::Presenter) }
9
- let(:response) { Response::Success.new(request, output) }
10
- let(:request) { Request.new(env, input) }
11
- let(:env) { mock }
12
- let(:input) { mock }
13
- let(:output) { mock }
8
+ let(:object) { described_class.new(processor_name, Spec::Presenter) }
9
+ let(:processor_name) { mock }
10
+ let(:response) { Response::Success.new(request, output) }
11
+ let(:request) { Request.new(action_name, env, input) }
12
+ let(:action_name) { mock }
13
+ let(:env) { mock }
14
+ let(:input) { mock }
15
+ let(:output) { mock }
14
16
 
15
17
  let(:wrapped) { Response::Success.new(request, data) }
16
- let(:data) { Spec::Handler::Wrapper::Presenter.new(output) }
18
+ let(:data) { Spec::Presenter.new(output) }
17
19
 
18
20
  it { should eql(wrapped) }
19
21
  end