light-service 0.3.3 → 0.3.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65825f46d1fd42f09f7e31d1c6237936c848aa7f
4
- data.tar.gz: cc2963f8195cb9e33b1bbb0fad85864d9249e29d
3
+ metadata.gz: 713a827193e2ac90169a99251cbb9d9745ed4394
4
+ data.tar.gz: 91400a943609efb9e0a811437b73d30faf1090cb
5
5
  SHA512:
6
- metadata.gz: 5b1cd806096d97fef2f82e8310affaa87102377ce6e318b613f8035ebe542731bcf5331d38e9a54fa748207cc4579da165c55a4a3d95cacd20988200034d52fc
7
- data.tar.gz: 5ab53d49c280764ef5cf6e1097ea103059db80d018bef4fc5533baeb80878c010c8c6852d6f9a2ad7abdba612d54e07031894f166ac072078d62e8ded2ef866f
6
+ metadata.gz: 4ceecbf3154d271ead7dfb8105dc38100589b0e0158943c3994a1f1649b30b9e5d65725b7d3416311b1975b5a8a5a0a181a2c26e4843689637b88a873d2792a3
7
+ data.tar.gz: 6f44c67e69cd032a46320d75ab6a7188f3d177e090207abdcbed34a1db984b914367d2ba6a9eeb3aaa881d3f4e9f91a59207323993db8984d7024b754ab387bc
@@ -2,6 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
- - 2.1.1
5
+ - 2.1.2
6
6
  # uncomment this line if your project needs to run something other than `rake`:
7
7
  script: bundle exec rspec spec
data/README.md CHANGED
@@ -273,6 +273,10 @@ For further examples, please visit the project's [Wiki](https://github.com/adomo
273
273
  Huge thanks to the [contributors](https://github.com/adomokos/light-service/graphs/contributors)!
274
274
 
275
275
  ## Release Notes
276
+ ### 0.3.4
277
+ * The method call `with` is [now optional](https://github.com/adomokos/light-service/blob/master/spec/organizer_spec.rb#L18) in case you have nothing to put into the context.
278
+ * Action name is being displayed in the error message when the expected or promised key is not in the context.
279
+
276
280
  ### 0.3.3
277
281
  * Switching the promises and expects keys accessors from Action to Context
278
282
 
@@ -14,19 +14,28 @@ module LightService
14
14
  @_promised_keys = args
15
15
  end
16
16
 
17
+ def expected_keys
18
+ @_expected_keys
19
+ end
20
+
21
+ def promised_keys
22
+ @_promised_keys
23
+ end
24
+
17
25
  def executed
18
26
  define_singleton_method "execute" do |context = {}|
19
27
  action_context = create_action_context(context)
20
28
  return action_context if action_context.stop_processing?
29
+ action = self
21
30
 
22
- ContextKeyVerifier.verify_expected_keys_are_in_context(action_context, @_expected_keys)
31
+ ContextKeyVerifier.verify_expected_keys_are_in_context(action_context, action)
23
32
 
24
- action_context.define_accessor_methods_for_keys(@_expected_keys)
25
- action_context.define_accessor_methods_for_keys(@_promised_keys)
33
+ action_context.define_accessor_methods_for_keys(expected_keys)
34
+ action_context.define_accessor_methods_for_keys(promised_keys)
26
35
 
27
36
  yield(action_context)
28
37
 
29
- ContextKeyVerifier.verify_promised_keys_are_in_context(action_context, @_promised_keys)
38
+ ContextKeyVerifier.verify_promised_keys_are_in_context(action_context, action)
30
39
  end
31
40
  end
32
41
 
@@ -4,15 +4,15 @@ module LightService
4
4
 
5
5
  class ContextKeyVerifier
6
6
  class << self
7
- def verify_expected_keys_are_in_context(context, expected_keys)
8
- verify_keys_are_in_context(context, expected_keys) do |not_found_keys|
9
- fail ExpectedKeysNotInContextError, "expected #{format_keys(not_found_keys)} to be in the context"
7
+ def verify_expected_keys_are_in_context(context, action)
8
+ verify_keys_are_in_context(context, action.expected_keys) do |not_found_keys|
9
+ fail ExpectedKeysNotInContextError, "expected #{format_keys(not_found_keys)} to be in the context during #{action.to_s}"
10
10
  end
11
11
  end
12
12
 
13
- def verify_promised_keys_are_in_context(context, promised_keys)
14
- verify_keys_are_in_context(context, promised_keys) do |not_found_keys|
15
- fail PromisedKeysNotInContextError, "promised #{format_keys(not_found_keys)} to be in the context"
13
+ def verify_promised_keys_are_in_context(context, action)
14
+ verify_keys_are_in_context(context, action.promised_keys) do |not_found_keys|
15
+ fail PromisedKeysNotInContextError, "promised #{format_keys(not_found_keys)} to be in the context during #{action.to_s}"
16
16
  end
17
17
  end
18
18
 
@@ -8,6 +8,10 @@ module LightService
8
8
  def with(data)
9
9
  new.with(data)
10
10
  end
11
+
12
+ def reduce(actions)
13
+ new.with.reduce(actions)
14
+ end
11
15
  end
12
16
 
13
17
  def with(data = {})
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -16,7 +16,8 @@ Gem::Specification.new do |gem|
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = LightService::VERSION
18
18
 
19
- gem.add_development_dependency("rspec", "~> 2.0")
19
+ gem.add_development_dependency("rspec", "~> 3.0")
20
+ gem.add_development_dependency("rspec-its", "~> 1.0")
20
21
  gem.add_development_dependency("simplecov", "~> 0.7.1")
21
22
  gem.add_development_dependency("pry", "0.9.12.2")
22
23
  end
@@ -11,6 +11,15 @@ module LightService
11
11
  context.milk_tea = "#{context.tea} - #{context.milk}"
12
12
  end
13
13
  end
14
+ class DummyActionForKeysToPromiseWithError
15
+ include LightService::Action
16
+ expects :tea, :milk
17
+ promises :milk_tea
18
+
19
+ executed do |context|
20
+ context[:some_tea] = "#{context.tea} - #{context.milk}"
21
+ end
22
+ end
14
23
 
15
24
  context "when expected keys are in the context" do
16
25
  it "can access the keys as class methods" do
@@ -23,13 +32,22 @@ module LightService
23
32
  end
24
33
  end
25
34
 
26
- context "when expected keys are not in the context" do
35
+ context "when expected key is not in the context" do
27
36
  it "raises an error" do
28
- exception_error_text = "expected :milk to be in the context"
37
+ exception_error_text = "expected :milk to be in the context during LightService::DummyActionForKeysToExpect"
29
38
  expect {
30
39
  DummyActionForKeysToExpect.execute(:tea => "black")
31
40
  }.to raise_error(ExpectedKeysNotInContextError, exception_error_text)
32
41
  end
33
42
  end
43
+
44
+ context "when promised key is not in context" do
45
+ it "raises an error" do
46
+ exception_error_text = "promised :milk_tea to be in the context during LightService::DummyActionForKeysToPromiseWithError"
47
+ expect {
48
+ DummyActionForKeysToPromiseWithError.execute(:tea => "black", :milk => "2%")
49
+ }.to raise_error(PromisedKeysNotInContextError, exception_error_text)
50
+ end
51
+ end
34
52
  end
35
53
  end
@@ -14,7 +14,7 @@ module LightService
14
14
 
15
15
  context "when the promised key is not in the context" do
16
16
  it "raises an ArgumentError" do
17
- exception_error_text = "promised :milk_tea, :something_else to be in the context"
17
+ exception_error_text = "promised :milk_tea, :something_else to be in the context during LightService::DummyActionForKeysToPromise"
18
18
  expect {
19
19
  DummyActionForKeysToPromise.execute(:tea => "black", :milk => "full cream")
20
20
  }.to raise_error(PromisedKeysNotInContextError, exception_error_text)
@@ -31,7 +31,7 @@ module LightService
31
31
 
32
32
  DummyAction.execute(context)
33
33
 
34
- context.to_hash.keys.should be_empty
34
+ expect(context.to_hash.keys).to be_empty
35
35
  end
36
36
  end
37
37
 
@@ -39,7 +39,7 @@ module LightService
39
39
  it "executes the block" do
40
40
  DummyAction.execute(context)
41
41
 
42
- context.to_hash.keys.should eq [:test_key]
42
+ expect(context.to_hash.keys).to eq [:test_key]
43
43
  end
44
44
  end
45
45
 
@@ -49,7 +49,7 @@ module LightService
49
49
 
50
50
  DummyAction.execute(context)
51
51
 
52
- context.to_hash.keys.should be_empty
52
+ expect(context.to_hash.keys).to be_empty
53
53
  end
54
54
 
55
55
  it "does not execute skipped actions" do
@@ -59,14 +59,14 @@ module LightService
59
59
 
60
60
  SkippedAction.execute(context)
61
61
 
62
- context.to_hash.should eq ({:test_key => "test_value"})
62
+ expect(context.to_hash).to eq ({:test_key => "test_value"})
63
63
  end
64
64
  end
65
65
 
66
66
  it "returns the context" do
67
67
  result = DummyAction.execute(context)
68
68
 
69
- result.to_hash.should eq ({:test_key => "test_value"})
69
+ expect(result.to_hash).to eq ({:test_key => "test_value"})
70
70
  end
71
71
 
72
72
  context "when invoked with hash" do
@@ -6,7 +6,7 @@ module LightService
6
6
  describe "can be made" do
7
7
  context "with no arguments" do
8
8
  subject { Context.make }
9
- it { should be_success }
9
+ it { is_expected.to be_success }
10
10
  its(:message) { should be_empty }
11
11
  end
12
12
 
@@ -49,7 +49,7 @@ module LightService
49
49
  context = Context.make
50
50
  context.skip_all!
51
51
 
52
- expect(context.skip_all?).to be_true
52
+ expect(context.skip_all?).to be_truthy
53
53
  end
54
54
 
55
55
  it "can be pushed into a SUCCESS state" do
@@ -112,12 +112,12 @@ module LightService
112
112
 
113
113
  it "flags processing to stop on failure" do
114
114
  context.fail!("on purpose")
115
- expect(context.stop_processing?).to be_true
115
+ expect(context.stop_processing?).to be_truthy
116
116
  end
117
117
 
118
118
  it "flags processing to stop when remaining actions should be skipped" do
119
119
  context.skip_all!
120
- expect(context.stop_processing?).to be_true
120
+ expect(context.stop_processing?).to be_truthy
121
121
  end
122
122
  end
123
123
 
@@ -14,6 +14,10 @@ describe LightService::Organizer do
14
14
  def self.do_something_with_no_actions(action_arguments)
15
15
  with(action_arguments).reduce
16
16
  end
17
+
18
+ def self.do_something_with_no_starting_context
19
+ reduce([AnAction, AnotherAction])
20
+ end
17
21
  end
18
22
 
19
23
  let(:context) { ::LightService::Context.make(user: user) }
@@ -21,10 +25,10 @@ describe LightService::Organizer do
21
25
 
22
26
  context "when #with is called with hash" do
23
27
  before do
24
- AnAction.should_receive(:execute) \
28
+ expect(AnAction).to receive(:execute) \
25
29
  .with(context) \
26
30
  .and_return context
27
- AnotherAction.should_receive(:execute) \
31
+ expect(AnotherAction).to receive(:execute) \
28
32
  .with(context) \
29
33
  .and_return context
30
34
  end
@@ -37,10 +41,10 @@ describe LightService::Organizer do
37
41
 
38
42
  context "when #with is called with Context" do
39
43
  before do
40
- AnAction.should_receive(:execute) \
44
+ expect(AnAction).to receive(:execute) \
41
45
  .with(context) \
42
46
  .and_return context
43
- AnotherAction.should_receive(:execute) \
47
+ expect(AnotherAction).to receive(:execute) \
44
48
  .with(context) \
45
49
  .and_return context
46
50
  end
@@ -54,7 +58,21 @@ describe LightService::Organizer do
54
58
  context "when no Actions are specified" do
55
59
  it "throws a Runtime error" do
56
60
  expect { AnOrganizer.do_something_with_no_actions(context) }.to \
57
- raise_error RuntimeError, "No action(s) were provided"
61
+ raise_error RuntimeError, "No action(s) were provided"
62
+ end
63
+ end
64
+
65
+ context "when no starting context is specified" do
66
+ it "creates one implicitly" do
67
+ expect(AnAction).to receive(:execute) \
68
+ .with({}) \
69
+ .and_return(context)
70
+ expect(AnotherAction).to receive(:execute) \
71
+ .with(context) \
72
+ .and_return(context)
73
+
74
+ expect { AnOrganizer.do_something_with_no_starting_context } \
75
+ .not_to raise_error
58
76
  end
59
77
  end
60
78
  end
@@ -9,8 +9,8 @@ describe CalculatesOrderTaxAction do
9
9
  end
10
10
 
11
11
  it "calculates the tax based on the tax percentage" do
12
- order.stub(:total => 100)
13
- order.should_receive(:tax=).with 7.2
12
+ allow(order).to receive_messages(:total => 100)
13
+ expect(order).to receive(:tax=).with 7.2
14
14
  CalculatesOrderTaxAction.execute(context)
15
15
  end
16
16
  end
@@ -9,16 +9,16 @@ describe CalculatesTax do
9
9
  let(:context) { double('context') }
10
10
 
11
11
  it "calls the actions in order" do
12
- ::LightService::Context.stub(:make) \
12
+ allow(::LightService::Context).to receive(:make) \
13
13
  .with(:order => order) \
14
14
  .and_return context
15
15
 
16
- LooksUpTaxPercentageAction.stub(:execute).with(context).and_return context
17
- CalculatesOrderTaxAction.stub(:execute).with(context).and_return context
18
- ProvidesFreeShippingAction.stub(:execute).with(context).and_return context
16
+ allow(LooksUpTaxPercentageAction).to receive(:execute).with(context).and_return context
17
+ allow(CalculatesOrderTaxAction).to receive(:execute).with(context).and_return context
18
+ allow(ProvidesFreeShippingAction).to receive(:execute).with(context).and_return context
19
19
 
20
20
  result = CalculatesTax.for_order(order)
21
21
 
22
- result.should eq context
22
+ expect(result).to eq context
23
23
  end
24
24
  end
@@ -7,8 +7,8 @@ describe LooksUpTaxPercentageAction do
7
7
  let(:region) { double('region') }
8
8
  let(:order) do
9
9
  order = double('order')
10
- order.stub(:region => region)
11
- order.stub(:total => 200)
10
+ allow(order).to receive_messages(:region => region)
11
+ allow(order).to receive_messages(:total => 200)
12
12
  order
13
13
  end
14
14
  let(:context) do
@@ -19,35 +19,35 @@ describe LooksUpTaxPercentageAction do
19
19
 
20
20
  context "when the tax_ranges were not found" do
21
21
  it "sets the context to failure" do
22
- TaxRange.stub(:for_region).with(region).and_return nil
22
+ allow(TaxRange).to receive(:for_region).with(region).and_return nil
23
23
  LooksUpTaxPercentageAction.execute(context)
24
24
 
25
- context.should be_failure
26
- context.message.should eq "The tax ranges were not found"
25
+ expect(context).to be_failure
26
+ expect(context.message).to eq "The tax ranges were not found"
27
27
  end
28
28
  end
29
29
 
30
30
  context "when the tax_percentage is not found" do
31
31
  it "sets the context to failure" do
32
- TaxRange.stub(:for_region).with(region).and_return tax_ranges
33
- tax_ranges.stub(:for_total => nil)
32
+ allow(TaxRange).to receive(:for_region).with(region).and_return tax_ranges
33
+ allow(tax_ranges).to receive_messages(:for_total => nil)
34
34
 
35
35
  LooksUpTaxPercentageAction.execute(context)
36
36
 
37
- context.should be_failure
38
- context.message.should eq "The tax percentage was not found"
37
+ expect(context).to be_failure
38
+ expect(context.message).to eq "The tax percentage was not found"
39
39
  end
40
40
  end
41
41
 
42
42
  context "when the tax_percentage is found" do
43
43
  it "sets the tax_percentage in context" do
44
- TaxRange.stub(:for_region).with(region).and_return tax_ranges
45
- tax_ranges.stub(:for_total => 25)
44
+ allow(TaxRange).to receive(:for_region).with(region).and_return tax_ranges
45
+ allow(tax_ranges).to receive_messages(:for_total => 25)
46
46
 
47
47
  LooksUpTaxPercentageAction.execute(context)
48
48
 
49
- context.should be_success
50
- context.fetch(:tax_percentage).should eq 25
49
+ expect(context).to be_success
50
+ expect(context.fetch(:tax_percentage)).to eq 25
51
51
  end
52
52
  end
53
53
  end
@@ -10,8 +10,8 @@ describe ProvidesFreeShippingAction do
10
10
 
11
11
  context "when the order total with tax is > 200" do
12
12
  specify "order gets free shipping" do
13
- order.stub(:total_with_tax => 201)
14
- order.should_receive(:provide_free_shipping!)
13
+ allow(order).to receive_messages(:total_with_tax => 201)
14
+ expect(order).to receive(:provide_free_shipping!)
15
15
 
16
16
  ProvidesFreeShippingAction.execute(context)
17
17
  end
@@ -19,8 +19,8 @@ describe ProvidesFreeShippingAction do
19
19
 
20
20
  context "when the order total with tax is <= 200" do
21
21
  specify "order gets free shipping" do
22
- order.stub(:total_with_tax => 200)
23
- order.should_not_receive(:provide_free_shipping!)
22
+ allow(order).to receive_messages(:total_with_tax => 200)
23
+ expect(order).not_to receive(:provide_free_shipping!)
24
24
 
25
25
  ProvidesFreeShippingAction.execute(context)
26
26
  end
@@ -3,3 +3,4 @@ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
3
 
4
4
  require 'light-service'
5
5
  require 'ostruct'
6
+ require 'rspec/its'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Attila Domokos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-23 00:00:00.000000000 Z
11
+ date: 2014-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '3.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-its
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: simplecov
29
43
  requirement: !ruby/object:Gem::Requirement