services 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/services/base.rb +18 -5
- data/lib/services/version.rb +1 -1
- data/spec/services/base_finder_spec.rb +1 -0
- data/spec/services/base_spec.rb +56 -17
- data/spec/support/test_services.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97b67785d62fb93ba36e511d6e6a2a7f1f949468
|
4
|
+
data.tar.gz: 95180d3cc52cdb8e86a12f8937b46967739adb1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3610f3f8aa0b79110ea17583191f4f5dec5a976eb8b9a6fc632bf614aa5a96d0b38508ecb80e1c6f4f35a7b65198c6c341634c171f53f732fce6278b38505580
|
7
|
+
data.tar.gz: d43bca63c94f9ef81c24752720c5e5d3d08c352c35c5c2905090a956882b20ffb0427908aa1bdc863ad5f4ddee7b476c2909ba1b57eef88fd731471fab363445
|
data/CHANGELOG.md
CHANGED
data/lib/services/base.rb
CHANGED
@@ -25,12 +25,23 @@ module Services
|
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
def
|
28
|
+
def _split_ids_and_objects(ids_or_objects, klass)
|
29
29
|
ids_or_objects = Array(ids_or_objects)
|
30
30
|
ids, objects = ids_or_objects.grep(Fixnum), ids_or_objects.grep(klass)
|
31
31
|
if ids.size + objects.size < ids_or_objects.size
|
32
32
|
raise "All params must be either #{klass.to_s.pluralize} or Fixnums: #{ids_or_objects.map { |id_or_object| [id_or_object.class, id_or_object.inspect].join(' - ')}}"
|
33
33
|
end
|
34
|
+
[ids, objects]
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_ids(ids_or_objects, klass = object_class)
|
38
|
+
ids, objects = _split_ids_and_objects(ids_or_objects, klass)
|
39
|
+
ids.concat objects.map(&:id) if objects.any?
|
40
|
+
ids
|
41
|
+
end
|
42
|
+
|
43
|
+
def find_objects(ids_or_objects, klass = object_class)
|
44
|
+
ids, objects = _split_ids_and_objects(ids_or_objects, klass)
|
34
45
|
if ids.any?
|
35
46
|
find_service = "Services::#{klass.to_s.pluralize}::Find"
|
36
47
|
objects_from_ids = find_service.constantize.call(ids)
|
@@ -46,10 +57,12 @@ module Services
|
|
46
57
|
objects
|
47
58
|
end
|
48
59
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
60
|
+
%i(object id).each do |type|
|
61
|
+
define_method "find_#{type}" do |*args|
|
62
|
+
send("find_#{type.to_s.pluralize}", *args).tap do |objects_or_ids|
|
63
|
+
raise "Expected exactly one object or ID but found #{objects_or_ids.size}." unless objects_or_ids.size == 1
|
64
|
+
end.first
|
65
|
+
end
|
53
66
|
end
|
54
67
|
|
55
68
|
def object_class
|
data/lib/services/version.rb
CHANGED
@@ -6,6 +6,7 @@ describe Services::BaseFinder do
|
|
6
6
|
let(:base_find) { Services::Models::BaseFind }
|
7
7
|
|
8
8
|
it 'has call logging disabled by default' do
|
9
|
+
pending 'Rails has to be loaded to call BaseFinder'
|
9
10
|
expect(base_find.call_logging_disabled).to eq(true)
|
10
11
|
expect { base_find.call }.to_not change { logs }
|
11
12
|
end
|
data/spec/services/base_spec.rb
CHANGED
@@ -1,39 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Services::Base do
|
4
|
-
let(:
|
4
|
+
let(:model_ids) { (1..5).to_a.shuffle }
|
5
|
+
let(:model_objects) { model_ids.map { |id| Model.new(id) } }
|
6
|
+
let(:model_ids_and_objects) { model_ids[0..2] + model_objects[3..-1] }
|
5
7
|
|
6
8
|
describe '#find_objects' do
|
7
9
|
context 'when passing in objects' do
|
8
10
|
it 'returns the same objects' do
|
9
|
-
expect(Services::Models::FindObjectsTest.call(model_objects)).to
|
11
|
+
expect(Services::Models::FindObjectsTest.call(model_objects)).to match_array(model_objects)
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
13
15
|
context 'when passing in IDs' do
|
14
16
|
it 'returns the objects for the IDs' do
|
15
|
-
expect(Services::Models::FindObjectsTest.call(
|
17
|
+
expect(Services::Models::FindObjectsTest.call(model_ids)).to match_array(model_objects)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
context 'when passing in objects and IDs' do
|
20
22
|
it 'returns the objects plus the objects for the IDs' do
|
21
|
-
|
22
|
-
rand(2) == 1
|
23
|
-
end
|
24
|
-
|
25
|
-
objects_and_ids = objects_as_objects + objects_as_ids.map(&:id)
|
26
|
-
only_objects = objects_as_objects + objects_as_ids
|
27
|
-
|
28
|
-
expect(Services::Models::FindObjectsTest.call(objects_and_ids)).to eq(only_objects)
|
23
|
+
expect(Services::Models::FindObjectsTest.call(model_ids_and_objects)).to match_array(model_objects)
|
29
24
|
end
|
30
25
|
end
|
31
26
|
|
32
27
|
context 'when passing in a single object or ID' do
|
33
28
|
it 'returns an array containing the object' do
|
34
|
-
|
35
|
-
|
36
|
-
expect(Services::Models::FindObjectsTest.call(id_or_object)).to eq([object])
|
29
|
+
[model_ids.first, model_objects.first].each do |id_or_object|
|
30
|
+
expect(Services::Models::FindObjectsTest.call(id_or_object)).to match_array([model_objects.first])
|
37
31
|
end
|
38
32
|
end
|
39
33
|
end
|
@@ -42,9 +36,8 @@ describe Services::Base do
|
|
42
36
|
describe '#find_object' do
|
43
37
|
context 'when passing in a single object or ID' do
|
44
38
|
it 'returns the object' do
|
45
|
-
|
46
|
-
|
47
|
-
expect(Services::Models::FindObjectTest.call(id_or_object)).to eq(object)
|
39
|
+
[model_ids.first, model_objects.first].each do |id_or_object|
|
40
|
+
expect(Services::Models::FindObjectTest.call(id_or_object)).to eq(model_objects.first)
|
48
41
|
end
|
49
42
|
end
|
50
43
|
end
|
@@ -57,4 +50,50 @@ describe Services::Base do
|
|
57
50
|
end
|
58
51
|
end
|
59
52
|
end
|
53
|
+
|
54
|
+
describe '#find_ids' do
|
55
|
+
context 'when passing in objects' do
|
56
|
+
it 'returns the IDs for the objects' do
|
57
|
+
expect(Services::Models::FindIdsTest.call(model_objects)).to match_array(model_ids)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when passing in IDs' do
|
62
|
+
it 'returns the same IDs' do
|
63
|
+
expect(Services::Models::FindIdsTest.call(model_ids)).to match_array(model_ids)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when passing in objects and IDs' do
|
68
|
+
it 'returns the IDs for the objects plus the passed in IDs' do
|
69
|
+
expect(Services::Models::FindIdsTest.call(model_ids_and_objects)).to match_array(model_ids)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when passing in a single object or ID' do
|
74
|
+
it 'returns an array containing the ID' do
|
75
|
+
[model_ids.first, model_objects.first].each do |id_or_object|
|
76
|
+
expect(Services::Models::FindIdsTest.call(id_or_object)).to match_array([model_ids.first])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#find_id' do
|
83
|
+
context 'when passing in a single object or ID' do
|
84
|
+
it 'returns the ID' do
|
85
|
+
[model_ids.first, model_objects.first].each do |id_or_object|
|
86
|
+
expect(Services::Models::FindIdTest.call(id_or_object)).to eq(model_ids.first)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'when passing in something else than a single object or ID' do
|
92
|
+
it 'raises an error' do
|
93
|
+
[%w(foo bar), nil, Object.new].each do |object|
|
94
|
+
expect { Services::Models::FindIdTest.call(object) }.to raise_error
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
60
99
|
end
|
@@ -67,6 +67,18 @@ module Services
|
|
67
67
|
find_object id_or_object
|
68
68
|
end
|
69
69
|
end
|
70
|
+
|
71
|
+
class FindIdsTest < Services::Base
|
72
|
+
def call(ids_or_objects)
|
73
|
+
find_ids ids_or_objects
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class FindIdTest < Services::Base
|
78
|
+
def call(id_or_object)
|
79
|
+
find_id id_or_object
|
80
|
+
end
|
81
|
+
end
|
70
82
|
end
|
71
83
|
end
|
72
84
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: services
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Meurer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|