Sha256: ec071a766010edd27e7d3ad12df9081ebb1b66ad42722af700331ffffb339b8d

Contents?: true

Size: 1.62 KB

Versions: 47

Compression:

Stored size: 1.62 KB

Contents

module Resque
  # Methods used by various classes in Resque.
  module Helpers
    # Direct access to the Redis instance.
    def mongo
      Resque.mongo
    end

    def mongo_workers
      Resque.mongo_workers
    end

    def mongo_stats
      Resque.mongo_stats
    end

    # Given a Ruby object, returns a string suitable for storage in a
    # queue.
    def encode(object)
      if defined? Yajl
        Yajl::Encoder.encode(object)
      else
        object.to_json
      end
    end

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

      if defined? Yajl
        begin
          Yajl::Parser.parse(object, :check_utf8 => false)
        rescue Yajl::ParseError
        end
      else
        begin
          JSON.parse(object)
        rescue JSON::ParserError
        end
      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

47 entries across 47 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.7.4 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.7.3 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.7.2 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.7.1 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.7.0 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.9 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.8 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.7 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.6 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.5 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.4 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.3 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.2 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.1 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.6.0 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.5.17 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.5.16 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.5.15 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb
classiccms-0.5.14 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/helpers.rb