class Quby::Compiler::DSL::ScoreSchemaBuilder

Public Class Methods

new(key:, label:, &block) click to toggle source
# File lib/quby/compiler/dsl/score_schema_builder.rb, line 7
def initialize(key:, label:, &block)
  @score_key = key
  @score_label = label
  @subschema_params = []
  @generated_calculations = []
end

Public Instance Methods

build() click to toggle source
# File lib/quby/compiler/dsl/score_schema_builder.rb, line 14
def build
  score_schema = Entities::ScoreSchema.new key: @score_key,
                                           label: @score_label,
                                           subscore_schemas: @subschema_params
  [score_schema, @generated_calculations + [generate_score_calculation(score_schema)]]
end
subscore(key, label, **options, &block) click to toggle source
# File lib/quby/compiler/dsl/score_schema_builder.rb, line 21
def subscore(key, label, **options, &block)
  if block
    # Generate a key to go with the subscore calculation made from the given block and save it inside the schema
    calculation_key = :"_#{@score_key}.#{key}"
    calculation = Entities::ScoreCalculation.new calculation_key,
                                                 label: "#{@score_label} #{label}",
                                                 &block
    @generated_calculations << calculation
    options.merge!(calculation_key: calculation_key)
  end
  @subschema_params << options.merge(key: key, label: label)
end

Private Instance Methods

generate_score_calculation(score_schema) click to toggle source
# File lib/quby/compiler/dsl/score_schema_builder.rb, line 36
def generate_score_calculation(score_schema)
  inner_hash_string = score_schema.subscore_schemas.map do |subschema|
    ":#{subschema.key} => score(:'#{subschema.calculation_key}')"
  end.join(",\n  ")
  # method-source can only grab whole lines of implementation. For regular questionnaires
  # this means calculation.sourcecode includes the surrounding score/attention/completion/etc. call.
  # This surrounding call will be stripped after reading in the source in quby.
  # We add a fake `score do` call around this generated implementation so the stripping will work correctly
  score_code_string = "score do\n  {#{inner_hash_string}}\nend"
  Entities::ScoreCalculation.new score_schema.key,
                                 label: score_schema.label,
                                 score: true,
                                 ruby_string: score_code_string
end