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.

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’



41
42
43
# File 'lib/resque/helpers.rb', line 41

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

#constantize(camel_cased_word) ⇒ Object

Given a camel cased word, returns the constant it represents

constantize(‘JobName’) # => JobName



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/resque/helpers.rb', line 48

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|
    constant = constant.const_get(name) || constant.const_missing(name)
  end
  constant
end

#decode(object) ⇒ Object

Given a string, returns a Ruby object.



28
29
30
31
32
33
34
35
36
# File 'lib/resque/helpers.rb', line 28

def decode(object)
  return unless object

  if defined? Yajl
    Yajl::Parser.parse(object, :check_utf8 => false)
  else
    JSON.parse(object)
  end
end

#encode(object) ⇒ Object

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



19
20
21
22
23
24
25
# File 'lib/resque/helpers.rb', line 19

def encode(object)
  if defined? Yajl
    Yajl::Encoder.encode(object)
  else
    object.to_json
  end
end

#mongoObject

Direct access to the Redis instance.



5
6
7
# File 'lib/resque/helpers.rb', line 5

def mongo
  Resque.mongo
end

#mongo_statsObject



13
14
15
# File 'lib/resque/helpers.rb', line 13

def mongo_stats
  Resque.mongo_stats
end

#mongo_workersObject



9
10
11
# File 'lib/resque/helpers.rb', line 9

def mongo_workers
  Resque.mongo_workers
end