light-service 0.3.2 → 0.3.3
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 +4 -4
- data/README.md +12 -8
- data/lib/light-service/action.rb +2 -30
- data/lib/light-service/context.rb +8 -0
- data/lib/light-service/organizer.rb +1 -5
- data/lib/light-service/version.rb +1 -1
- data/spec/acceptance/add_numbers_spec.rb +3 -12
- data/spec/action_expected_keys_spec.rb +1 -1
- data/spec/action_expects_and_promises_spec.rb +4 -4
- data/spec/action_promised_keys_spec.rb +4 -4
- data/spec/sample/tax/calculates_order_tax_action.rb +1 -1
- data/spec/sample/tax/looks_up_tax_percentage_action.rb +4 -4
- data/spec/sample/tax/provides_free_shipping_action.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65825f46d1fd42f09f7e31d1c6237936c848aa7f
|
4
|
+
data.tar.gz: cc2963f8195cb9e33b1bbb0fad85864d9249e29d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b1cd806096d97fef2f82e8310affaa87102377ce6e318b613f8035ebe542731bcf5331d38e9a54fa748207cc4579da165c55a4a3d95cacd20988200034d52fc
|
7
|
+
data.tar.gz: 5ab53d49c280764ef5cf6e1097ea103059db80d018bef4fc5533baeb80878c010c8c6852d6f9a2ad7abdba612d54e07031894f166ac072078d62e8ded2ef866f
|
data/README.md
CHANGED
@@ -83,14 +83,14 @@ class LooksUpTaxPercentageAction
|
|
83
83
|
promises :tax_percentage
|
84
84
|
|
85
85
|
executed do |context|
|
86
|
-
tax_ranges = TaxRange.for_region(
|
87
|
-
|
86
|
+
tax_ranges = TaxRange.for_region(context.order.region)
|
87
|
+
context.tax_percentage = 0
|
88
88
|
|
89
89
|
next context if object_is_nil?(tax_ranges, context, 'The tax ranges were not found')
|
90
90
|
|
91
|
-
|
91
|
+
context.tax_percentage = tax_ranges.for_total(context.order.total)
|
92
92
|
|
93
|
-
next context if object_is_nil?(
|
93
|
+
next context if object_is_nil?(context.tax_percentage, context, 'The tax percentage was not found')
|
94
94
|
end
|
95
95
|
|
96
96
|
def self.object_is_nil?(object, context, message)
|
@@ -177,7 +177,7 @@ class FooAction
|
|
177
177
|
promises :bar
|
178
178
|
|
179
179
|
executed do |context|
|
180
|
-
bar =
|
180
|
+
bar = context.baz + 2
|
181
181
|
context[:bar] = bar
|
182
182
|
end
|
183
183
|
end
|
@@ -193,7 +193,7 @@ class FooAction
|
|
193
193
|
promises :bar
|
194
194
|
|
195
195
|
executed do |context|
|
196
|
-
|
196
|
+
context.bar = context.baz + 2
|
197
197
|
end
|
198
198
|
end
|
199
199
|
```
|
@@ -257,7 +257,7 @@ Or install it yourself as:
|
|
257
257
|
|
258
258
|
## Usage
|
259
259
|
|
260
|
-
Based on the refactoring example above, just create an organizer object that calls the
|
260
|
+
Based on the refactoring example above, just create an organizer object that calls the
|
261
261
|
actions in order and write code for the actions. That's it.
|
262
262
|
|
263
263
|
For further examples, please visit the project's [Wiki](https://github.com/adomokos/light-service/wiki).
|
@@ -273,8 +273,12 @@ 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.3
|
277
|
+
* Switching the promises and expects keys accessors from Action to Context
|
278
|
+
|
276
279
|
### 0.3.2
|
277
|
-
* Fixing documentation and using separate arguments instead of a hash when setting the context to failure
|
280
|
+
* Fixing documentation and using separate arguments instead of a hash when setting the context to failure with error code
|
281
|
+
|
278
282
|
### 0.3.1
|
279
283
|
* Adding [error codes](https://github.com/adomokos/light-service#error-codes) to the context
|
280
284
|
|
data/lib/light-service/action.rb
CHANGED
@@ -21,12 +21,11 @@ module LightService
|
|
21
21
|
|
22
22
|
ContextKeyVerifier.verify_expected_keys_are_in_context(action_context, @_expected_keys)
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
action_context.define_accessor_methods_for_keys(@_expected_keys)
|
25
|
+
action_context.define_accessor_methods_for_keys(@_promised_keys)
|
26
26
|
|
27
27
|
yield(action_context)
|
28
28
|
|
29
|
-
set_promises_in_context(action_context)
|
30
29
|
ContextKeyVerifier.verify_promised_keys_are_in_context(action_context, @_promised_keys)
|
31
30
|
end
|
32
31
|
end
|
@@ -41,33 +40,6 @@ module LightService
|
|
41
40
|
LightService::Context.make(context)
|
42
41
|
end
|
43
42
|
|
44
|
-
def define_expectations_readers(context)
|
45
|
-
context.keys.each do |key|
|
46
|
-
define_singleton_method key do
|
47
|
-
context.fetch(key)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def define_promises_accessors(context)
|
53
|
-
return unless @_promised_keys
|
54
|
-
@_promised_keys.each do |key|
|
55
|
-
instance_variable_set("@#{key}", VALUE_NOT_SET)
|
56
|
-
self.class.send(:attr_accessor, key)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def set_promises_in_context(context)
|
61
|
-
return unless @_promised_keys
|
62
|
-
@_promised_keys.each do |key|
|
63
|
-
value = instance_variable_get("@#{key}")
|
64
|
-
next if value == VALUE_NOT_SET
|
65
|
-
context[key] = value
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
VALUE_NOT_SET = "___value_was_not_set___"
|
70
43
|
end
|
71
|
-
|
72
44
|
end
|
73
45
|
end
|
@@ -74,5 +74,13 @@ module LightService
|
|
74
74
|
def stop_processing?
|
75
75
|
failure? || skip_all?
|
76
76
|
end
|
77
|
+
|
78
|
+
def define_accessor_methods_for_keys(keys)
|
79
|
+
return if keys.nil?
|
80
|
+
keys.each do |key|
|
81
|
+
define_singleton_method("#{key}") { self.fetch(key) }
|
82
|
+
define_singleton_method("#{key}=") { |value| self[key] = value }
|
83
|
+
end
|
84
|
+
end
|
77
85
|
end
|
78
86
|
end
|
@@ -17,11 +17,7 @@ module LightService
|
|
17
17
|
|
18
18
|
def reduce(*actions)
|
19
19
|
raise "No action(s) were provided" if actions.empty?
|
20
|
-
|
21
|
-
if actions.is_a? Array
|
22
|
-
actions.flatten!
|
23
|
-
end
|
24
|
-
|
20
|
+
actions.flatten!
|
25
21
|
actions.reduce(@context) { |context, action| action.execute(context) }
|
26
22
|
end
|
27
23
|
end
|
@@ -18,10 +18,7 @@ class AddsOneAction
|
|
18
18
|
promises :number
|
19
19
|
|
20
20
|
executed do |context|
|
21
|
-
number
|
22
|
-
number += 1
|
23
|
-
|
24
|
-
self.number = number
|
21
|
+
context.number += 1
|
25
22
|
end
|
26
23
|
end
|
27
24
|
|
@@ -31,10 +28,7 @@ class AddsTwoAction
|
|
31
28
|
promises :number
|
32
29
|
|
33
30
|
executed do |context|
|
34
|
-
number
|
35
|
-
number += 2
|
36
|
-
|
37
|
-
self.number = number
|
31
|
+
context.number += 2
|
38
32
|
end
|
39
33
|
end
|
40
34
|
|
@@ -44,10 +38,7 @@ class AddsThreeAction
|
|
44
38
|
promises :number
|
45
39
|
|
46
40
|
executed do |context|
|
47
|
-
number
|
48
|
-
number += 3
|
49
|
-
|
50
|
-
self.number = number
|
41
|
+
context.number += 3
|
51
42
|
end
|
52
43
|
end
|
53
44
|
|
@@ -45,8 +45,8 @@ module LightService
|
|
45
45
|
expects :baz
|
46
46
|
|
47
47
|
executed do |context|
|
48
|
-
# Notice how I use `
|
49
|
-
bar =
|
48
|
+
# Notice how I use `context.baz` here
|
49
|
+
bar = context.baz + 2
|
50
50
|
context[:bar] = bar
|
51
51
|
end
|
52
52
|
end
|
@@ -81,8 +81,8 @@ module LightService
|
|
81
81
|
promises :bar
|
82
82
|
|
83
83
|
executed do |context|
|
84
|
-
# Notice how I use `
|
85
|
-
|
84
|
+
# Notice how I use `context.bar` here
|
85
|
+
context.bar = context.baz + 2
|
86
86
|
end
|
87
87
|
end
|
88
88
|
it "puts the value through the accessor into the context" do
|
@@ -8,7 +8,7 @@ module LightService
|
|
8
8
|
promises :milk_tea, :something_else
|
9
9
|
|
10
10
|
executed do |context|
|
11
|
-
context[:some_tea] = "#{
|
11
|
+
context[:some_tea] = "#{context.tea} - #{context.milk}"
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -28,8 +28,8 @@ module LightService
|
|
28
28
|
promises :milk_tea
|
29
29
|
|
30
30
|
executed do |context|
|
31
|
-
|
32
|
-
|
31
|
+
context.milk_tea = "#{context.tea} - #{context.milk}"
|
32
|
+
context.milk_tea += " hello"
|
33
33
|
end
|
34
34
|
end
|
35
35
|
it "sets in the context if it was set with not nil" do
|
@@ -45,7 +45,7 @@ module LightService
|
|
45
45
|
promises :milk_tea
|
46
46
|
|
47
47
|
executed do |context|
|
48
|
-
|
48
|
+
context.milk_tea = nil
|
49
49
|
end
|
50
50
|
end
|
51
51
|
it "sets in the context if it was set with nil" do
|
@@ -4,14 +4,14 @@ class LooksUpTaxPercentageAction
|
|
4
4
|
promises :tax_percentage
|
5
5
|
|
6
6
|
executed do |context|
|
7
|
-
tax_ranges = TaxRange.for_region(
|
8
|
-
|
7
|
+
tax_ranges = TaxRange.for_region(context.order.region)
|
8
|
+
context.tax_percentage = 0
|
9
9
|
|
10
10
|
next context if object_is_nil?(tax_ranges, context, 'The tax ranges were not found')
|
11
11
|
|
12
|
-
|
12
|
+
context.tax_percentage = tax_ranges.for_total(context.order.total)
|
13
13
|
|
14
|
-
next context if object_is_nil?(
|
14
|
+
next context if object_is_nil?(context.tax_percentage, context, 'The tax percentage was not found')
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.object_is_nil?(object, context, message)
|
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.
|
4
|
+
version: 0.3.3
|
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-
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|