services 2.0.2 → 2.1.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: 3957769bb927ced167265444d98a3a10992e8eb6
4
- data.tar.gz: 230cfe057c104954db348751127dea932c661b69
3
+ metadata.gz: 97b67785d62fb93ba36e511d6e6a2a7f1f949468
4
+ data.tar.gz: 95180d3cc52cdb8e86a12f8937b46967739adb1b
5
5
  SHA512:
6
- metadata.gz: 8f0643545a27ab417933795342a71e898778bf752477ab889ac4b63ab22962dc0f4297a2a779673ff5a264b7495c9a6c1fcf530241eadf1635be6928d3d473e7
7
- data.tar.gz: d76942f091238687ae52cb3fbeffe1f6d61f3d93c3d3e926f3eb092936937ef5f4da5fcb58bcdff8868d4c89dbcf1b1220f3cd995e9df08d872aac081d2fb3af
6
+ metadata.gz: 3610f3f8aa0b79110ea17583191f4f5dec5a976eb8b9a6fc632bf614aa5a96d0b38508ecb80e1c6f4f35a7b65198c6c341634c171f53f732fce6278b38505580
7
+ data.tar.gz: d43bca63c94f9ef81c24752720c5e5d3d08c352c35c5c2905090a956882b20ffb0427908aa1bdc863ad5f4ddee7b476c2909ba1b57eef88fd731471fab363445
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 2.1.0
2
+
3
+ * Add `find_ids` and `find_id` helpers to base service
4
+
1
5
  ## 2.0.2
2
6
 
3
7
  * Make BaseFinder smarter, don't create SQL subquery if not necessary
data/lib/services/base.rb CHANGED
@@ -25,12 +25,23 @@ module Services
25
25
 
26
26
  private
27
27
 
28
- def find_objects(ids_or_objects, klass = object_class)
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
- def find_object(*args)
50
- find_objects(*args).tap do |objects|
51
- raise "Expected exactly one object but found #{objects.size}" unless objects.size == 1
52
- end.first
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
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '2.0.2'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -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
@@ -1,39 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Services::Base do
4
- let(:model_objects) { (1..5).to_a.shuffle.map { |id| Model.new(id) } }
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 eq(model_objects)
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(model_objects.map(&:id))).to eq(model_objects)
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
- objects_as_objects, objects_as_ids = model_objects.partition do |object|
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
- object = model_objects.sample
35
- [object.id, object].each do |id_or_object|
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
- 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)
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.2
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-25 00:00:00.000000000 Z
11
+ date: 2014-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake