Class: Reactor::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/subscription.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Subscription

Returns a new instance of Subscription.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reactor/subscription.rb', line 17

def initialize(options = {}, &block)
  @source = options[:source]
  @handler_name = self.class.build_handler_name(
    options[:event_name], options[:handler_name]
  )

  @event_name = options[:event_name]
  @action = options[:action] || block

  @delay = options[:delay].to_i
  @deprecated = !!options[:deprecated]
  @sidekiq_options = options[:sidekiq_options] || {}
  build_worker_class
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def action
  @action
end

#delayObject (readonly)

Returns the value of attribute delay.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def delay
  @delay
end

#deprecatedObject (readonly)

Returns the value of attribute deprecated.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def deprecated
  @deprecated
end

#event_nameObject (readonly)

Returns the value of attribute event_name.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def event_name
  @event_name
end

#handler_nameObject (readonly)

Returns the value of attribute handler_name.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def handler_name
  @handler_name
end

#sidekiq_optionsObject (readonly)

Returns the value of attribute sidekiq_options.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def sidekiq_options
  @sidekiq_options
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def source
  @source
end

#worker_classObject (readonly)

Returns the value of attribute worker_class.



4
5
6
# File 'lib/reactor/subscription.rb', line 4

def worker_class
  @worker_class
end

Class Method Details

.build_handler_name(event_name, handler_name_option = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/reactor/subscription.rb', line 7

def self.build_handler_name(event_name, handler_name_option = nil)
  if handler_name_option
    handler_name_option.to_s.camelize
  elsif event_name == '*'
    'WildcardHandler'
  else
    "#{event_name.to_s.camelize}Handler"
  end
end

Instance Method Details

#event_handler_namesObject



37
38
39
# File 'lib/reactor/subscription.rb', line 37

def event_handler_names
  @event_handler_names ||= []
end

#generate_namespaceObject



46
47
48
49
50
51
52
53
# File 'lib/reactor/subscription.rb', line 46

def generate_namespace
  module_chain.reduce(Reactor.subscriber_namespace) do |mod, name|
    unless mod.const_defined?(name, false)
      mod.const_set(name, Module.new)
    end
    mod.const_get(name)
  end
end

#handler_defined?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/reactor/subscription.rb', line 32

def handler_defined?
  namespace.const_defined?(handler_name) &&
    namespace.const_get(handler_name).parents.include?(Reactor.subscriber_namespace)
end

#mailer_subscriber?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/reactor/subscription.rb', line 55

def mailer_subscriber?
  !!(source < ActionMailer::Base)
end

#namespaceObject



41
42
43
44
# File 'lib/reactor/subscription.rb', line 41

def namespace
  return @namespace if @namespace
  @namespace = generate_namespace
end