services 0.2.2 → 0.2.4

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: ad10bcd2778a3534fbc648b1dd2610a05037e7ce
4
- data.tar.gz: 052a46223424bb49023619f47ba7a5a031ce49c4
3
+ metadata.gz: 174a80aa11d343dbbbf8a4e44293530534635159
4
+ data.tar.gz: 733e7d7374a736b8d0d0abe8b6183118015e4539
5
5
  SHA512:
6
- metadata.gz: b8ef850720eee935e3009f2926437ae2639e8145cbde40498a616d3fcd813093f894b663b3c3956516f5cc183745fe578f07e1174b5bae7765aa2b0663fc052b
7
- data.tar.gz: 1837efa6d130430ef7420b3cd4fdbeb0d9c6936b73840734adc242270aa9b336b4450bde0ceaf13f9c7d8af2121ecfc8e6c223ab0e302de6bdc8091636297aa1
6
+ metadata.gz: 371972b84cd904629577b38de062ce71734d7d22a2ace45ae844e764128eee68648703c6073bb6ec4847d42505af56b36b2dd3554274b8a75847d06ec3ec5266
7
+ data.tar.gz: ad3c0b04f60a7b35f84f5879db9661ee05040c0dcfc5c5cecdac6c9ad0b022f0d2782f8a739605e976ea3e74b9fe905c3b4647f21b25ec39d11e56c1b6e0a40b
data/lib/services/base.rb CHANGED
@@ -25,28 +25,31 @@ module Services
25
25
 
26
26
  private
27
27
 
28
- def find_object(ids_or_objects, klass = nil)
28
+ def find_objects(ids_or_objects, klass = nil)
29
29
  if klass.nil?
30
30
  klass = self.class.to_s[/Services::([^:]+)/, 1].singularize.constantize rescue nil
31
31
  raise "Could not determine class from #{self.class}" if klass.nil?
32
32
  end
33
- case ids_or_objects
34
- when klass
35
- return ids_or_objects
36
- when Array
37
- raise 'Array can only contain IDs.' if ids_or_objects.any? { |ids_or_object| !ids_or_object.is_a?(Fixnum) }
38
- objects = "Services::#{klass.to_s.pluralize}::Find".constantize.call(ids_or_objects)
39
- missing_ids = ids_or_objects - objects.pluck(:id)
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)}"
37
+ end
38
+ if ids.any?
39
+ find_service = "Services::#{klass.to_s.pluralize}::Find"
40
+ objects_from_ids = find_service.constantize.call(ids)
41
+ object_ids = if objects_from_ids.respond_to?(:pluck)
42
+ objects_from_ids.pluck(:id)
43
+ else
44
+ objects_from_ids.map(&:id)
45
+ end
46
+ missing_ids = ids - object_ids
40
47
  raise self.class::Error, "#{klass.to_s.pluralize(missing_ids)} #{missing_ids.join(', ')} not found." if missing_ids.size > 0
41
- return objects
42
- when Fixnum
43
- object = "Services::#{klass.to_s.pluralize}::Find".constantize.call(ids_or_objects).first
44
- raise self.class::Error, "#{klass} #{ids_or_objects} not found." if object.nil?
45
- return object
46
- else
47
- raise "Unexpected ids_or_objects class: #{ids_or_objects.class}"
48
+ objects.concat objects_from_ids
48
49
  end
50
+ ids_or_objects.is_a?(Array) ? objects : objects.first
49
51
  end
52
+ alias_method :find_object, :find_objects
50
53
 
51
54
  def controller
52
55
  @controller ||= begin
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -1,18 +1,51 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Services::Base do
4
- class ServiceWithError < Services::Base
5
- def call
6
- raise Error.new('I am a service error.')
4
+ describe '#find_objects' do
5
+ let(:objects) { (1..5).to_a.shuffle.map { |id| Model.new(id) } }
6
+
7
+ context 'when passing in objects' do
8
+ it 'returns the same objects' do
9
+ expect(Services::Models::FindObjectsTest.call(objects)).to eq(objects)
10
+ end
11
+ end
12
+
13
+ context 'when passing in IDs' do
14
+ it 'returns the objects for the IDs' do
15
+ expect(Services::Models::FindObjectsTest.call(objects.map(&:id))).to eq(objects)
16
+ end
17
+ end
18
+
19
+ context 'when passing in objects and IDs' do
20
+ it 'returns the objects plus the objects for the IDs' do
21
+ objects_as_objects, objects_as_ids = 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)
29
+ end
30
+ end
31
+
32
+ context 'when passing in a single object or ID' do
33
+ it 'returns a single object' do
34
+ object = objects.sample
35
+ [object.id, object].each do |id_or_object|
36
+ expect(Services::Models::FindObjectsTest.call(id_or_object)).to eq(object)
37
+ end
38
+ end
7
39
  end
8
40
  end
9
- if ServiceWithError::Error.new.respond_to?(:cause)
41
+
42
+ if StandardError.new.respond_to?(:cause)
10
43
  context 'wrapping exceptions' do
11
44
  it 'does not wrap service errors or subclasses' do
12
45
  expect do
13
- ServiceWithError.call
46
+ ErrorService.call
14
47
  end.to raise_error do |error|
15
- expect(error).to be_a(ServiceWithError::Error)
48
+ expect(error).to be_a(ErrorService::Error)
16
49
  expect(error.message).to eq('I am a service error.')
17
50
  expect(error.cause).to be_nil
18
51
  end
@@ -1,3 +1,52 @@
1
+ class Model
2
+ attr_reader :id
3
+
4
+ def initialize(id)
5
+ @id = id
6
+ ModelRepository.add self
7
+ end
8
+
9
+ def ==(another_model)
10
+ self.id == another_model.id
11
+ end
12
+ end
13
+
14
+ class ModelRepository
15
+ def self.add(model)
16
+ @models ||= []
17
+ @models << model
18
+ end
19
+
20
+ def self.find(id)
21
+ return nil unless defined?(@models)
22
+ @models.detect do |model|
23
+ model.id == id
24
+ end
25
+ end
26
+ end
27
+
28
+ module Services
29
+ module Models
30
+ class Find < Services::Base
31
+ def call(ids)
32
+ ids.map { |id| ModelRepository.find id }.compact
33
+ end
34
+ end
35
+
36
+ class FindObjectsTest < Services::Base
37
+ def call(ids_or_objects)
38
+ find_objects ids_or_objects
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ class ErrorService < Services::Base
45
+ def call
46
+ raise Error.new('I am a service error.')
47
+ end
48
+ end
49
+
1
50
  class UniqueService < Services::Base
2
51
  def call
3
52
  check_uniqueness!
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: 0.2.2
4
+ version: 0.2.4
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-08-03 00:00:00.000000000 Z
11
+ date: 2014-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake