Sha256: e7ebc1f5743ca7eda4aeccd2e40d10466b8044dc1d63d635334c871a9a2d7237

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

module Screengem
  module Factories
    #
    # Knows how to create a Task from a task name.
    #
    class TaskFactory < BasicObject
      include ::Singleton

      definition = ::RUBY_VERSION >= "2.7" ? "..." : "*args"

      class_eval <<~RUBY, __FILE__, __LINE__ + 1
        def method_missing(task_name, #{definition})
          task_class_name = "\#{task_name}_task".camelize
          task_class = "\#{task_scope}::\#{task_class_name}".constantize

          task_class.new(#{definition})
        rescue => e
          TaskError.new(<<~MSG)
            Unable to create task: '\#{task_class_name}'.
              Details: \#{e.message}
          MSG
        end
      RUBY

      def respond_to_missing?(_task_name, *)
        true
      end

      class TaskError < ::Screengem::Task
        include FactoryCreationError
      end

      private

      def task_scope
        @task_scope ||= ::Screengem.configuration.task_scope
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
screengem-0.18.0 lib/screengem/factories/task_factory.rb