Sha256: 27075ba9ccf8def9360dd0d9adbe9ef1c819975a64cec107dbeb5e1f44cc3bce

Contents?: true

Size: 1.4 KB

Versions: 34

Compression:

Stored size: 1.4 KB

Contents

require 'multi_json'

module Rewritten
  # Methods used by various classes in Resque.
  module Helpers
    class DecodeException < StandardError; end

    # Direct access to the Redis instance.
    def redis
      Resque.redis
    end

    # Given a Ruby object, returns a string suitable for storage in a
    # queue.
    def encode(object)
      ::MultiJson.encode(object)
    end

    # Given a string, returns a Ruby object.
    def decode(object)
      return unless object

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

    # Given a word with dashes, returns a camel cased version of it.
    #
    # classify('job-name') # => 'JobName'
    def classify(dashed_word)
      dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join
    end

    # Given a camel cased word, returns the constant it represents
    #
    # constantize('JobName') # => JobName
    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
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
rewritten-0.15.2 lib/rewritten/helpers.rb
rewritten-0.15.1 lib/rewritten/helpers.rb
rewritten-0.15.0 lib/rewritten/helpers.rb
rewritten-0.14.2 lib/rewritten/helpers.rb
rewritten-0.14.1 lib/rewritten/helpers.rb
rewritten-0.14.0 lib/rewritten/helpers.rb
rewritten-0.13.1 lib/rewritten/helpers.rb
rewritten-0.13.0 lib/rewritten/helpers.rb
rewritten-0.12.1 lib/rewritten/helpers.rb
rewritten-0.12.0 lib/rewritten/helpers.rb
rewritten-0.11.1 lib/rewritten/helpers.rb
rewritten-0.11.0 lib/rewritten/helpers.rb
rewritten-0.10.0 lib/rewritten/helpers.rb
rewritten-0.9.1 lib/rewritten/helpers.rb
rewritten-0.9.0 lib/rewritten/helpers.rb
rewritten-0.8.2 lib/rewritten/helpers.rb
rewritten-0.8.1 lib/rewritten/helpers.rb
rewritten-0.8.0 lib/rewritten/helpers.rb
rewritten-0.7.0 lib/rewritten/helpers.rb
rewritten-0.6.0 lib/rewritten/helpers.rb