services 0.2.4 → 0.2.5
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 +4 -4
- data/lib/services/base.rb +18 -11
- data/lib/services/version.rb +1 -1
- data/spec/services/base_spec.rb +27 -8
- data/spec/support/test_services.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29308e17d5df01f543e985f8aeeb933a0e22e66f
|
4
|
+
data.tar.gz: 8ccb2f4ca9d250a98a9b78e87d2a5291078a1e00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54d37715aaea3ffbc50852d8ceff00374d58443f3b67a93ee686ec9b88223904ee22fe49cb3ce6d2a36bdc1831f6b490c40ee542f0eca0f5b29634169a237c33
|
7
|
+
data.tar.gz: c324202d7c4311b6d549f1c764fe817ec1540cd0a3ab02dc9aeacc4493bf479621282aed5dfd8f447c35be7c86a27b2d324714b8a5937d85c29045a6a4a0a1ac
|
data/lib/services/base.rb
CHANGED
@@ -25,15 +25,11 @@ module Services
|
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
def find_objects(ids_or_objects, klass =
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
ids_or_objects_array = Array(ids_or_objects)
|
34
|
-
ids, objects = ids_or_objects_array.grep(Fixnum), ids_or_objects_array.grep(klass)
|
35
|
-
if ids.size + objects.size < ids_or_objects_array.size
|
36
|
-
raise "All params must be either #{klass.to_s.pluralize} or Fixnums: #{ids_or_objects_array.map(&:class)}"
|
28
|
+
def find_objects(ids_or_objects, klass = determine_service_class)
|
29
|
+
ids_or_objects = Array(ids_or_objects)
|
30
|
+
ids, objects = ids_or_objects.grep(Fixnum), ids_or_objects.grep(klass)
|
31
|
+
if ids.size + objects.size < ids_or_objects.size
|
32
|
+
raise "All params must be either #{klass.to_s.pluralize} or Fixnums: #{ids_or_objects.map(&:class)}"
|
37
33
|
end
|
38
34
|
if ids.any?
|
39
35
|
find_service = "Services::#{klass.to_s.pluralize}::Find"
|
@@ -47,9 +43,20 @@ module Services
|
|
47
43
|
raise self.class::Error, "#{klass.to_s.pluralize(missing_ids)} #{missing_ids.join(', ')} not found." if missing_ids.size > 0
|
48
44
|
objects.concat objects_from_ids
|
49
45
|
end
|
50
|
-
|
46
|
+
objects
|
47
|
+
end
|
48
|
+
|
49
|
+
def find_object(*args)
|
50
|
+
find_objects(*args).tap do |objects|
|
51
|
+
raise "Expected only one object but found #{objects.size}" unless objects.size == 1
|
52
|
+
end.first
|
53
|
+
end
|
54
|
+
|
55
|
+
def determine_service_class
|
56
|
+
self.class.to_s[/Services::([^:]+)/, 1].singularize.constantize
|
57
|
+
rescue
|
58
|
+
raise "Could not determine service class from #{self.class}"
|
51
59
|
end
|
52
|
-
alias_method :find_object, :find_objects
|
53
60
|
|
54
61
|
def controller
|
55
62
|
@controller ||= begin
|
data/lib/services/version.rb
CHANGED
data/spec/services/base_spec.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Services::Base do
|
4
|
-
|
5
|
-
let(:objects) { (1..5).to_a.shuffle.map { |id| Model.new(id) } }
|
4
|
+
let(:model_objects) { (1..5).to_a.shuffle.map { |id| Model.new(id) } }
|
6
5
|
|
6
|
+
describe '#find_objects' do
|
7
7
|
context 'when passing in objects' do
|
8
8
|
it 'returns the same objects' do
|
9
|
-
expect(Services::Models::FindObjectsTest.call(
|
9
|
+
expect(Services::Models::FindObjectsTest.call(model_objects)).to eq(model_objects)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
context 'when passing in IDs' do
|
14
14
|
it 'returns the objects for the IDs' do
|
15
|
-
expect(Services::Models::FindObjectsTest.call(
|
15
|
+
expect(Services::Models::FindObjectsTest.call(model_objects.map(&:id))).to eq(model_objects)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
context 'when passing in objects and IDs' do
|
20
20
|
it 'returns the objects plus the objects for the IDs' do
|
21
|
-
objects_as_objects, objects_as_ids =
|
21
|
+
objects_as_objects, objects_as_ids = model_objects.partition do |object|
|
22
22
|
rand(2) == 1
|
23
23
|
end
|
24
24
|
|
@@ -30,10 +30,29 @@ describe Services::Base do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
context 'when passing in a single object or ID' do
|
33
|
-
it 'returns
|
34
|
-
object =
|
33
|
+
it 'returns an array containing the object' do
|
34
|
+
object = model_objects.sample
|
35
35
|
[object.id, object].each do |id_or_object|
|
36
|
-
expect(Services::Models::FindObjectsTest.call(id_or_object)).to eq(object)
|
36
|
+
expect(Services::Models::FindObjectsTest.call(id_or_object)).to eq([object])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#find_object' do
|
43
|
+
context 'when passing in a single object or ID' do
|
44
|
+
it 'returns the object' do
|
45
|
+
object = model_objects.sample
|
46
|
+
[object.id, object].each do |id_or_object|
|
47
|
+
expect(Services::Models::FindObjectTest.call(id_or_object)).to eq(object)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when passing in something else than a single object or ID' do
|
53
|
+
it 'raises an error' do
|
54
|
+
[%w(foo bar), nil, Object.new].each do |object|
|
55
|
+
expect { Services::Models::FindObjectTest.call(object) }.to raise_error
|
37
56
|
end
|
38
57
|
end
|
39
58
|
end
|