Module: Resque::Helpers
- Defined in:
- lib/resque/helpers.rb
Overview
Methods used by various classes in Resque.
Defined Under Namespace
Classes: DecodeException
Class Method Summary collapse
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
Given a string, returns a Ruby object.
-
#encode(object) ⇒ Object
Given a Ruby object, returns a string suitable for storage in a queue.
-
#redis ⇒ Object
Direct access to the Redis instance.
Class Method Details
.extended(parent_class) ⇒ Object
14 15 16 |
# File 'lib/resque/helpers.rb', line 14 def self.extended(parent_class) warn("Resque::Helpers will be gone with no replacement in Resque 2.0.0.") end |
.included(parent_class) ⇒ Object
18 19 20 |
# File 'lib/resque/helpers.rb', line 18 def self.included(parent_class) warn("Resque::Helpers will be gone with no replacement in Resque 2.0.0.") end |
Instance Method Details
#classify(dashed_word) ⇒ Object
Given a word with dashes, returns a camel cased version of it.
classify(‘job-name’) # => ‘JobName’
63 64 65 |
# File 'lib/resque/helpers.rb', line 63 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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/resque/helpers.rb', line 84 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.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/resque/helpers.rb', line 46 def decode(object) return unless object begin if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load) MultiJson.load object else MultiJson.decode object end rescue ::MultiJson::DecodeError => e raise DecodeException, e., e.backtrace end end |
#encode(object) ⇒ Object
Given a Ruby object, returns a string suitable for storage in a queue.
37 38 39 40 41 42 43 |
# File 'lib/resque/helpers.rb', line 37 def encode(object) if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load) MultiJson.dump object else MultiJson.encode object end end |
#redis ⇒ Object
Direct access to the Redis instance.
25 26 27 28 29 30 31 32 33 |
# File 'lib/resque/helpers.rb', line 25 def redis # No infinite recursions, please. # Some external libraries depend on Resque::Helpers being mixed into # Resque, but this method causes recursions. If we have a super method, # assume it is canonical. (see #1150) return super if defined?(super) Resque.redis end |