la_gear 1.1.1 → 1.2.0

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: 503e95feb00357ec5d4b83d49d4a9676feb8c4bd
4
- data.tar.gz: 5a440b745bd414158e2e724aca64562452ced2b8
3
+ metadata.gz: c3b73127f098ada69cc2e0ab170a4da60d2207b6
4
+ data.tar.gz: d3e8b4e4352fbb3fbd4cd13eb3c4efabb3548cf0
5
5
  SHA512:
6
- metadata.gz: 5b521b51ddbdf2338caf2840f4bb1dec3d0eefcfe2590836d911f54742a58abcdede0c7e9dd42e764407c35b3f7a124173fc866e13aa6e94b03f4260e2b96ab5
7
- data.tar.gz: 718958b3be88d232a9659ff926eba097274705263c56863cd70ba381067d10958c7f86e481b3ee5f65dabac9265aa0e07f534abd5daee0d4200c75cfce3074e9
6
+ metadata.gz: c566ae31db9318a8c28a8e7b2d2d9eb075ae14c2c1eef9e1416e95a478309023d6f2e8b959c58028eb9dba5bdf5c416e02e3d183659398f0fe3180e2974d7af9
7
+ data.tar.gz: dbc6562784c5f9a44927f239d3e0e4ecd8a3ed5ede4b14dbdae5dc0ee50a5ed0fe9e92c7fe0d0f647f8a6365d0440b73358881b0b939114a729de9dc762d4ef1
data/la_gear.gemspec CHANGED
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'minitest', '~> 5.7.0'
29
29
  spec.add_development_dependency 'simplecov'
30
30
  spec.add_development_dependency 'rr'
31
+ spec.add_development_dependency 'pry'
31
32
  end
@@ -3,30 +3,51 @@ module LaGear
3
3
  module PublishTriggerable
4
4
  def publish_after_commit(routing_key, opts)
5
5
  publish_method = "publish_#{routing_key}"
6
+ la_gear_opts = extract_la_gear_options(opts)
6
7
  define_method publish_method do
7
8
  message = block_given? ? yield(self) : {}
8
- Bus.publish(routing_key.to_s, message)
9
+ Bus.publish(routing_key.to_s, message, la_gear_opts)
9
10
  end
10
11
  after_commit publish_method, opts
11
12
  end
12
13
 
13
14
  def send_after_commit(routing_key, opts)
14
15
  publish_method = "publish_#{routing_key}"
16
+ la_gear_opts = extract_la_gear_options(opts)
15
17
  define_method publish_method do
16
18
  message = block_given? ? yield(self) : {}
17
- Bus.publish_local(routing_key.to_s, message)
19
+ Bus.publish_local(routing_key.to_s, message, la_gear_opts)
18
20
  end
19
21
  after_commit publish_method, opts
20
22
  end
21
23
 
22
24
  def send_in_after_commit(routing_key, opts, interval)
23
25
  publish_method = "publish_#{routing_key}"
26
+ la_gear_opts = extract_la_gear_options(opts)
24
27
  define_method publish_method do
25
28
  message = block_given? ? yield(self) : {}
26
- Bus.publish_local_in(routing_key.to_s, message, interval)
29
+ Bus.publish_local_in(routing_key.to_s, message, la_gear_opts, interval)
27
30
  end
28
31
  after_commit publish_method, opts
29
32
  end
33
+
34
+ private
35
+
36
+ def extract_la_gear_options(opts = {})
37
+ la_gear_opts = {}
38
+
39
+ if opts.key?(:version)
40
+ la_gear_opts[:version] = opts[:version]
41
+ opts.delete(:version)
42
+ end
43
+
44
+ if opts.key?(:suffix)
45
+ la_gear_opts[:suffix] = opts[:suffix]
46
+ opts.delete(:suffix)
47
+ end
48
+
49
+ la_gear_opts
50
+ end
30
51
  end
31
52
  end
32
53
  end
data/lib/la_gear/bus.rb CHANGED
@@ -13,33 +13,70 @@ module LaGear
13
13
  end
14
14
  module_function :init_pool
15
15
 
16
- def publish(routing_key, msg, bunny_opts = {}, sidekiq_opts = {})
16
+ def publish(routing_key, msg, la_gear_opts = {}, bunny_opts = {}, sidekiq_opts = {})
17
+ routing_key = NamespaceUtility.adjust_routing_key(routing_key, la_gear_opts)
17
18
  DelayablePublisher.sidekiq_delay(sidekiq_opts).publish(routing_key, msg, bunny_opts)
18
19
  end
19
20
  module_function :publish
20
21
 
21
- def publish_in(interval, routing_key, msg, bunny_opts = {}, sidekiq_opts = {})
22
+ def publish_in(interval, routing_key, msg, la_gear_opts = {}, bunny_opts = {}, sidekiq_opts = {})
23
+ routing_key = NamespaceUtility.adjust_routing_key(routing_key, la_gear_opts)
22
24
  DelayablePublisher.sidekiq_delay_for(interval, sidekiq_opts).publish(routing_key, msg, bunny_opts)
23
25
  end
24
26
  module_function :publish_in
25
27
 
26
- def publish_at(timestamp, routing_key, msg, bunny_opts = {}, sidekiq_opts = {})
28
+ def publish_at(timestamp, routing_key, msg, la_gear_opts = {}, bunny_opts = {}, sidekiq_opts = {})
29
+ routing_key = NamespaceUtility.adjust_routing_key(routing_key, la_gear_opts)
27
30
  DelayablePublisher.sidekiq_delay_until(timestamp, sidekiq_opts).publish(routing_key, msg, bunny_opts)
28
31
  end
29
32
  module_function :publish_at
30
33
 
31
- def publish_local(routing_key, msg)
32
- routing_key.classify.constantize.perform_async(*msg.values)
34
+ def publish_local(routing_key, msg, la_gear_opts = {})
35
+ routing_key = NamespaceUtility.adjust_routing_key(routing_key, la_gear_opts)
36
+ NamespaceUtility.local_worker(routing_key).perform_async(*msg.values)
33
37
  end
34
38
  module_function :publish_local
35
39
 
36
- def publish_local_in(routing_key, msg, interval)
37
- routing_key.classify.constantize.perform_in(interval, *msg.values)
40
+ def publish_local_in(routing_key, msg, la_gear_opts = {}, interval)
41
+ routing_key = NamespaceUtility.adjust_routing_key(routing_key, la_gear_opts)
42
+ NamespaceUtility.local_worker(routing_key).perform_in(interval, *msg.values)
38
43
  end
39
44
  module_function :publish_local_in
40
45
 
41
46
  private
42
47
 
48
+ class NamespaceUtility
49
+ class << self
50
+ def local_worker(routing_key)
51
+ routing_key.split('.').map(&:classify).join('::').constantize
52
+ end
53
+
54
+ def adjust_routing_key(routing_key, opts = {})
55
+ if opts.key?(:version)
56
+ routing_key = add_version(routing_key, opts[:version])
57
+ opts.delete(:version)
58
+ elsif opts.key?(:suffix)
59
+ routing_key = add_suffix(routing_key, opts[:suffix])
60
+ end
61
+
62
+ opts.delete(:suffix)
63
+ routing_key
64
+ end
65
+
66
+ private
67
+
68
+ def add_version(routing_key, version)
69
+ return add_suffix(routing_key, "v#{version}") if version.present?
70
+ routing_key
71
+ end
72
+
73
+ def add_suffix(routing_key, suffix)
74
+ return "#{routing_key}.#{suffix}" if suffix.present?
75
+ routing_key
76
+ end
77
+ end
78
+ end
79
+
43
80
  class DelayablePublisher
44
81
  def self.publish(routing_key, msg, opts = {})
45
82
  opts = opts.merge(to_queue: routing_key)
@@ -1,3 +1,3 @@
1
1
  module LaGear
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -40,8 +40,7 @@ module LaGear
40
40
  end
41
41
 
42
42
  def routing_key
43
- # TODO: the splitting is a semantic smell...
44
- name.split('::').last.underscore
43
+ name.underscore.gsub('/', '.')
45
44
  end
46
45
 
47
46
  def retry_routing_key
data/test/helper.rb CHANGED
@@ -19,5 +19,7 @@ require 'sneakers'
19
19
  require 'la_gear'
20
20
 
21
21
  require 'active_support/all'
22
+ require 'pry'
22
23
 
23
24
  LaGear::Test = Minitest::Test
25
+ LaGear::Spec = Minitest::Spec