Sha256: 08942408f1d6fe988d90c1820a9f7441e2fb32e7bea7f7dfc14c8a045e762169

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

module Screengem
  module Factories
    #
    # Knows how to create a Question from a question name.
    #
    class QuestionFactory < BasicObject
      include ::Singleton

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

      class_eval <<~RUBY, __FILE__, __LINE__ + 1
        def method_missing(question_name, #{definition})
          question_class_name = "\#{question_name}_question".camelize
          question_class = "\#{question_scope}::\#{question_class_name}".constantize

          question_class.new(#{definition})
        rescue => e
          QuestionError.new(<<~MSG)
            Unable to create question: '\#{question_class_name}'.
              Details: \#{e.message}
          MSG
        end
      RUBY

      def respond_to_missing?(_question_name, *)
        true
      end

      class QuestionError < ::Screengem::Question
        include FactoryCreationError
      end

      private

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
screengem-0.21.2 lib/screengem/factories/question_factory.rb
screengem-0.21.1 lib/screengem/factories/question_factory.rb
screengem-0.21.0 lib/screengem/factories/question_factory.rb
screengem-0.20.1 lib/screengem/factories/question_factory.rb
screengem-0.20.0 lib/screengem/factories/question_factory.rb
screengem-0.19.0 lib/screengem/factories/question_factory.rb
screengem-0.18.1 lib/screengem/factories/question_factory.rb