substation 0.0.9 → 0.0.10.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +0 -1
  3. data/Changelog.md +24 -82
  4. data/Gemfile.devtools +17 -24
  5. data/README.md +46 -116
  6. data/config/flay.yml +2 -2
  7. data/config/flog.yml +1 -1
  8. data/config/mutant.yml +0 -1
  9. data/config/reek.yml +5 -10
  10. data/lib/substation.rb +3 -8
  11. data/lib/substation/chain.rb +64 -108
  12. data/lib/substation/chain/dsl.rb +30 -57
  13. data/lib/substation/dispatcher.rb +1 -3
  14. data/lib/substation/environment.rb +23 -9
  15. data/lib/substation/environment/dsl.rb +3 -4
  16. data/lib/substation/observer.rb +2 -4
  17. data/lib/substation/processor.rb +7 -106
  18. data/lib/substation/processor/evaluator.rb +42 -83
  19. data/lib/substation/processor/pivot.rb +25 -0
  20. data/lib/substation/processor/wrapper.rb +2 -4
  21. data/lib/substation/request.rb +1 -10
  22. data/lib/substation/response.rb +0 -11
  23. data/lib/substation/utils.rb +1 -3
  24. data/lib/substation/version.rb +1 -3
  25. data/spec/integration/substation/dispatcher/call_spec.rb +12 -12
  26. data/spec/spec_helper.rb +21 -30
  27. data/spec/unit/substation/chain/call_spec.rb +32 -202
  28. data/spec/unit/substation/chain/dsl/builder/class_methods/call_spec.rb +2 -2
  29. data/spec/unit/substation/chain/dsl/builder/dsl_spec.rb +6 -8
  30. data/spec/unit/substation/chain/dsl/builder/failure_chain_spec.rb +30 -0
  31. data/spec/unit/substation/chain/dsl/chain_spec.rb +2 -1
  32. data/spec/unit/substation/chain/dsl/class_methods/processors_spec.rb +24 -0
  33. data/spec/unit/substation/chain/dsl/initialize_spec.rb +19 -0
  34. data/spec/unit/substation/chain/dsl/processors_spec.rb +21 -9
  35. data/spec/unit/substation/chain/dsl/use_spec.rb +3 -2
  36. data/spec/unit/substation/chain/each_spec.rb +9 -5
  37. data/spec/unit/substation/chain/incoming/result_spec.rb +21 -0
  38. data/spec/unit/substation/chain/outgoing/call_spec.rb +25 -0
  39. data/spec/unit/substation/{processor → chain/outgoing}/result_spec.rb +5 -6
  40. data/spec/unit/substation/dispatcher/action/call_spec.rb +6 -7
  41. data/spec/unit/substation/dispatcher/action/class_methods/coerce_spec.rb +5 -7
  42. data/spec/unit/substation/dispatcher/action_names_spec.rb +1 -1
  43. data/spec/unit/substation/dispatcher/call_spec.rb +3 -3
  44. data/spec/unit/substation/dispatcher/class_methods/coerce_spec.rb +7 -7
  45. data/spec/unit/substation/environment/chain_spec.rb +32 -22
  46. data/spec/unit/substation/environment/class_methods/build_spec.rb +4 -11
  47. data/spec/unit/substation/environment/dsl/class_methods/registry_spec.rb +3 -5
  48. data/spec/unit/substation/environment/dsl/register_spec.rb +3 -8
  49. data/spec/unit/substation/environment/dsl/registry_spec.rb +3 -5
  50. data/spec/unit/substation/observer/chain/call_spec.rb +3 -5
  51. data/spec/unit/substation/observer/class_methods/coerce_spec.rb +2 -4
  52. data/spec/unit/substation/observer/null/call_spec.rb +1 -3
  53. data/spec/unit/substation/processor/evaluator/call_spec.rb +35 -21
  54. data/spec/unit/substation/processor/pivot/call_spec.rb +17 -0
  55. data/spec/unit/substation/processor/wrapper/call_spec.rb +7 -8
  56. data/spec/unit/substation/request/env_spec.rb +4 -5
  57. data/spec/unit/substation/request/error_spec.rb +4 -5
  58. data/spec/unit/substation/request/input_spec.rb +4 -5
  59. data/spec/unit/substation/request/success_spec.rb +4 -5
  60. data/spec/unit/substation/response/env_spec.rb +5 -6
  61. data/spec/unit/substation/response/failure/success_predicate_spec.rb +4 -5
  62. data/spec/unit/substation/response/input_spec.rb +5 -6
  63. data/spec/unit/substation/response/output_spec.rb +4 -5
  64. data/spec/unit/substation/response/request_spec.rb +5 -6
  65. data/spec/unit/substation/response/success/success_predicate_spec.rb +4 -5
  66. data/spec/unit/substation/utils/class_methods/coerce_callable_spec.rb +13 -15
  67. data/spec/unit/substation/utils/class_methods/const_get_spec.rb +6 -6
  68. data/spec/unit/substation/utils/class_methods/symbolize_keys_spec.rb +4 -4
  69. data/substation.gemspec +1 -1
  70. metadata +18 -45
  71. data/config/rubocop.yml +0 -35
  72. data/lib/substation/processor/transformer.rb +0 -26
  73. data/spec/unit/substation/chain/class_methods/failure_response_spec.rb +0 -16
  74. data/spec/unit/substation/chain/dsl/class_methods/build_spec.rb +0 -24
  75. data/spec/unit/substation/chain/dsl/failure_chain_spec.rb +0 -35
  76. data/spec/unit/substation/chain/failure_data/equalizer_spec.rb +0 -46
  77. data/spec/unit/substation/chain/failure_data/hash_spec.rb +0 -13
  78. data/spec/unit/substation/environment/equalizer_spec.rb +0 -25
  79. data/spec/unit/substation/processor/evaluator/class_methods/new_spec.rb +0 -9
  80. data/spec/unit/substation/processor/evaluator/data/call_spec.rb +0 -34
  81. data/spec/unit/substation/processor/evaluator/pivot/call_spec.rb +0 -34
  82. data/spec/unit/substation/processor/evaluator/request/call_spec.rb +0 -34
  83. data/spec/unit/substation/processor/fallible/name_spec.rb +0 -15
  84. data/spec/unit/substation/processor/fallible/with_failure_chain_spec.rb +0 -18
  85. data/spec/unit/substation/processor/incoming/result_spec.rb +0 -25
  86. data/spec/unit/substation/processor/outgoing/call_spec.rb +0 -28
  87. data/spec/unit/substation/processor/outgoing/name_spec.rb +0 -14
  88. data/spec/unit/substation/processor/outgoing/success_predicate_spec.rb +0 -15
  89. data/spec/unit/substation/processor/success_predicate_spec.rb +0 -22
  90. data/spec/unit/substation/processor/transformer/call_spec.rb +0 -21
  91. data/spec/unit/substation/request/name_spec.rb +0 -15
  92. data/spec/unit/substation/response/to_request_spec.rb +0 -19
@@ -7,8 +7,8 @@ describe Chain::DSL::Builder, '.call' do
7
7
 
8
8
  let(:registry) { { :test => Spec::Processor } }
9
9
 
10
- let(:builder) { mock(:dsl => dsl) }
11
- let(:dsl) { mock }
10
+ let(:builder) { double(:dsl => dsl) }
11
+ let(:dsl) { double }
12
12
 
13
13
  before do
14
14
  described_class.should_receive(:new).with(registry).and_return(builder)
@@ -3,21 +3,19 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Chain::DSL::Builder, '#dsl' do
6
- subject { dsl.new(processors, &block) }
6
+ subject { dsl.new(env, processors, &block) }
7
7
 
8
+ let(:env) { Spec::FAKE_ENV }
8
9
  let(:dsl) { builder.dsl }
9
10
  let(:builder) { described_class.new(registry) }
10
11
  let(:registry) { { :test => Spec::Processor } }
11
12
  let(:processors) { [] }
12
- let(:block) { ->(_) { test(Spec::FAKE_HANDLER, EMPTY_ARRAY) } }
13
-
13
+ let(:block) { lambda { |_| test(Spec::FAKE_HANDLER) } }
14
14
  let(:processor) { Spec::FAKE_PROCESSOR }
15
15
 
16
- it 'should register instantiated processors' do
17
- expect(subject.processors).to include(processor)
18
- end
16
+ its(:processors) { should include(processor) }
19
17
 
20
- it 'should create a subclass of Chain::DSL' do
21
- expect(subject.class).to be < Chain::DSL
18
+ it "should create a subclass of Chain::DSL" do
19
+ subject.class.should be < Chain::DSL
22
20
  end
23
21
  end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "Initializing a processor's failure chain" do
6
+ subject { chain.call(request) }
7
+
8
+ let(:env) {
9
+ Environment.build {
10
+ register :evaluate, Processor::Evaluator
11
+ register :wrap, Processor::Wrapper
12
+ }
13
+ }
14
+
15
+ let(:chain) {
16
+ env.chain {
17
+ evaluate(Spec::Handler::Evaluator) {
18
+ wrap Spec::Presenter
19
+ }
20
+ }
21
+ }
22
+
23
+ let(:request) { Request.new(app_env, input) }
24
+ let(:app_env) { double }
25
+ let(:input) { :invalid }
26
+
27
+ let(:response) { Response::Failure.new(request, Spec::Presenter.new(:failure)) }
28
+
29
+ it { should eql(response) }
30
+ end
@@ -5,7 +5,8 @@ require 'spec_helper'
5
5
  describe Chain::DSL, '#chain' do
6
6
  subject { object.chain(other) }
7
7
 
8
- let(:object) { described_class.new(chain) }
8
+ let(:object) { described_class.new(env, chain) }
9
+ let(:env) { Spec::FAKE_ENV }
9
10
  let(:chain) { Chain::EMPTY }
10
11
  let(:other) { [ processor ] }
11
12
  let(:processor) { Spec::FAKE_PROCESSOR }
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Chain::DSL, '.processors' do
6
+
7
+ let(:env) { Spec::FAKE_ENV }
8
+ let(:chain) { Chain::EMPTY }
9
+
10
+ context "and a block is given" do
11
+ subject { described_class.processors(env, chain, &block) }
12
+
13
+ let(:block) { lambda { |_| use(Spec::FAKE_PROCESSOR) } }
14
+ let(:processor) { Spec::FAKE_PROCESSOR }
15
+
16
+ it { should include(processor) }
17
+ end
18
+
19
+ context "and no block is given" do
20
+ subject { described_class.processors(env, chain) }
21
+
22
+ it { should be_empty }
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ # Test wether env is correctly passed down
6
+ describe Chain::DSL, '#initialize' do
7
+ subject { dsl.new(env, processors, &block) }
8
+
9
+ let(:env) { Spec::FAKE_ENV }
10
+ let(:dsl) { builder.dsl }
11
+ let(:builder) { described_class::Builder.new(registry) }
12
+ let(:registry) { { :test => Spec::Processor } }
13
+ let(:processors) { [] }
14
+ let(:block) { lambda { |_| test(Spec::FAKE_HANDLER) } }
15
+
16
+ let(:processor) { Spec::FAKE_PROCESSOR }
17
+
18
+ its(:processors) { should include(processor) }
19
+ end
@@ -5,26 +5,38 @@ require 'spec_helper'
5
5
  describe Chain::DSL, '#processors' do
6
6
  subject { object.processors }
7
7
 
8
+ let(:env) { Spec::FAKE_ENV }
8
9
  let(:processor) { Spec::FAKE_PROCESSOR }
9
- let(:name) { mock }
10
10
 
11
- context 'and a block is given' do
12
- let(:object) { described_class.new(chain, &block) }
13
- let(:chain) { EMPTY_ARRAY }
14
- let(:block) { ->(_) { use(Spec::FAKE_PROCESSOR) } }
11
+ context "and a block is given" do
12
+ let(:object) { described_class.new(env, chain, &block) }
13
+ let(:chain) { Chain::EMPTY }
14
+ let(:block) { lambda { |_| use(Spec::FAKE_PROCESSOR) } }
15
15
 
16
16
  it { should include(processor) }
17
17
 
18
18
  its(:length) { should == 1 }
19
19
  end
20
20
 
21
- context 'and no block is given' do
22
- let(:object) { described_class.new(chain) }
23
- let(:chain) { Chain.new([ processor ], failure_chain) }
24
- let(:failure_chain) { mock }
21
+ context "and no block is given" do
22
+ let(:object) { described_class.new(env, chain) }
23
+ let(:chain) { Chain.new([ processor ]) }
25
24
 
26
25
  it { should include(processor) }
27
26
 
28
27
  its(:length) { should == 1 }
29
28
  end
29
+
30
+ # TODO move this elsewhere once mutant supports it
31
+ context "test DSL#initialize" do
32
+ let(:object) { described_class.new(env, chain, &block) }
33
+ let(:chain) { Chain::EMPTY }
34
+ let(:block) { lambda { |_| use(Spec::FAKE_PROCESSOR) } }
35
+
36
+ it { should include(processor) }
37
+
38
+ it "passes down the env to registered processors" do
39
+ subject.first.env.should be(env)
40
+ end
41
+ end
30
42
  end
@@ -5,8 +5,9 @@ require 'spec_helper'
5
5
  describe Chain::DSL, '#use' do
6
6
  subject { object.use(processor) }
7
7
 
8
- let(:object) { described_class.new(chain) }
9
- let(:chain) { EMPTY_ARRAY }
8
+ let(:object) { described_class.new(env, chain) }
9
+ let(:env) { Spec::FAKE_ENV }
10
+ let(:chain) { Chain::EMPTY }
10
11
  let(:processor) { Spec::FAKE_PROCESSOR }
11
12
 
12
13
  its(:processors) { should include(processor) }
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  describe Chain, '#each' do
6
6
  subject { object.each { |tuple| yields << processor } }
7
7
 
8
- let(:object) { described_class.new(processors, described_class::EMPTY) }
8
+ let(:object) { described_class.new(processors) }
9
9
  let(:processors) { [ processor ] }
10
10
  let(:processor) { Spec::FAKE_PROCESSOR }
11
11
  let(:yields) { [] }
@@ -22,14 +22,14 @@ describe Chain, '#each' do
22
22
  end
23
23
 
24
24
  it 'yields only processors with the expected handler' do
25
- expect { subject }.to change { yields.dup }
26
- .from([])
27
- .to([ processor ])
25
+ expect { subject }.to change { yields.dup }.
26
+ from([]).
27
+ to([ processor ])
28
28
  end
29
29
  end
30
30
 
31
31
  describe Chain do
32
- subject { described_class.new(processors, described_class::EMPTY) }
32
+ subject { described_class.new(processors) }
33
33
 
34
34
  let(:processors) { [ processor ] }
35
35
  let(:processor) { Spec::FAKE_PROCESSOR }
@@ -39,4 +39,8 @@ describe Chain do
39
39
  end
40
40
 
41
41
  it { should be_kind_of(Enumerable) }
42
+
43
+ it 'case matches Enumerable' do
44
+ (Enumerable === subject).should be(true)
45
+ end
42
46
  end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Chain::Incoming, '#result' do
6
+
7
+ subject { object.result(response) }
8
+
9
+ let(:object) {
10
+ Class.new {
11
+ include Substation::Chain::Incoming
12
+ }.new
13
+ }
14
+
15
+ let(:response) { Response::Success.new(request, input) }
16
+ let(:request) { Request.new(env, input) }
17
+ let(:env) { double }
18
+ let(:input) { double }
19
+
20
+ it { should eql(request) }
21
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Chain::Outgoing, '#call' do
6
+
7
+ subject { object.call(request) }
8
+
9
+ let(:object) {
10
+ Class.new {
11
+ include Substation::Chain::Outgoing
12
+ def call(request)
13
+ response = request.success(request.input)
14
+ respond_with(response, :altered)
15
+ end
16
+ }.new
17
+ }
18
+
19
+ let(:response) { Response::Success.new(request, :altered) }
20
+ let(:request) { Request.new(env, input) }
21
+ let(:env) { double }
22
+ let(:input) { double }
23
+
24
+ it { should eql(response) }
25
+ end
@@ -2,21 +2,20 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Processor, '#result' do
5
+ describe Chain::Outgoing, '#result' do
6
6
 
7
7
  subject { object.result(response) }
8
8
 
9
9
  let(:object) {
10
10
  Class.new {
11
- include Substation::Processor
11
+ include Substation::Chain::Outgoing
12
12
  }.new
13
13
  }
14
14
 
15
15
  let(:response) { Response::Success.new(request, input) }
16
- let(:request) { Request.new(name, env, input) }
17
- let(:name) { mock }
18
- let(:env) { mock }
19
- let(:input) { mock }
16
+ let(:request) { Request.new(env, input) }
17
+ let(:env) { double }
18
+ let(:input) { double }
20
19
 
21
20
  it { should be(response) }
22
21
  end
@@ -7,13 +7,12 @@ describe Dispatcher::Action, '#call' do
7
7
  subject { object.call(request) }
8
8
 
9
9
  let(:object) { described_class.new(klass, observer) }
10
- let(:klass) { mock }
11
- let(:observer) { mock }
12
- let(:request) { Request.new(name, env, input) }
13
- let(:name) { mock }
14
- let(:env) { mock }
15
- let(:input) { mock }
16
- let(:response) { mock }
10
+ let(:klass) { double }
11
+ let(:observer) { double }
12
+ let(:request) { Request.new(env, input) }
13
+ let(:env) { double }
14
+ let(:input) { double }
15
+ let(:response) { double }
17
16
 
18
17
  before do
19
18
  klass.should_receive(:call).with(request).and_return(response)
@@ -9,15 +9,13 @@ describe Dispatcher::Action, '.coerce' do
9
9
  let(:action) { Spec::Action::Success }
10
10
  let(:coerced) { Dispatcher::Action.new(action, observer) }
11
11
 
12
- context 'when config is a hash' do
13
- context 'and coercion is possible' do
12
+ context "when config is a hash" do
13
+ context "and coercion is possible" do
14
14
 
15
- let(:config) {
16
- {
15
+ let(:config) {{
17
16
  :action => action,
18
17
  :observer => observer_value
19
- }
20
- }
18
+ }}
21
19
 
22
20
  before do
23
21
  Observer.should_receive(:coerce).with(observer_value).and_return(observer)
@@ -48,7 +46,7 @@ describe Dispatcher::Action, '.coerce' do
48
46
  end
49
47
  end
50
48
 
51
- context 'when config is no hash' do
49
+ context "when config is no hash" do
52
50
  let(:config) { action }
53
51
  let(:observer_value) { nil }
54
52
  let(:observer) { Observer::NULL }
@@ -8,7 +8,7 @@ describe Dispatcher, '#action_names' do
8
8
 
9
9
  let(:object) { described_class.new(config, env) }
10
10
  let(:config) { { :test => { :action => Spec::Action::Success } } }
11
- let(:env) { mock }
11
+ let(:env) { double }
12
12
 
13
13
  it { should eql(Set[ :test ]) }
14
14
  end
@@ -8,9 +8,9 @@ describe Dispatcher, '#call' do
8
8
 
9
9
  let(:object) { described_class.coerce(config, env) }
10
10
  let(:config) { { 'test' => { 'action' => 'Spec::Action::Success' } } }
11
- let(:request) { Request.new(action_name, env, input) }
12
- let(:env) { mock }
13
- let(:input) { mock }
11
+ let(:request) { Request.new(env, input) }
12
+ let(:input) { double }
13
+ let(:env) { double }
14
14
 
15
15
  let(:expected_response) do
16
16
  Spec::Action::Success.call(request)
@@ -6,15 +6,15 @@ describe Dispatcher, '.coerce' do
6
6
 
7
7
  subject { described_class.coerce(config, env) }
8
8
 
9
- let(:config) {
10
- { 'test' => { 'action' => 'Spec::Action::Success' } }
11
- }
9
+ let(:config) {{
10
+ 'test' => { 'action' => 'Spec::Action::Success' }
11
+ }}
12
12
 
13
- let(:env) { mock }
13
+ let(:env) { double }
14
14
 
15
- let(:coerced) {
16
- { :test => described_class::Action.coerce(:action => 'Spec::Action::Success') }
17
- }
15
+ let(:coerced) {{
16
+ :test => described_class::Action.coerce(:action => 'Spec::Action::Success')
17
+ }}
18
18
 
19
19
  it { should eql(described_class.new(coerced, env)) }
20
20
  end
@@ -1,40 +1,50 @@
1
- # encoding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe Substation::Environment, '#chain' do
6
4
 
7
- let(:object) { described_class.new(registry, dsl) }
5
+ let(:object) { described_class.build(&block) }
8
6
 
7
+ let(:other) { Chain::EMPTY }
8
+ let(:chain) { lambda { |_| test Spec::FAKE_HANDLER } }
9
9
  let(:dsl) { Chain::DSL::Builder.call(registry) }
10
- let(:registry) { described_class::DSL.registry(&r_block) }
11
- let(:r_block) { ->(_) { register(:test, Spec::Processor) } }
10
+ let(:registry) { described_class::DSL.registry(&block) }
11
+ let(:block) { lambda { |_| register(:test, Spec::Processor) } }
12
12
 
13
- let(:other) { mock('other', :each => EMPTY_ARRAY) }
14
- let(:failure_chain) { mock('failure_chain') }
15
- let(:block) { ->(_) { test Spec::FAKE_HANDLER, Chain::EMPTY } }
13
+ let(:expected) { Chain.new(processors) }
16
14
 
17
- context 'when other, failure_chain and block are given' do
18
- subject { object.chain(other, failure_chain, &block) }
15
+ context "when other is not given" do
16
+ context "and a block is given" do
17
+ subject { object.chain(&chain) }
19
18
 
20
- it { should eql(dsl.build(other, failure_chain, &block)) }
21
- end
19
+ let(:processors) { dsl.processors(object, other, &chain) }
22
20
 
23
- context 'when other, failure_chain and no block are given' do
24
- subject { object.chain(other, failure_chain) }
21
+ it { should eql(expected) }
22
+ end
25
23
 
26
- it { should eql(dsl.build(other, failure_chain)) }
27
- end
24
+ context "and no block is given" do
25
+ subject { object.chain }
28
26
 
29
- context 'when other, no failure_chain and no block are given' do
30
- subject { object.chain(other) }
27
+ let(:processors) { dsl.processors(object, other) }
31
28
 
32
- it { should eql(dsl.build(other, Chain::EMPTY)) }
29
+ it { should eql(expected) }
30
+ end
33
31
  end
34
32
 
35
- context 'when no parameters are given' do
36
- subject { object.chain }
33
+ context "when other is given" do
34
+ context "and a block is given" do
35
+ subject { object.chain(other, &chain) }
36
+
37
+ let(:processors) { dsl.processors(object, other, &chain) }
38
+
39
+ it { should eql(expected) }
40
+ end
41
+
42
+ context "and no block is given" do
43
+ subject { object.chain(other) }
44
+
45
+ let(:processors) { dsl.processors(object, other) }
37
46
 
38
- it { should eql(dsl.build(Chain::EMPTY, Chain::EMPTY)) }
47
+ it { should eql(expected) }
48
+ end
39
49
  end
40
50
  end