Sha256: a0100c84eefd8b79bc5786b151459d359611f84df728a4b4f9ee413c8f3c1fa3

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

module Taskinator
  class CreateProcessWorker

    attr_reader :definition
    attr_reader :uuid
    attr_reader :args

    def initialize(definition_name, uuid, args)

      # convert to the module
      @definition = constantize(definition_name)

      # this will be uuid of the created process
      @uuid = uuid

      # convert to the typed arguments
      @args = Taskinator::Persistence.deserialize(args)

    end

    def perform
      @definition._create_process_(false, *@args, :uuid => @uuid).enqueue!
    end

    private

    # :nocov:
    def constantize(camel_cased_word)

      # borrowed from activesupport/lib/active_support/inflector/methods.rb

      names = camel_cased_word.split('::')

      # Trigger a built-in NameError exception including the ill-formed constant in the message.
      Object.const_get(camel_cased_word) if names.empty?

      # Remove the first blank element in case of '::ClassName' notation.
      names.shift if names.size > 1 && names.first.empty?

      names.inject(Object) do |constant, name|
        if constant == Object
          constant.const_get(name)
        else
          candidate = constant.const_get(name)
          next candidate if constant.const_defined?(name, false)
          next candidate unless Object.const_defined?(name)

          # Go down the ancestors to check if it is owned directly. The check
          # stops when we reach Object or the end of ancestors tree.
          constant = constant.ancestors.inject do |const, ancestor|
            break const    if ancestor == Object
            break ancestor if ancestor.const_defined?(name, false)
            const
          end

          # owner is in Object, so raise
          constant.const_get(name, false)
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
taskinator-0.3.1 lib/taskinator/create_process_worker.rb
taskinator-0.3.0 lib/taskinator/create_process_worker.rb
taskinator-0.2.0 lib/taskinator/create_process_worker.rb