Class: Quby::Compiler::DSL::ScoreSchemaBuilder

Inherits:
Base
  • Object
show all
Defined in:
lib/quby/compiler/dsl/score_schema_builder.rb

Instance Method Summary collapse

Methods inherited from Base

build

Methods included from Helpers

#check_question_keys_uniqueness, #image_alt, #image_tag, #video_tag

Constructor Details

#initialize(key:, label:, sbg_key: nil, &block) ⇒ ScoreSchemaBuilder

Returns a new instance of ScoreSchemaBuilder.



7
8
9
10
11
12
13
# File 'lib/quby/compiler/dsl/score_schema_builder.rb', line 7

def initialize(key:, label:, sbg_key: nil, &block)
  @score_key = key
  @score_label = label
  @score_sbg_key = sbg_key
  @subschema_params = []
  @generated_calculations = []
end

Instance Method Details

#buildObject



15
16
17
18
19
20
21
# File 'lib/quby/compiler/dsl/score_schema_builder.rb', line 15

def build
  score_schema = Entities::ScoreSchema.new key: @score_key,
                                           label: @score_label,
                                           sbg_key: @score_sbg_key,
                                           subscore_schemas: @subschema_params
  [score_schema, @generated_calculations + [generate_score_calculation(score_schema)]]
end

#subscore(key, label, **options, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/quby/compiler/dsl/score_schema_builder.rb', line 23

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