Module: ActionSubscriber::Subscribable

Included in:
Base
Defined in:
lib/action_subscriber/subscribable.rb

Instance Method Summary collapse

Instance Method Details

#allow_low_priority_methods?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/action_subscriber/subscribable.rb', line 3

def allow_low_priority_methods?
  !!(::ActionSubscriber.configuration.allow_low_priority_methods)
end

#filter_low_priority_methods(methods) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/action_subscriber/subscribable.rb', line 7

def filter_low_priority_methods(methods)
  if allow_low_priority_methods?
    return methods
  else
    return methods - methods.grep(/_low/)
  end
end

#generate_queue_name(method_name) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/action_subscriber/subscribable.rb', line 15

def generate_queue_name(method_name)
  [
    local_application_name,
    remote_application_name,
    resource_name,
    method_name
  ].compact.join('.')
end

#generate_routing_key_name(method_name) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/action_subscriber/subscribable.rb', line 24

def generate_routing_key_name(method_name)
  [
    remote_application_name,
    resource_name,
    method_name
  ].compact.join('.')
end

#local_application_name(reload = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/action_subscriber/subscribable.rb', line 32

def local_application_name(reload = false)
  if reload || @_local_application_name.nil?
    @_local_application_name = case
                          when ENV['APP_NAME'] then
                            ENV['APP_NAME'].to_s.dup
                          when defined?(::Rails) then
                            if ::Rails.application.class.respond_to?(:module_parent_name)
                              ::Rails.application.class.module_parent_name.dup
                            else
                              ::Rails.application.class.parent_name.dup
                            end
                          else
                            raise "Define an application name (ENV['APP_NAME'])"
                          end

    @_local_application_name.downcase!
  end

  @_local_application_name
end

#queue_name_for_method(method_name) ⇒ Object

Build the `queue` for a given method.

If the queue name is not set, the queue name is

"local.remote.resoure.action"

Example

"bob.alice.user.created"


61
62
63
64
65
66
67
# File 'lib/action_subscriber/subscribable.rb', line 61

def queue_name_for_method(method_name)
  return queue_names[method_name] if queue_names[method_name]

  queue_name = generate_queue_name(method_name)
  queue_for(method_name, queue_name)
  return queue_name
end

#resource_nameObject

The name of the resource respresented by this subscriber. If the class name were `UserSubscriber` the resource_name would be `user`.



72
73
74
# File 'lib/action_subscriber/subscribable.rb', line 72

def resource_name
  @_resource_name ||= self.name.underscore.gsub(/_subscriber/, '').to_s
end

#routing_key_name_for_method(method_name) ⇒ Object

Build the `routing_key` for a given method.

If the routing_key name is not set, the routing_key name is

"remote.resoure.action"

Example

"amigo.user.created"


84
85
86
87
88
89
90
# File 'lib/action_subscriber/subscribable.rb', line 84

def routing_key_name_for_method(method_name)
  return routing_key_names[method_name] if routing_key_names[method_name]

  routing_key_name = generate_routing_key_name(method_name)
  routing_key_for(method_name, routing_key_name)
  return routing_key_name
end

#subscribable_methodsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/action_subscriber/subscribable.rb', line 92

def subscribable_methods
  return @_subscribable_methods if @_subscribable_methods

  methods = instance_methods
  methods -= ::Object.instance_methods

  self.included_modules.each do |mod|
    methods -= mod.instance_methods
  end

  @_subscribable_methods = filter_low_priority_methods(methods)
  @_subscribable_methods.sort!

  return @_subscribable_methods
end