simple_service 1.3.8 → 1.3.9

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: 53bc7180191d536a538cebcce463f372e699f9e9
4
- data.tar.gz: 589d9e66591fe266abb81fb215b1570aa45fae4e
3
+ metadata.gz: 9d1fd9d6d46d6946f325a672bb447d015966972b
4
+ data.tar.gz: 3512dab5a7c70201e2f1ae10e5053edb40c5e469
5
5
  SHA512:
6
- metadata.gz: 2c1202dc463df6be66e8349f8c5d93c6bbf612ef5b4757f3805a83be6ee355cb5fd84d1b1ced17e9e431eaff6e7b45ed52bf65a11e7212d9552cd46c380af6f7
7
- data.tar.gz: 2f6724c18598ca132e6f1a157bb9050ce05beb1b14dc4fc3601f959c8d68a10beaa5cb7981ec54cb7ed1ffebd0915a3961abd86752c179612a2e551e7a69052b
6
+ metadata.gz: 20464b3d21dfb4258b0a615c6a579949f83b33ca35f016a2f77e003433041f3e187577478cfeb5497051304eb163cd92f2a458368830f9f07a39eded82e28073
7
+ data.tar.gz: 87fcc9920a8534a960210a54eb4bbe15b121189d96ce3d6c117eec81200e762aff88d300c39da58846de1bf7ed0108f681d6136636326024e5a92397b27fba2d
@@ -5,4 +5,6 @@ module SimpleService
5
5
  class ExpectedKeyError < StandardError; end;
6
6
  class CallNotDefinedError < StandardError; end;
7
7
  class ReturnKeyError < StandardError; end;
8
+ class InvalidArgumentError < StandardError; end;
9
+
8
10
  end
@@ -6,8 +6,8 @@ module SimpleService
6
6
 
7
7
  attr_accessor :context
8
8
 
9
- def initialize(context = {})
10
- @context = context
9
+ def initialize(_context = {})
10
+ @context = validate_context(_context)
11
11
 
12
12
  symbolize_context_keys
13
13
  setup_call_chain
@@ -41,11 +41,16 @@ module SimpleService
41
41
  context
42
42
  end
43
43
 
44
+ # also merge any optional keys
45
+ command.get_optional.each do |key|
46
+ _context[key] = context[key]
47
+ end
48
+
44
49
  # instantiate and call the command
45
- new_context = command.new(_context).call
50
+ resulting_context = command.new(_context).call
46
51
 
47
52
  # update the master context with the results of the command
48
- @context.merge!(new_context)
53
+ @context.merge!(resulting_context)
49
54
  end
50
55
  end
51
56
  end
@@ -58,6 +63,15 @@ module SimpleService
58
63
 
59
64
  private
60
65
 
66
+ def validate_context(_context)
67
+ unless _context.class == Hash
68
+ raise InvalidArgumentError,
69
+ "Hash required as argument, but was given a #{_context.class}"
70
+ end
71
+
72
+ _context
73
+ end
74
+
61
75
  def with_validation
62
76
  # don't mess with the context if we are doing internal validation
63
77
  add_validation_keys_to_context unless skip_validation
@@ -10,6 +10,14 @@ module SimpleService
10
10
  @expects || []
11
11
  end
12
12
 
13
+ def optional(*args)
14
+ @optional = args
15
+ end
16
+
17
+ def get_optional
18
+ @optional || []
19
+ end
20
+
13
21
  def returns(*args)
14
22
  @returns = args
15
23
  end
@@ -101,6 +109,10 @@ module SimpleService
101
109
  self.class.get_expects
102
110
  end
103
111
 
112
+ def optional
113
+ self.class.get_optional
114
+ end
115
+
104
116
  def returns
105
117
  self.class.get_returns
106
118
  end
@@ -110,7 +122,7 @@ module SimpleService
110
122
  end
111
123
 
112
124
  def all_context_keys
113
- (expects + returns + ['message', 'success']).uniq
125
+ (expects + optional + returns + ['message', 'success']).uniq
114
126
  end
115
127
 
116
128
  def organizer?
@@ -1,3 +1,3 @@
1
1
  module SimpleService
2
- VERSION = '1.3.8'
2
+ VERSION = '1.3.9'
3
3
  end
@@ -4,10 +4,11 @@ describe SimpleService::Command do
4
4
 
5
5
  class ValidCommand < SimpleService::Command
6
6
  expects :foo, :bar
7
+ optional :stuff
7
8
  returns :bar, :baz
8
9
  def call
9
10
  context.merge!(
10
- bar: 'modified',
11
+ bar: ['modified', self.stuff].compact.join(' '),
11
12
  baz: 'blah'
12
13
  )
13
14
  end
@@ -31,6 +32,12 @@ describe SimpleService::Command do
31
32
  ).to eql(bar: 'modified', baz: 'blah')
32
33
  end
33
34
 
35
+ it 'provides optional arguments as getters' do
36
+ expect(
37
+ ValidCommand.call(foo: 'blah', bar: 'meh', stuff: 'something')
38
+ ).to eql(bar: 'modified something', baz: 'blah')
39
+ end
40
+
34
41
  end
35
42
 
36
43
  describe '#call' do
@@ -2,26 +2,29 @@ require 'spec_helper'
2
2
 
3
3
  describe SimpleService::Organizer do
4
4
 
5
- context 'classes with expects and returns' do
5
+ context 'classes with .expects, .optional, and .returns' do
6
6
 
7
7
  class TestCommandOne < SimpleService::Command
8
8
  expects :foo
9
+ optional :blah
9
10
  returns :foo, :bar
10
11
  def call
11
- context.merge!(bar: 'bar')
12
+ context.merge!(bar: ['bar', self.blah].compact.join(' '))
12
13
  end
13
14
  end
14
15
 
15
16
  class TestCommandTwo < SimpleService::Command
16
17
  expects :foo, :bar
18
+ optional :blarg
17
19
  returns :foo, :bar, :baz
18
20
  def call
19
- context.merge!(baz: 'baz')
21
+ context.merge!(baz: ['baz', self.blarg].compact.join(' '))
20
22
  end
21
23
  end
22
24
 
23
25
  class TestOrganizer < SimpleService::Organizer
24
26
  expects :foo
27
+ optional :blah, :blarg
25
28
  returns :foo, :bar, :baz
26
29
  commands TestCommandOne, TestCommandTwo
27
30
  end
@@ -32,6 +35,12 @@ describe SimpleService::Organizer do
32
35
  TestOrganizer.call(foo: 'foo')
33
36
  ).to eql(foo: 'foo', bar: 'bar', baz: 'baz', success: true)
34
37
  end
38
+
39
+ it 'returns the correct hash when optional arguments provided' do
40
+ expect(
41
+ TestOrganizer.call(foo: 'foo', blah: 'blah', blarg: 'blarg')
42
+ ).to eql(foo: 'foo', bar: 'bar blah', baz: 'baz blarg', success: true)
43
+ end
35
44
  end
36
45
 
37
46
  describe '#call' do
@@ -149,4 +158,24 @@ describe SimpleService::Organizer do
149
158
 
150
159
  end
151
160
 
161
+ context 'when arguments are not a hash' do
162
+
163
+ class DoNothingCommand < SimpleService::Command
164
+ def call
165
+ 'do nothing'
166
+ end
167
+ end
168
+
169
+ class DoNothingOrganizer < SimpleService::Organizer
170
+ commands DoNothingCommand
171
+ end
172
+
173
+ it 'raises an error' do
174
+ expect { DoNothingOrganizer.new('not a hash').call }.to raise_error(
175
+ SimpleService::InvalidArgumentError,
176
+ 'Hash required as argument, but was given a String'
177
+ )
178
+ end
179
+ end
180
+
152
181
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.8
4
+ version: 1.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrod Spillers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-14 00:00:00.000000000 Z
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler