Module: Rewritten::Helpers

Included in:
Rewritten
Defined in:
lib/rewritten/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’



33
34
35
# File 'lib/rewritten/helpers.rb', line 33

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rewritten/helpers.rb', line 40

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.



20
21
22
23
24
25
26
27
28
# File 'lib/rewritten/helpers.rb', line 20

def decode(object)
  return unless object

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

#encode(object) ⇒ Object

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



15
16
17
# File 'lib/rewritten/helpers.rb', line 15

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

#redisObject

Direct access to the Redis instance.



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

def redis
  Resque.redis
end