Module: Resque::Helpers
Overview
Methods used by various classes in Resque.
Instance Method Summary collapse
-
#classify(dashed_word) ⇒ Object
Given a word with dashes, returns a camel cased version of it.
-
#constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string:.
- #decode(object) ⇒ Object
- #encode(object) ⇒ Object
-
#redis ⇒ Object
Direct access to the Redis instance.
Instance Method Details
#classify(dashed_word) ⇒ Object
Given a word with dashes, returns a camel cased version of it.
classify(‘job-name’) # => ‘JobName’
20 21 22 |
# File 'lib/resque/helpers.rb', line 20 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/resque/helpers.rb', line 41 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
13 14 15 |
# File 'lib/resque/helpers.rb', line 13 def decode(object) Resque.coder.decode(object) end |