Module: PikaQue::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/pika_que/util.rb

Instance Method Summary collapse

Instance Method Details

#constantize(str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/pika_que/util.rb', line 7

def constantize(str)
  return str if (str.is_a?(Class) || str.is_a?(Module))

  names = str.split('::')
  names.shift if names.empty? || names.first.empty?

  names.inject(Object) do |constant, name|
    constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
end

#inflectorObject



44
45
46
# File 'lib/pika_que/util.rb', line 44

def inflector
  @inflector ||= Dry::Inflector.new
end

#register_worker_class(worker_name, base_class, queue_name, queue_opts = {}, handler_class = nil, handler_opts = {}, local_config = {}) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/pika_que/util.rb', line 18

def register_worker_class(worker_name, base_class, queue_name, queue_opts = {}, handler_class = nil, handler_opts = {}, local_config = {})
  Object.const_set(worker_name, Class.new(base_class) do
      from_queue queue_name, queue_opts         
      handle_with handler_class, handler_opts if handler_class
      config local_config if local_config.any?
    end
  )
end

#worker_classes(workers = []) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pika_que/util.rb', line 27

def worker_classes(workers = [])
  return [] if workers.nil?
  
  workers.map do |worker|
    if worker.is_a? Hash
      if worker[:worker]
        worker[:worker]
      else
        queue_name = worker[:queue_name] || worker[:queue]
        "#{inflector.classify(inflector.underscore(queue_name))}Worker"
      end
    else
      worker
    end
  end
end