Sha256: 5dfbb01ae934e14d9bdecbac852093454c0495628d8b5f94a0bb98618f0c4ff0

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

module Proletariat
  # Internal: Helper utility to parse and constantize strings into arrays of
  #           Worker classes.
  module WorkerDescriptionParser
    # Public: Parse given string into array of Worker classes.
    #
    # description - String to be parsed. Should contain comma-separated class
    #               names.
    #
    # Examples
    #
    #   WorkerDescriptionParser.parse('FirstWorker,SecondWorker')
    #   # => [FirstWorker, SecondWorker]
    #
    # Returns an Array of Worker classes.
    def self.parse(description)
      description.split(',').map(&:strip).map do |string|
        constantize string
      end.compact
    end

    private

    # Intenal: Performs constantizing of worker names into Classes.
    #
    # name - The name to be constantized.
    #
    # Returns the Worker class if valid.
    # Returns the nil if Worker class cannot be found.
    def self.constantize(name)
      name.split('::').reduce(Object) { |a, e| a.const_get(e) }
    rescue NameError => e
      Proletariat.logger.warn "Missing worker class: #{e.name}"
      nil
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
proletariat-0.1.2 lib/proletariat/util/worker_description_parser.rb
proletariat-0.1.1 lib/proletariat/util/worker_description_parser.rb
proletariat-0.1.0 lib/proletariat/util/worker_description_parser.rb
proletariat-0.0.6 lib/proletariat/util/worker_description_parser.rb
proletariat-0.0.5 lib/proletariat/util/worker_description_parser.rb
proletariat-0.0.4 lib/proletariat/util/worker_description_parser.rb
proletariat-0.0.3 lib/proletariat/util/worker_description_parser.rb