services 7.0.1 → 7.0.2

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
  SHA256:
3
- metadata.gz: eb51ec8db2b413530af5cdc1c9a95cd81f3a519cd2492dd9664ab4254028520c
4
- data.tar.gz: 3c140ae804c3a69105ce4d2ed1d7b0975e224a33a86afe9bd3c539d82ef23c54
3
+ metadata.gz: b065533d21d6ca4c70a84c9954013b30fe1117029815136e7255187374e90491
4
+ data.tar.gz: 94660090d080809e9b86e0ffaab444344797849fd6223395121590fa806bea58
5
5
  SHA512:
6
- metadata.gz: db4dc1a984b5c4e7aacbd9e88712b892cdbcd938101190283d68b93ec898261efab50c9cf634cb6ef4e432358e82039b1601be44bdebe4794a90c58b6ca863cd
7
- data.tar.gz: 7bcdfb0ae02c57ffe370988bccbfbef46eaa29edf7a7475cefaa32dc08f667bd8f69828b24b269a9bdcc73d74f342e62b553b610e958e38a7cba3647e829c34f
6
+ metadata.gz: 0e66ba451286220dfe6452de7c51d6580174f7c7e8b46201f9b69e8999baaf7429b83e289d5a9d37b14f2034a2cc91a4ec0be7009e78a18836dee9206985d12e
7
+ data.tar.gz: d83b44042182db49481a619f3546b06cd298c091daf4fc9f3c6a27a5e2da39b516441365dab12b5a8d4b980b0fc576456a24e6e38eb5b1a97f8372f8e2a53cff
data/README.md CHANGED
@@ -148,18 +148,13 @@ module Services
148
148
  class Find < Services::Query
149
149
  convert_condition_objects_to_ids :post
150
150
 
151
- private def process(scope, conditions)
152
- conditions.each do |k, v|
153
- case k
154
- when :email, :name
155
- scope = scope.where(k => v)
156
- when :post_id
157
- scope = scope.joins(:posts).where("#{Post.table_name}.id" => v)
158
- else
159
- raise ArgumentError, "Unexpected condition: #{k}"
160
- end
151
+ private def process(scope, condition, value)
152
+ case condition
153
+ when :email, :name
154
+ scope.where(condition => value)
155
+ when :post_id
156
+ scope.joins(:posts).where("#{Post.table_name}.id" => value)
161
157
  end
162
- scope
163
158
  end
164
159
  end
165
160
  end
@@ -21,7 +21,7 @@ module Services
21
21
  module ClassMethods
22
22
  ASYNC_METHOD_SUFFIXES.each do |async_method_suffix|
23
23
  define_method "call_#{async_method_suffix}" do |*args|
24
- args.map! do |arg|
24
+ args = args.map do |arg|
25
25
  arg.respond_to?(:to_global_id) ? arg.to_global_id : arg
26
26
  end
27
27
  self.public_send "perform_#{async_method_suffix}", *args
@@ -30,9 +30,20 @@ module Services
30
30
  end
31
31
 
32
32
  def perform(*args)
33
- args.map! do |arg|
33
+ args = args.map do |arg|
34
34
  GlobalID::Locator.locate(arg) || arg
35
35
  end
36
+ # If the `call` method takes any kwargs and the last argument is a hash, symbolize the hash keys,
37
+ # otherwise they won't be recognized as kwards when splatted.
38
+ # Since the arguments to `perform` are serialized to the database before Sidekiq picks them up,
39
+ # symbol keys are converted to strings.
40
+ call_method = method(:call)
41
+ while call_method.owner != self.class
42
+ call_method = call_method.super_method
43
+ end
44
+ if call_method.parameters.map(&:first).grep(/\Akey/).any? && args.last.is_a?(Hash)
45
+ args.last.symbolize_keys!
46
+ end
36
47
  call *args
37
48
  end
38
49
  end
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '7.0.1'.freeze
2
+ VERSION = '7.0.2'.freeze
3
3
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::Asyncable do
4
+ describe '#perform' do
5
+ it 'calls `call` with the correct args' do
6
+ expect { AsyncService.new.perform 'test', pelle: 'fant' }.to raise_error(%w(test baz fant).to_json)
7
+
8
+ # If the `call` method arguments contains kwargs and the last argument to `perform` is a Hash,
9
+ # it's keys should be symbolized. The reason is that the arguments to `perform` are serialized to
10
+ # the database before Sidekiq picks them up, i.e. symbol keys are converted to strings.
11
+ expect { AsyncService.new.perform 'test', 'pelle' => 'fant' }.to raise_error(%w(test baz fant).to_json)
12
+ end
13
+ end
14
+ end
@@ -29,8 +29,10 @@ module Services
29
29
 
30
30
  private
31
31
 
32
- def process(scope, conditions)
33
- raise conditions.to_json
32
+ def process(scope, condition, value)
33
+ if condition == :comment_id
34
+ raise({ condition => value }.to_json)
35
+ end
34
36
  end
35
37
  end
36
38
  end
@@ -43,18 +45,13 @@ module Services
43
45
 
44
46
  private
45
47
 
46
- def process(scope, conditions)
47
- conditions.each do |k, v|
48
- case k
49
- when :title, :body
50
- scope = scope.where(k => v)
51
- when :comment_id
52
- scope = scope.joins(:comments).where("#{Comment.table_name}.id" => v)
53
- else
54
- raise ArgumentError, "Unexpected condition: #{k}"
55
- end
48
+ def process(scope, condition, value)
49
+ case condition
50
+ when :title, :body
51
+ scope.where(condition => value)
52
+ when :comment_id
53
+ scope.joins(:comments).where("#{Comment.table_name}.id" => value)
56
54
  end
57
- scope
58
55
  end
59
56
  end
60
57
  end
@@ -67,16 +64,11 @@ module Services
67
64
 
68
65
  private
69
66
 
70
- def process(scope, conditions)
71
- conditions.each do |k, v|
72
- case k
73
- when :body, :post_id
74
- scope = scope.where(k => v)
75
- else
76
- raise ArgumentError, "Unexpected condition: #{k}"
77
- end
67
+ def process(scope, condition, value)
68
+ case condition
69
+ when :body, :post_id
70
+ scope.where(condition => value)
78
71
  end
79
- scope
80
72
  end
81
73
  end
82
74
  end
@@ -173,3 +173,9 @@ class NestedExceptionService < Services::Base
173
173
  end
174
174
  end
175
175
  end
176
+
177
+ class AsyncService < Services::Base
178
+ def call(foo, bar: 'baz', pelle:)
179
+ raise [foo, bar, pelle].to_json
180
+ end
181
+ end
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: 7.0.1
4
+ version: 7.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2018-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -210,6 +210,7 @@ files:
210
210
  - lib/services/railtie.rb
211
211
  - lib/services/version.rb
212
212
  - services.gemspec
213
+ - spec/services/asyncable_spec.rb
213
214
  - spec/services/base_spec.rb
214
215
  - spec/services/configuration_spec.rb
215
216
  - spec/services/logger/redis_spec.rb
@@ -251,6 +252,7 @@ signing_key:
251
252
  specification_version: 4
252
253
  summary: A nifty service layer for your Rails app
253
254
  test_files:
255
+ - spec/services/asyncable_spec.rb
254
256
  - spec/services/base_spec.rb
255
257
  - spec/services/configuration_spec.rb
256
258
  - spec/services/logger/redis_spec.rb