light-service 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -60,16 +60,15 @@ and executes them one-by-one. Then you need to create the actions which will onl
60
60
 
61
61
  ```ruby
62
62
  class CalculatesTax
63
- def self.for_order(order)
64
- context = LightService::Context.make(:order => order)
65
-
66
- [
67
- LooksUpTaxPercentageAction,
68
- CalculatesOrderTaxAction,
69
- ProvidesFreeShippingAction
70
- ].each{ |action| action.execute(context) }
63
+ extend LightService::Organizer
71
64
 
72
- context
65
+ def self.for_order(order)
66
+ with(order: order).reduce \
67
+ [
68
+ LooksUpTaxPercentageAction,
69
+ CalculatesOrderTaxAction,
70
+ ProvidesFreeShippingAction
71
+ ]
73
72
  end
74
73
  end
75
74
 
@@ -0,0 +1,13 @@
1
+ module LightService
2
+ module Organizer
3
+ protected
4
+ def with(data = {})
5
+ @context = LightService::Context.make(data)
6
+ self
7
+ end
8
+
9
+ def reduce(actions=[])
10
+ actions.reduce(@context) { |context, action| action.execute(context) }
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/light-service.rb CHANGED
@@ -2,3 +2,4 @@ require "light-service/version"
2
2
 
3
3
  require 'light-service/context'
4
4
  require 'light-service/action'
5
+ require 'light-service/organizer'
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ class Organizer
4
+ extend LightService::Organizer
5
+
6
+ def self.add_number(number)
7
+ with(number: number).reduce \
8
+ [
9
+ AddsOneAction,
10
+ AddsTwoAction,
11
+ AddsThreeAction
12
+ ]
13
+ end
14
+ end
15
+
16
+ class AddsOneAction
17
+ include LightService::Action
18
+
19
+ executed do |context|
20
+ number = context.fetch :number
21
+ number += 1
22
+
23
+ context[:number] = number
24
+ end
25
+ end
26
+
27
+ class AddsTwoAction
28
+ include LightService::Action
29
+
30
+ executed do |context|
31
+ number = context.fetch :number
32
+ number += 2
33
+
34
+ context[:number] = number
35
+ end
36
+ end
37
+
38
+ class AddsThreeAction
39
+ include LightService::Action
40
+
41
+ executed do |context|
42
+ number = context.fetch :number
43
+ number += 3
44
+
45
+ context[:number] = number
46
+ end
47
+ end
48
+
49
+ describe Organizer do
50
+ it "Adds 1 2 3 and through to 1" do
51
+ result = Organizer.add_number 1
52
+ result.fetch(:number).should == 7
53
+ end
54
+ end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require_relative 'sample/calculates_order_tax_action'
2
+ require_relative 'tax/calculates_order_tax_action'
3
3
 
4
4
  describe CalculatesOrderTaxAction do
5
5
  let(:order) { double('order') }
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require_relative 'tax/calculates_tax'
3
+ require_relative 'tax/looks_up_tax_percentage_action'
4
+ require_relative 'tax/calculates_order_tax_action'
5
+ require_relative 'tax/provides_free_shipping_action'
6
+
7
+ describe CalculatesTax do
8
+ let(:order) { double('order') }
9
+ let(:context) { double('context') }
10
+
11
+ it "calls the actions in order" do
12
+ ::LightService::Context.stub(:make) \
13
+ .with(:order => order) \
14
+ .and_return context
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
19
+
20
+ result = CalculatesTax.for_order(order)
21
+
22
+ result.should eq context
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require_relative 'sample/looks_up_tax_percentage_action'
2
+ require_relative 'tax/looks_up_tax_percentage_action'
3
3
 
4
4
  class TaxRange; end
5
5
 
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require_relative 'sample/provides_free_shipping_action'
2
+ require_relative 'tax/provides_free_shipping_action'
3
3
 
4
4
  describe ProvidesFreeShippingAction do
5
5
  let(:order) { double('order') }
@@ -0,0 +1,12 @@
1
+ class CalculatesTax
2
+ extend LightService::Organizer
3
+
4
+ def self.for_order(order)
5
+ with(order: order).reduce \
6
+ [
7
+ LooksUpTaxPercentageAction,
8
+ CalculatesOrderTaxAction,
9
+ ProvidesFreeShippingAction
10
+ ]
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-28 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -60,19 +60,21 @@ files:
60
60
  - lib/light-service.rb
61
61
  - lib/light-service/action.rb
62
62
  - lib/light-service/context.rb
63
+ - lib/light-service/organizer.rb
63
64
  - lib/light-service/version.rb
64
65
  - light-service.gemspec
65
66
  - light-service.png
66
- - spec/acceptance/calculates_order_tax_action_spec.rb
67
- - spec/acceptance/calculates_tax_spec.rb
68
- - spec/acceptance/looks_up_tax_percentage_action.rb
69
- - spec/acceptance/provides_free_shipping_action_spec.rb
70
- - spec/acceptance/sample/calculates_order_tax_action.rb
71
- - spec/acceptance/sample/calculates_tax.rb
72
- - spec/acceptance/sample/looks_up_tax_percentage_action.rb
73
- - spec/acceptance/sample/provides_free_shipping_action.rb
67
+ - spec/acceptance/add_numbers_spec.rb
74
68
  - spec/action_base_spec.rb
75
69
  - spec/context_spec.rb
70
+ - spec/sample/calculates_order_tax_action_spec.rb
71
+ - spec/sample/calculates_tax_spec.rb
72
+ - spec/sample/looks_up_tax_percentage_action.rb
73
+ - spec/sample/provides_free_shipping_action_spec.rb
74
+ - spec/sample/tax/calculates_order_tax_action.rb
75
+ - spec/sample/tax/calculates_tax.rb
76
+ - spec/sample/tax/looks_up_tax_percentage_action.rb
77
+ - spec/sample/tax/provides_free_shipping_action.rb
76
78
  - spec/spec_helper.rb
77
79
  homepage: ''
78
80
  licenses: []
@@ -94,19 +96,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  version: '0'
95
97
  requirements: []
96
98
  rubyforge_project:
97
- rubygems_version: 1.8.24
99
+ rubygems_version: 1.8.23
98
100
  signing_key:
99
101
  specification_version: 3
100
102
  summary: A service skeleton with an emphasis on simplicity
101
103
  test_files:
102
- - spec/acceptance/calculates_order_tax_action_spec.rb
103
- - spec/acceptance/calculates_tax_spec.rb
104
- - spec/acceptance/looks_up_tax_percentage_action.rb
105
- - spec/acceptance/provides_free_shipping_action_spec.rb
106
- - spec/acceptance/sample/calculates_order_tax_action.rb
107
- - spec/acceptance/sample/calculates_tax.rb
108
- - spec/acceptance/sample/looks_up_tax_percentage_action.rb
109
- - spec/acceptance/sample/provides_free_shipping_action.rb
104
+ - spec/acceptance/add_numbers_spec.rb
110
105
  - spec/action_base_spec.rb
111
106
  - spec/context_spec.rb
107
+ - spec/sample/calculates_order_tax_action_spec.rb
108
+ - spec/sample/calculates_tax_spec.rb
109
+ - spec/sample/looks_up_tax_percentage_action.rb
110
+ - spec/sample/provides_free_shipping_action_spec.rb
111
+ - spec/sample/tax/calculates_order_tax_action.rb
112
+ - spec/sample/tax/calculates_tax.rb
113
+ - spec/sample/tax/looks_up_tax_percentage_action.rb
114
+ - spec/sample/tax/provides_free_shipping_action.rb
112
115
  - spec/spec_helper.rb
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
- require_relative 'sample/calculates_tax'
3
- require_relative 'sample/looks_up_tax_percentage_action'
4
- require_relative 'sample/calculates_order_tax_action'
5
- require_relative 'sample/provides_free_shipping_action'
6
-
7
- describe CalculatesTax do
8
- let(:order) { double('order') }
9
- let(:context) { double('context') }
10
-
11
- it "calls the actions in order" do
12
- ::LightService::Context.stub(:make) \
13
- .with(:order => order) \
14
- .and_return context
15
-
16
- LooksUpTaxPercentageAction.stub(:execute).with(context)
17
- CalculatesOrderTaxAction.stub(:execute).with(context)
18
- ProvidesFreeShippingAction.stub(:execute).with(context)
19
-
20
- result = CalculatesTax.for_order(order)
21
- result.should eq context
22
- end
23
- end
@@ -1,13 +0,0 @@
1
- class CalculatesTax
2
- def self.for_order(order)
3
- context = ::LightService::Context.make(:order => order)
4
-
5
- [
6
- LooksUpTaxPercentageAction,
7
- CalculatesOrderTaxAction,
8
- ProvidesFreeShippingAction
9
- ].each{ |action| action.execute(context) }
10
-
11
- context
12
- end
13
- end