light-service 0.3.0 → 0.3.1

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: 713b37be5e541d42ebed24537f656289ed6d5ab6
4
- data.tar.gz: dbf4a703cbfa397562d59a1f61ca8c08136a7111
3
+ metadata.gz: 5dbadd45523f6245d302d195937328fe1819f9b7
4
+ data.tar.gz: fa3836bd03de541b3d24cc38e9a13800b3c35381
5
5
  SHA512:
6
- metadata.gz: 5040578df5d4f0bbce8f530a31f67fe1baacdd12658d3c20b4e936dc0f53511efe666858a491134d604ed246ff76d119c09cebfbbf802bb4ce95aeaad69dff0f
7
- data.tar.gz: aa4adc93839de21f98b0e275da0d28e0dfbc5fe1f1114ac2bdb14b267bbff6208a19754357ed5c713861cba542229ad0ce0ac9033469b5f41c14ee887f49bdfe
6
+ metadata.gz: c8356b2a0cd6a0ea403f470352c6c686473340752ac6d35f7bd87893e06167032b51177c4ca4d261da819e4609106034838fbbd0a56d5ec5bddaf8731639959c
7
+ data.tar.gz: 23696cd73809aaa2ddd2e76c5d2f3d3fb3b2e34640210c830b2404b4132fad2badc816531e54322a3b7d673b1767586e10d0f331ef7d1e0a2bc218ef1c1c3b2a
data/.travis.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.9.3"
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
4
6
  # uncomment this line if your project needs to run something other than `rake`:
5
7
  script: bundle exec rspec spec
data/README.md CHANGED
@@ -200,6 +200,43 @@ end
200
200
 
201
201
  Take a look at [this spec](spec/action_expects_and_promises_spec.rb) to see the refactoring in action.
202
202
 
203
+ ## Error Codes
204
+
205
+ You can do a more structured error handling by using error codes in the context.
206
+ If something goes wrong in your actions, you can fail the process by setting the context to failure:
207
+
208
+ ```ruby
209
+ class FooAction
210
+ include LightService::Action
211
+
212
+ executed do |context|
213
+ context.fail!("I don't like what happened here.")
214
+ end
215
+ end
216
+ ```
217
+
218
+ However, you might need to handle the errors coming from your action pipeline differently.
219
+ Using an error code can help you check what type of expected error occurred from the organizer
220
+ or action invocation point.
221
+
222
+ ```ruby
223
+ class FooAction
224
+ include LightService::Action
225
+
226
+ executed do |context|
227
+ unless (service_call.success?)
228
+ context.fail!("Service call failed", 1001)
229
+ end
230
+
231
+ # Do something else
232
+
233
+ unless (entity.save)
234
+ context.fail!("Saving the entity failed", 2001)
235
+ end
236
+ end
237
+ end
238
+ ```
239
+
203
240
  ## Requirements
204
241
 
205
242
  This gem requires ruby 1.9.x
@@ -236,6 +273,9 @@ For further examples, please visit the project's [Wiki](https://github.com/adomo
236
273
  Huge thanks to the [contributors](https://github.com/adomokos/light-service/graphs/contributors)!
237
274
 
238
275
  ## Release Notes
276
+ ### 0.3.1
277
+ * Adding [error codes](https://github.com/adomokos/light-service#error-codes) to the context
278
+
239
279
  ### 0.3.0
240
280
  * Adding the `expects` and `promises` macros - Read more about it in [this blog post](http://www.adomokos.com/2014/05/expects-and-promises-in-lightservice.html)
241
281
 
@@ -5,10 +5,10 @@ module LightService
5
5
  end
6
6
 
7
7
  class Context < Hash
8
- attr_accessor :outcome, :message
8
+ attr_accessor :outcome, :message, :error_code
9
9
 
10
- def initialize(context={}, outcome=::LightService::Outcomes::SUCCESS, message='')
11
- @outcome, @message = outcome, message
10
+ def initialize(context={}, outcome=::LightService::Outcomes::SUCCESS, message='', error_code=nil)
11
+ @outcome, @message, @error_code = outcome, message, error_code
12
12
  context.to_hash.each {|k,v| self[k] = v}
13
13
  self
14
14
  end
@@ -19,7 +19,7 @@ module LightService
19
19
  end
20
20
 
21
21
  return context if context.is_a?(Context)
22
- self.new(context, ::LightService::Outcomes::SUCCESS, '')
22
+ self.new(context)
23
23
  end
24
24
 
25
25
  def add_to_context(values)
@@ -60,8 +60,13 @@ module LightService
60
60
  fail!(message)
61
61
  end
62
62
 
63
- def fail!(message=nil)
64
- @message = message
63
+ def fail!(options=nil)
64
+ if options.is_a? Hash
65
+ @message = options[:message]
66
+ @error_code = options[:error_code]
67
+ else
68
+ @message = options
69
+ end
65
70
  @outcome = ::LightService::Outcomes::FAILURE
66
71
  end
67
72
 
@@ -30,7 +30,7 @@ module LightService
30
30
  end
31
31
 
32
32
  def format_keys(keys)
33
- keys.map{|k| ":#{k}"}.join(', ')
33
+ keys.map { |k| ":#{k}"}.join(', ')
34
34
  end
35
35
  end
36
36
  end
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/spec/context_spec.rb CHANGED
@@ -13,6 +13,7 @@ module LightService
13
13
  context "with a hash" do
14
14
  it "has the hash values" do
15
15
  context = Context.make(:one => 1)
16
+
16
17
  expect(context[:one]).to eq(1)
17
18
  end
18
19
  end
@@ -20,6 +21,7 @@ module LightService
20
21
  context "with FAILURE" do
21
22
  it "is failed" do
22
23
  context = Context.new({}, ::LightService::Outcomes::FAILURE, '')
24
+
23
25
  expect(context).to be_failure
24
26
  end
25
27
  end
@@ -33,49 +35,73 @@ module LightService
33
35
 
34
36
  it "can be asked for success?" do
35
37
  context = Context.new({}, ::LightService::Outcomes::SUCCESS)
36
- expect(context.success?).to be_true
38
+
39
+ expect(context).to be_success
37
40
  end
38
41
 
39
42
  it "can be asked for failure?" do
40
43
  context = Context.new({}, ::LightService::Outcomes::FAILURE)
41
- expect(context.failure?).to be_true
44
+
45
+ expect(context).to be_failure
42
46
  end
43
47
 
44
48
  it "can be asked for skip_all?" do
45
49
  context = Context.make
46
50
  context.skip_all!
51
+
47
52
  expect(context.skip_all?).to be_true
48
53
  end
49
54
 
50
55
  it "can be pushed into a SUCCESS state" do
51
56
  context = Context.make
52
57
  context.succeed!("a happy end")
58
+
53
59
  expect(context).to be_success
54
60
  end
55
61
 
56
62
  it "can be pushed into a SUCCESS state without a message" do
57
63
  context = Context.make
58
64
  context.succeed!
65
+
59
66
  expect(context).to be_success
60
67
  expect(context.message).to be_nil
61
68
  end
62
69
 
63
- it "can be pushed into a FAILURE state" do
70
+ it "can be pushed into a FAILURE state without a message" do
71
+ context = Context.make
72
+ context.fail!
73
+
74
+ expect(context).to be_failure
75
+ expect(context.message).to be_nil
76
+ end
77
+
78
+ it "can be pushed into a FAILURE state with a message" do
64
79
  context = Context.make
65
80
  context.fail!("a sad end")
81
+
66
82
  expect(context).to be_failure
67
83
  end
68
84
 
69
- it "can be pushed into a FAILURE state without a message" do
85
+ it "can be pushed into a FAILURE state with a message in an options hash" do
70
86
  context = Context.make
71
- context.fail!
87
+ context.fail!(:message => "a sad end")
88
+
72
89
  expect(context).to be_failure
73
- expect(context.message).to be_nil
90
+ expect(context.error_code).to be_nil
91
+ end
92
+
93
+ it "can be pushed into a FAILURE state with an error code in an options hash" do
94
+ context = Context.make
95
+ context.fail!(:error_code => 10005)
96
+
97
+ expect(context).to be_failure
98
+ expect(context.error_code).to eq(10005)
74
99
  end
75
100
 
76
101
  it "can set a flag to skip all subsequent actions" do
77
102
  context = Context.make
78
103
  context.skip_all!
104
+
79
105
  expect(context).to be_skip_all
80
106
  end
81
107
 
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.0
4
+ version: 0.3.1
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-15 00:00:00.000000000 Z
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec