Class: JobBoss::Queuer

Inherits:
Object
  • Object
show all
Defined in:
lib/job_boss/queuer.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Queuer



3
4
5
6
# File 'lib/job_boss/queuer.rb', line 3

def initialize(attributes = nil)
  attributes[:priority] ||= 1
  @attributes = attributes || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/job_boss/queuer.rb', line 8

def method_missing(method_id, *args)
  require 'active_support'
  require 'job_boss/job'

  method_name = method_id.id2name

  if @classes && @controller
    # In here, we've already figured out the class, so assume the method_missing call is to the method
    actionable_class = @classes.detect {|controller_class| controller_class.respond_to?(method_name) }

    if actionable_class
      require 'job_boss/job'
      path = "#{@controller}##{method_name}"

      @class = nil
      @controller = nil

      Job.create(@attributes.merge(:path => path,
                                    :args => args))
    else
      raise ArgumentError, "Invalid action"
    end
  else
    # Check to see if there's a class
    @classes = []
    begin
      @classes << Kernel.const_get("#{method_name.classify}Jobs").new
    rescue NameError
    end

    begin
      @classes << Kernel.const_get("#{method_name.classify}")
    rescue
    end

    raise ArgumentError, "Invalid controller" if @classes.blank?

    @controller = method_name

    self
  end
end