Module: Resque::Helpers

Included in:
Resque, Job, Job, Stat, Worker, Worker
Defined in:
lib/resque/helpers.rb

Overview

Methods used by various classes in Resque.

Defined Under Namespace

Classes: DecodeException

Instance Method Summary collapse

Instance Method Details

#classify(dashed_word) ⇒ Object

Given a word with dashes, returns a camel cased version of it.

classify(‘job-name’) # => ‘JobName’



47
48
49
# File 'lib/resque/helpers.rb', line 47

def classify(dashed_word)
  dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join
end

#constantize(camel_cased_word) ⇒ Object

Tries to find a constant with the name specified in the argument string:

constantize(“Module”) # => Module constantize(“Test::Unit”) # => Test::Unit

The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:

C = ‘outside’ module M

C = 'inside'
C # => 'inside'
constantize("C") # => 'outside', same as ::C

end

NameError is raised when the constant is unknown.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/resque/helpers.rb', line 68

def constantize(camel_cased_word)
  camel_cased_word = camel_cased_word.to_s

  if camel_cased_word.include?('-')
    camel_cased_word = classify(camel_cased_word)
  end

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

  constant = Object
  names.each do |name|
    args = Module.method(:const_get).arity != 1 ? [false] : []

    if constant.const_defined?(name, *args)
      constant = constant.const_get(name)
    else
      constant = constant.const_missing(name)
    end
  end
  constant
end

#decode(object) ⇒ Object

Given a string, returns a Ruby object.



34
35
36
37
38
39
40
41
42
# File 'lib/resque/helpers.rb', line 34

def decode(object)
  return unless object

  begin
    ::MultiJson.decode(object)
  rescue ::MultiJson::DecodeError => e
    raise DecodeException, e.message, e.backtrace
  end
end

#encode(object) ⇒ Object

Given a Ruby object, returns a string suitable for storage in a queue.



29
30
31
# File 'lib/resque/helpers.rb', line 29

def encode(object)
  ::MultiJson.encode(object)
end

#mongo_statsObject



23
24
25
# File 'lib/resque/helpers.rb', line 23

def mongo_stats
  Resque.mongo_stats
end

#mongo_workersObject

Direct access to the Redis instance. def mongo

Resque.mongo

end



19
20
21
# File 'lib/resque/helpers.rb', line 19

def mongo_workers
  Resque.mongo_workers
end