light-service 0.1.0 → 0.2.0

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: 18e898c817a11b13398d6e66dce89c0809ba98d3
4
- data.tar.gz: f305ad48b609b5f52bab3ce769276bd5b13eeef6
3
+ metadata.gz: d6e076aae96c52a5762908422611819327dde2bc
4
+ data.tar.gz: 97f23597368c33f4e5fca9d829b286ae73db14d6
5
5
  SHA512:
6
- metadata.gz: fb9721235e2c14b1af534487fb8060d08a1df789a539b178ea609d078e12196019278d1873afeed9d9bf4577341956133e7106254acfbe0186f5674c5d49c059
7
- data.tar.gz: db7ac429e532fce47d5f82d507244d96d9d9ea630ad00cc4e27df20d6b0a664ef999f5c89ae8f8819712835b76a3bb27abb73818d4313ac66288fb4f28fba57b
6
+ metadata.gz: d6aeba0dd3f98960c6e5b13a878018afb5e7f2c6a50b0254dfcadf13b6045c649df51f29c07fd8b023c4f3d075551a6923d63919f3870c2cebc3ce2bf05067ed
7
+ data.tar.gz: f9cf3c1bff5df5411bee9240de31192d8a63f42175335905fd5c62d5e1b11d99daf7d824cdd1db8f69348982d17cda18a9a6db77fc3b2d3bbab9f5c73a694a09
data/README.md CHANGED
@@ -97,7 +97,7 @@ class LooksUpTaxPercentageAction
97
97
 
98
98
  def self.object_is_nil?(object, context, message)
99
99
  if object.nil?
100
- context.set_failure!(message)
100
+ context.fail!(message)
101
101
  return true
102
102
  end
103
103
 
@@ -189,9 +189,9 @@ Huge thanks to the [contributors](https://github.com/adomokos/light-service/grap
189
189
 
190
190
  ## Release Notes
191
191
 
192
- ### 0.0.12
193
- * You can invoke the actions by passing in a [list of actions](https://github.com/adomokos/light-service/blob/c393995a6bc52f1f67535cd242e144350057431c/spec/organizer_spec.rb#L16) or an array of actions.
194
- * `Context#make` will not create a new Context if it's already a Context. Thanks to [@ramontayag](https://github.com/ramontayag) and [@padi](https://github.com/padi) for their commit.
192
+ ### 0.2.0
193
+ * [Renaming](https://github.com/adomokos/light-service/commit/8d40ff7d393a157a8a558f9e4e021b8731550834) the `set_success!` and `set_failure!` methods to `succeed!` and `fail!`.
194
+ * [Throwing](https://github.com/adomokos/light-service/commit/5ef315b8aeeafc99e38676adad3c11df5d93b0e3) an ArgumentError if the `make` method's argument is not Hash or LightService::Context.
195
195
 
196
196
  ## License
197
197
 
@@ -14,6 +14,10 @@ module LightService
14
14
  end
15
15
 
16
16
  def self.make(context={})
17
+ unless context.is_a? Hash or context.is_a? ::LightService::Context
18
+ raise ArgumentError, 'Argument must be Hash or LightService::Context'
19
+ end
20
+
17
21
  return context if context.is_a?(Context)
18
22
  self.new(context, ::LightService::Outcomes::SUCCESS, '')
19
23
  end
@@ -42,11 +46,21 @@ module LightService
42
46
  end
43
47
 
44
48
  def set_success!(message)
49
+ warn 'DEPRECATED: Please use `succeed!` instead'
50
+ succeed!(message)
51
+ end
52
+
53
+ def succeed!(message)
45
54
  @message = message
46
55
  @outcome = ::LightService::Outcomes::SUCCESS
47
56
  end
48
57
 
49
58
  def set_failure!(message)
59
+ warn 'DEPRECATED: Please use `fail!` instead'
60
+ fail!(message)
61
+ end
62
+
63
+ def fail!(message)
50
64
  @message = message
51
65
  @outcome = ::LightService::Outcomes::FAILURE
52
66
  end
@@ -55,5 +69,6 @@ module LightService
55
69
  @message = message
56
70
  @skip_all = true
57
71
  end
72
+
58
73
  end
59
74
  end
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,13 +3,12 @@ require 'spec_helper'
3
3
  class Organizer
4
4
  include LightService::Organizer
5
5
 
6
- def self.add_number(number)
7
- with(number: number).reduce \
8
- [
9
- AddsOneAction,
10
- AddsTwoAction,
11
- AddsThreeAction
12
- ]
6
+ def self.add_numbers(number)
7
+ with(:number => number).reduce(
8
+ AddsOneAction,
9
+ AddsTwoAction,
10
+ AddsThreeAction
11
+ )
13
12
  end
14
13
  end
15
14
 
@@ -48,7 +47,9 @@ end
48
47
 
49
48
  describe Organizer do
50
49
  it "Adds 1 2 3 and through to 1" do
51
- result = Organizer.add_number 1
52
- result.fetch(:number).should == 7
50
+ result = Organizer.add_numbers 1
51
+ number = result.fetch(:number)
52
+
53
+ expect(number).to eq(7)
53
54
  end
54
55
  end
data/spec/action_spec.rb CHANGED
@@ -18,11 +18,11 @@ module LightService
18
18
  end
19
19
  end
20
20
 
21
- let(:context) { ::LightService::Context.new }
21
+ let(:context) { ::LightService::Context.make }
22
22
 
23
23
  context "when the action context has failure" do
24
24
  it "returns immediately" do
25
- context.set_failure!("an error")
25
+ context.fail!("an error")
26
26
 
27
27
  DummyAction.execute(context)
28
28
 
data/spec/context_spec.rb CHANGED
@@ -2,109 +2,68 @@ require "spec_helper"
2
2
 
3
3
  module LightService
4
4
  describe Context do
5
- subject { Context.new({:test => 1}, Outcomes::SUCCESS, "some_message") }
6
5
 
7
- it "initializes the object with default arguments" do
8
- service_result = Context.new({test: 1})
9
-
10
- expect(service_result).to be_success
11
- expect(service_result.message).to eq ''
12
- expect(service_result[:test]).to eq 1
13
- end
14
-
15
- it "initializes the object with the context" do
16
- service_result = Context.new.tap { |o| o.add_to_context({test: 1})}
17
-
18
- expect(service_result).to be_success
19
- expect(service_result.message).to eq ''
20
- expect(service_result[:test]).to eq 1
21
- end
22
-
23
- describe ".make" do
24
- it "initializes the object with make" do
25
- service_result = Context.make({test: 1})
26
-
27
- expect(service_result).to be_success
28
- expect(service_result.message).to eq ""
29
- expect(service_result[:test]).to eq 1
30
- end
31
-
32
- context "when passing valid parameters" do
33
- subject { Context.make(params).to_hash }
34
-
35
- let(:params) { {test: 1} }
36
- it { should eq({test: 1}) }
37
-
38
- let(:params) { Context.make(test: 1) }
39
- it { should eq({test: 1}) }
6
+ describe "can be made" do
7
+ context "with no arguments" do
8
+ subject { Context.make }
9
+ it { should be_success }
10
+ its(:message) { should be_empty }
40
11
  end
41
12
 
42
- context "when passing invalid parameters" do
43
- subject { lambda {Context.make(invalid_params)} }
44
-
45
- let(:invalid_params) { nil }
46
- it { should raise_error(NoMethodError) }
47
-
48
- let(:invalid_params) { [] }
49
- it { should raise_error(NoMethodError) }
13
+ context "with a hash" do
14
+ it "has the hash values" do
15
+ context = Context.make(:one => 1)
16
+ expect(context[:one]).to eq(1)
17
+ end
50
18
  end
51
19
 
52
- context "data is a context" do
53
- let(:original_context) { Context.new }
54
-
55
- it "returns the same context object" do
56
- new_context = Context.make(original_context)
57
- expect(new_context.object_id).to eq(original_context.object_id)
20
+ context "with FAILURE" do
21
+ it "is failed" do
22
+ context = Context.new({}, ::LightService::Outcomes::FAILURE, '')
23
+ expect(context).to be_failure
58
24
  end
59
25
  end
60
26
  end
61
27
 
62
- describe "#to_hash" do
63
- it "converts context into a hash" do
64
- Context.make(test: 1).to_hash.should == {test: 1}
65
- Context.make({}).to_hash.should == {}
28
+ describe "can't be made" do
29
+ specify "with invalid parameters" do
30
+ expect{Context.make([])}.to raise_error(ArgumentError)
66
31
  end
67
32
  end
68
33
 
69
- context "when created" do
70
- it { should be_success }
34
+ it "can be asked for success?" do
35
+ context = Context.new({}, ::LightService::Outcomes::SUCCESS)
36
+ expect(context.success?).to be_true
71
37
  end
72
38
 
73
- it "allows to set success" do
74
- subject.set_success!('the success')
75
-
76
- expect(subject).to be_success
77
- expect(subject).not_to be_failure
78
- expect(subject.message) == 'the success'
39
+ it "can be asked for failure?" do
40
+ context = Context.new({}, ::LightService::Outcomes::FAILURE)
41
+ expect(context.failure?).to be_true
79
42
  end
80
43
 
81
- it "allows to set failure" do
82
- subject.set_failure!('the failure')
83
-
84
- expect(subject).not_to be_success
85
- expect(subject).to be_failure
86
- expect(subject.message).to eq('the failure')
44
+ it "can be asked for skip_all?" do
45
+ context = Context.make
46
+ context.skip_all!
47
+ expect(context.skip_all?).to be_true
87
48
  end
88
49
 
89
- it "allows to set skip_all" do
90
- subject.skip_all!('the reason to skip')
91
-
92
- expect(subject).to be_skip_all
93
- expect(subject).to be_success
94
- expect(subject).not_to be_failure
95
- expect(subject.message).to eq('the reason to skip')
50
+ it "can be pushed into a SUCCESS state" do
51
+ context = Context.make
52
+ context.succeed!("a happy end")
53
+ expect(context).to be_success
96
54
  end
97
55
 
98
- it "lets setting a group of context values" do
99
- subject.to_hash.should include(test: 1)
100
- subject.to_hash.keys.length.should == 1
101
-
102
- subject.add_to_context(test: 1, two: 2)
56
+ it "can be pushed into a FAILURE state" do
57
+ context = Context.make
58
+ context.fail!("a sad end")
59
+ expect(context).to be_failure
60
+ end
103
61
 
104
- expect(subject.to_hash.keys.length).to eq(2)
105
- expect(subject.to_hash).to include(test: 1)
106
- expect(subject.to_hash).to include(two: 2)
62
+ it "can set a flag to skip all subsequent actions" do
63
+ context = Context.make
64
+ context.skip_all!
65
+ expect(context).to be_skip_all
107
66
  end
108
- end
109
67
 
68
+ end
110
69
  end
@@ -7,18 +7,12 @@ describe LightService::Organizer do
7
7
  class AnOrganizer
8
8
  include LightService::Organizer
9
9
 
10
- def self.some_method(user)
11
- with(user: user).reduce([AnAction, AnotherAction])
10
+ def self.do_something(action_arguments)
11
+ with(action_arguments).reduce([AnAction, AnotherAction])
12
12
  end
13
13
 
14
- def self.some_method_with(user)
15
- context = LightService::Context.make(user: user)
16
- with(context).reduce(AnAction, AnotherAction)
17
- end
18
-
19
- def self.some_method_with_no_actions(user)
20
- context = LightService::Context.make(user: user)
21
- with(context).reduce
14
+ def self.do_something_with_no_actions(action_arguments)
15
+ with(action_arguments).reduce
22
16
  end
23
17
  end
24
18
 
@@ -35,8 +29,8 @@ describe LightService::Organizer do
35
29
  .and_return context
36
30
  end
37
31
 
38
- it "creates a Context" do
39
- result = AnOrganizer.some_method(user)
32
+ it "implicitly creates a Context" do
33
+ result = AnOrganizer.do_something(:user => user)
40
34
  expect(result).to eq(context)
41
35
  end
42
36
  end
@@ -51,15 +45,15 @@ describe LightService::Organizer do
51
45
  .and_return context
52
46
  end
53
47
 
54
- it "creates a Context" do
55
- result = AnOrganizer.some_method_with(user)
48
+ it "uses that Context without recreating it" do
49
+ result = AnOrganizer.do_something(context)
56
50
  expect(result).to eq(context)
57
51
  end
58
52
  end
59
53
 
60
54
  context "when no Actions are specified" do
61
55
  it "throws a Runtime error" do
62
- expect { AnOrganizer.some_method_with_no_actions(user) }.to \
56
+ expect { AnOrganizer.do_something_with_no_actions(context) }.to \
63
57
  raise_error RuntimeError, "No action(s) were provided"
64
58
  end
65
59
  end
@@ -17,7 +17,7 @@ class LooksUpTaxPercentageAction
17
17
 
18
18
  def self.object_is_nil?(object, context, message)
19
19
  if object.nil?
20
- context.set_failure!(message)
20
+ context.fail!(message)
21
21
  return true
22
22
  end
23
23
 
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.1.0
4
+ version: 0.2.0
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-02-18 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec