Sha256: a50442d62cf0c41a69fdc105e4521f97563ec983c5a631e84801cd77fd8ca769

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 KB

Contents

module Screengem
  #
  # Knows how to generate the sample dampen configuration.
  #
  class DampenConfigurationGenerator
    include Screengem::PrimitiveKey

    # Seconds to sleep after executing a question or task.
    DEFAULT_SLEEP = 2

    def generate
      configuration = empty_configuration

      default_configuration = configuration["default"]

      default_configuration["questions"] = all_questions
      default_configuration["tasks"] = all_tasks

      configuration
    end

    private

    def empty_configuration
      {
        "default" => {
          "questions" => {},
          "tasks" => {}
        }
      }
    end

    def all_questions
      all_primitives(Screengem::Question)
    end

    def all_tasks
      all_primitives(Screengem::Task)
    end

    def all_primitives(primitive_root)
      sorted_descendents(primitive_root).each_with_object({}) do |primitive_class, memo|
        next unless apply_dampening?(primitive_class)

        memo[primitive_key(primitive_class.name)] = DEFAULT_SLEEP
      end
    end

    def sorted_descendents(primitive_root)
      primitive_root.descendants.sort_by { |primitive_class| primitive_key(primitive_class.name) }
    end

    def apply_dampening?(primitive_class)
      primitive_class.supports_dampening? && primitive_class.descendants.empty?
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
screengem-0.16.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.15.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.14.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.13.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.12.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.11.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.10.1 lib/screengem/dampen_configuration_generator.rb
screengem-0.10.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.9.1 lib/screengem/dampen_configuration_generator.rb
screengem-0.9.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.8.1 lib/screengem/dampen_configuration_generator.rb
screengem-0.8.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.7.3 lib/screengem/dampen_configuration_generator.rb
screengem-0.7.2 lib/screengem/dampen_configuration_generator.rb
screengem-0.7.1 lib/screengem/dampen_configuration_generator.rb
screengem-0.6.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.5.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.4.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.3.0 lib/screengem/dampen_configuration_generator.rb
screengem-0.2.0 lib/screengem/dampen_configuration_generator.rb