Sha256: 21834cd4a81fa2986df96a7946b70933c677d78ce93496a2119d3c482cb9a6d4

Contents?: true

Size: 951 Bytes

Versions: 18

Compression:

Stored size: 951 Bytes

Contents

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

      def method_missing(question_name, *args) # rubocop:disable Style/MethodMissingSuper
        question_class_name = "#{question_name}_question".camelize
        question_class = "#{question_scope}::#{question_class_name}".constantize

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

      def respond_to_missing?(_question_name, *)
        true
      end

      class QuestionError < ::Screengem::Question
        include FactoryCreationError

        skip_dampening
      end

      private

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
screengem-0.11.0 lib/screengem/factories/question_factory.rb
screengem-0.10.1 lib/screengem/factories/question_factory.rb
screengem-0.10.0 lib/screengem/factories/question_factory.rb
screengem-0.9.1 lib/screengem/factories/question_factory.rb
screengem-0.9.0 lib/screengem/factories/question_factory.rb
screengem-0.8.1 lib/screengem/factories/question_factory.rb
screengem-0.8.0 lib/screengem/factories/question_factory.rb
screengem-0.7.3 lib/screengem/factories/question_factory.rb
screengem-0.7.2 lib/screengem/factories/question_factory.rb
screengem-0.7.1 lib/screengem/factories/question_factory.rb
screengem-0.6.0 lib/screengem/factories/question_factory.rb
screengem-0.5.0 lib/screengem/factories/question_factory.rb
screengem-0.4.0 lib/screengem/factories/question_factory.rb
screengem-0.3.0 lib/screengem/factories/question_factory.rb
screengem-0.2.0 lib/screengem/factories/question_factory.rb
screengem-0.1.2 lib/screengem/factories/question_factory.rb
screengem-0.1.1 lib/screengem/factories/question_factory.rb
screengem-0.1.0 lib/screengem/factories/question_factory.rb