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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 174a80aa11d343dbbbf8a4e44293530534635159
4
- data.tar.gz: 733e7d7374a736b8d0d0abe8b6183118015e4539
3
+ metadata.gz: 29308e17d5df01f543e985f8aeeb933a0e22e66f
4
+ data.tar.gz: 8ccb2f4ca9d250a98a9b78e87d2a5291078a1e00
5
5
  SHA512:
6
- metadata.gz: 371972b84cd904629577b38de062ce71734d7d22a2ace45ae844e764128eee68648703c6073bb6ec4847d42505af56b36b2dd3554274b8a75847d06ec3ec5266
7
- data.tar.gz: ad3c0b04f60a7b35f84f5879db9661ee05040c0dcfc5c5cecdac6c9ad0b022f0d2782f8a739605e976ea3e74b9fe905c3b4647f21b25ec39d11e56c1b6e0a40b
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 = nil)
29
- if klass.nil?
30
- klass = self.class.to_s[/Services::([^:]+)/, 1].singularize.constantize rescue nil
31
- raise "Could not determine class from #{self.class}" if klass.nil?
32
- end
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
- ids_or_objects.is_a?(Array) ? objects : objects.first
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
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
@@ -1,24 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Services::Base do
4
- describe '#find_objects' do
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(objects)).to eq(objects)
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(objects.map(&:id))).to eq(objects)
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 = objects.partition do |object|
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 a single object' do
34
- object = objects.sample
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
@@ -38,6 +38,12 @@ module Services
38
38
  find_objects ids_or_objects
39
39
  end
40
40
  end
41
+
42
+ class FindObjectTest < Services::Base
43
+ def call(id_or_object)
44
+ find_object id_or_object
45
+ end
46
+ end
41
47
  end
42
48
  end
43
49
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer