light-service 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,10 +10,12 @@ module LightService
10
10
  def initialize(context={}, outcome=::LightService::Outcomes::SUCCESS, message='')
11
11
  @outcome, @message = outcome, message
12
12
  context.to_hash.each {|k,v| self[k] = v}
13
+ self
13
14
  end
14
15
 
15
16
  def self.make(context={})
16
- Context.new(context, ::LightService::Outcomes::SUCCESS, '')
17
+ return context if context.is_a?(Context)
18
+ self.new(context, ::LightService::Outcomes::SUCCESS, '')
17
19
  end
18
20
 
19
21
  def add_to_context(values)
@@ -15,7 +15,13 @@ module LightService
15
15
  self
16
16
  end
17
17
 
18
- def reduce(actions=[])
18
+ def reduce(*actions)
19
+ raise "No action(s) were provided" if actions.empty?
20
+
21
+ if actions.is_a? Array
22
+ actions.flatten!
23
+ end
24
+
19
25
  actions.reduce(@context) { |context, action| action.execute(context) }
20
26
  end
21
27
  end
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -7,6 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.description = %q{A service skeleton with an emphasis on simplicity}
8
8
  gem.summary = %q{A service skeleton with an emphasis on simplicity}
9
9
  gem.homepage = ""
10
+ gem.license = "MIT"
10
11
 
11
12
  gem.files = `git ls-files`.split($\)
12
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
data/spec/context_spec.rb CHANGED
@@ -6,24 +6,27 @@ module LightService
6
6
 
7
7
  it "initializes the object with default arguments" do
8
8
  service_result = Context.new({test: 1})
9
- service_result.should be_success
10
- service_result.message.should eq ""
11
- service_result[:test].should eq 1
9
+
10
+ expect(service_result).to be_success
11
+ expect(service_result.message).to eq ''
12
+ expect(service_result[:test]).to eq 1
12
13
  end
13
14
 
14
15
  it "initializes the object with the context" do
15
- service_result = Context.new.tap { |o| o.add_to_context({:test => 1})}
16
- service_result.should be_success
17
- service_result.message.should eq ""
18
- service_result[:test].should eq 1
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
19
21
  end
20
22
 
21
23
  describe ".make" do
22
24
  it "initializes the object with make" do
23
- service_result = Context.make({:test => 1})
24
- service_result.should be_success
25
- service_result.message.should eq ""
26
- service_result[:test].should eq 1
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
27
30
  end
28
31
 
29
32
  context "when passing valid parameters" do
@@ -45,6 +48,15 @@ module LightService
45
48
  let(:invalid_params) { [] }
46
49
  it { should raise_error(NoMethodError) }
47
50
  end
51
+
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)
58
+ end
59
+ end
48
60
  end
49
61
 
50
62
  describe "#to_hash" do
@@ -59,31 +71,39 @@ module LightService
59
71
  end
60
72
 
61
73
  it "allows to set success" do
62
- subject.set_success!("the success")
63
- subject.should be_success
64
- subject.message.should == "the success"
65
- end
74
+ subject.set_success!('the success')
66
75
 
67
- specify "evaluates failure?" do
68
- subject.set_success!("the success")
69
- subject.should_not be_failure
76
+ expect(subject).to be_success
77
+ expect(subject).not_to be_failure
78
+ expect(subject.message) == 'the success'
70
79
  end
71
80
 
72
81
  it "allows to set failure" do
73
- subject.set_failure!("the failure")
74
- subject.should_not be_success
75
- subject.message.should == "the failure"
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')
87
+ end
88
+
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')
76
96
  end
77
97
 
78
98
  it "lets setting a group of context values" do
79
- subject.to_hash.should include(:test => 1)
99
+ subject.to_hash.should include(test: 1)
80
100
  subject.to_hash.keys.length.should == 1
81
101
 
82
- subject.add_to_context(:test => 1, :two => 2)
102
+ subject.add_to_context(test: 1, two: 2)
83
103
 
84
- subject.to_hash.keys.length.should == 2
85
- subject.to_hash.should include(:test => 1)
86
- subject.to_hash.should include(:two => 2)
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)
87
107
  end
88
108
  end
89
109
 
@@ -2,21 +2,27 @@ require 'spec_helper'
2
2
 
3
3
  describe LightService::Organizer do
4
4
  class AnAction; end
5
+ class AnotherAction; end
5
6
 
6
7
  class AnOrganizer
7
8
  include LightService::Organizer
8
9
 
9
10
  def self.some_method(user)
10
- with(user: user).reduce [AnAction]
11
+ with(user: user).reduce([AnAction, AnotherAction])
11
12
  end
12
13
 
13
14
  def self.some_method_with(user)
14
- context = LightService::Context.new(user: user)
15
- with(context).reduce [AnAction]
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
16
22
  end
17
23
  end
18
24
 
19
- let!(:context) { ::LightService::Context.new(user: user) }
25
+ let(:context) { ::LightService::Context.make(user: user) }
20
26
  let(:user) { double(:user) }
21
27
 
22
28
  context "when #with is called with hash" do
@@ -24,11 +30,37 @@ describe LightService::Organizer do
24
30
  AnAction.should_receive(:execute) \
25
31
  .with(context) \
26
32
  .and_return context
33
+ AnotherAction.should_receive(:execute) \
34
+ .with(context) \
35
+ .and_return context
27
36
  end
37
+
28
38
  it "creates a Context" do
29
- LightService::Context.should_receive(:make).and_return context
39
+ result = AnOrganizer.some_method(user)
40
+ expect(result).to eq(context)
41
+ end
42
+ end
43
+
44
+ context "when #with is called with Context" do
45
+ before do
46
+ AnAction.should_receive(:execute) \
47
+ .with(context) \
48
+ .and_return context
49
+ AnotherAction.should_receive(:execute) \
50
+ .with(context) \
51
+ .and_return context
52
+ end
53
+
54
+ it "creates a Context" do
55
+ result = AnOrganizer.some_method_with(user)
56
+ expect(result).to eq(context)
57
+ end
58
+ end
30
59
 
31
- AnOrganizer.some_method(user)
60
+ context "when no Actions are specified" do
61
+ it "throws a Runtime error" do
62
+ expect { AnOrganizer.some_method_with_no_actions(user) }.to \
63
+ raise_error RuntimeError, "No action(s) were provided"
32
64
  end
33
65
  end
34
66
  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.11
4
+ version: 0.0.12
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: 2013-10-27 00:00:00.000000000 Z
12
+ date: 2013-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -95,7 +95,8 @@ files:
95
95
  - spec/sample/tax/provides_free_shipping_action.rb
96
96
  - spec/spec_helper.rb
97
97
  homepage: ''
98
- licenses: []
98
+ licenses:
99
+ - MIT
99
100
  post_install_message:
100
101
  rdoc_options: []
101
102
  require_paths:
@@ -106,18 +107,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
107
  - - ! '>='
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
- segments:
110
- - 0
111
- hash: -863764600829619710
112
110
  required_rubygems_version: !ruby/object:Gem::Requirement
113
111
  none: false
114
112
  requirements:
115
113
  - - ! '>='
116
114
  - !ruby/object:Gem::Version
117
115
  version: '0'
118
- segments:
119
- - 0
120
- hash: -863764600829619710
121
116
  requirements: []
122
117
  rubyforge_project:
123
118
  rubygems_version: 1.8.23