light-service 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43b8226d18386ca933714af81639598fdff2332d
4
- data.tar.gz: e7112eea09148ee8f06412e7e2f61cea18375d05
3
+ metadata.gz: 65825f46d1fd42f09f7e31d1c6237936c848aa7f
4
+ data.tar.gz: cc2963f8195cb9e33b1bbb0fad85864d9249e29d
5
5
  SHA512:
6
- metadata.gz: 6c772140e17955b44a37b923ed4f09a110e5d21da479e2ba44c8a6b0f81b8f647808c722932b56b407bbee781bd57c33981cb36b403f24ee29f746e184410ad1
7
- data.tar.gz: 741bf14615577c1d9fe518e5835e3a4a9bde8879810ce9765983db65195afbd5d63e598fe07335bbe47158a9779d9fe23e0f890560dc19bffe5e3bba3ba84784
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(self.order.region)
87
- self.tax_percentage = 0
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
- self.tax_percentage = tax_ranges.for_total(self.order.total)
91
+ context.tax_percentage = tax_ranges.for_total(context.order.total)
92
92
 
93
- next context if object_is_nil?(self.tax_percentage, context, 'The tax percentage was not found')
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 = self.baz + 2
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
- self.bar = self.baz + 2
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
 
@@ -21,12 +21,11 @@ module LightService
21
21
 
22
22
  ContextKeyVerifier.verify_expected_keys_are_in_context(action_context, @_expected_keys)
23
23
 
24
- define_expectations_readers(context)
25
- define_promises_accessors(context)
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
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -18,10 +18,7 @@ class AddsOneAction
18
18
  promises :number
19
19
 
20
20
  executed do |context|
21
- number = self.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 = self.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 = self.number
48
- number += 3
49
-
50
- self.number = number
41
+ context.number += 3
51
42
  end
52
43
  end
53
44
 
@@ -8,7 +8,7 @@ module LightService
8
8
  promises :milk_tea
9
9
 
10
10
  executed do |context|
11
- context[:milk_tea] = "#{self.tea} - #{milk}"
11
+ context.milk_tea = "#{context.tea} - #{context.milk}"
12
12
  end
13
13
  end
14
14
 
@@ -45,8 +45,8 @@ module LightService
45
45
  expects :baz
46
46
 
47
47
  executed do |context|
48
- # Notice how I use `self.baz` here
49
- bar = self.baz + 2
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 `self.bar` here
85
- self.bar = self.baz + 2
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] = "#{self.tea} - #{self.milk}"
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
- self.milk_tea = "#{self.tea} - #{self.milk}"
32
- self.milk_tea += " hello"
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
- self.milk_tea = nil
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
@@ -3,6 +3,6 @@ class CalculatesOrderTaxAction
3
3
  expects :order, :tax_percentage
4
4
 
5
5
  executed do |context|
6
- order.tax = (order.total * (tax_percentage/100)).round(2)
6
+ context.order.tax = (context.order.total * (context.tax_percentage/100)).round(2)
7
7
  end
8
8
  end
@@ -4,14 +4,14 @@ class LooksUpTaxPercentageAction
4
4
  promises :tax_percentage
5
5
 
6
6
  executed do |context|
7
- tax_ranges = TaxRange.for_region(self.order.region)
8
- self.tax_percentage = 0
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
- self.tax_percentage = tax_ranges.for_total(self.order.total)
12
+ context.tax_percentage = tax_ranges.for_total(context.order.total)
13
13
 
14
- next context if object_is_nil?(self.tax_percentage, context, 'The tax percentage was not found')
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)
@@ -3,6 +3,7 @@ class ProvidesFreeShippingAction
3
3
  expects :order
4
4
 
5
5
  executed do |context|
6
+ order = context.order
6
7
  if order.total_with_tax > 200
7
8
  order.provide_free_shipping!
8
9
  end
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.2
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-22 00:00:00.000000000 Z
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec