Class: AnswerFactory::Machines::MutateCodeblock

Inherits:
Machine
  • Object
show all
Defined in:
lib/machines/mutate_codeblock.rb

Instance Attribute Summary

Attributes inherited from Machine

#options

Instance Method Summary collapse

Methods inherited from Machine

#initialize

Constructor Details

This class inherits a constructor from AnswerFactory::Machines::Machine

Instance Method Details

#build(batch, overridden_options = {}) ⇒ Object Also known as: generate

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/machines/mutate_codeblock.rb', line 10

def build(batch, overridden_options={})
  raise ArgumentError, "MutateCodeblock#build cannot process a #{batch.class}" unless
    batch.kind_of?(Batch)
  
  all_options = @options.merge(overridden_options)
    
  replicates = all_options[:replicates] || 1
  
  result = Batch.new
  
  batch.each do |answer|
    replicates.times do
      which_point = rand(answer.points) + 1
      size = all_options[:size_preserving?] ?
        answer.program[which_point].points :
        Kernel.rand(2 * answer.program[which_point].points) + 1
      new_code = NudgeType::CodeType.any_value(all_options.merge({target_size_in_points:size}))
      mutated_code = answer.replace_point_or_clone(which_point, new_code)
      variant = Answer.new(mutated_code, progress:answer.progress + 1)
      result << variant
    end
  end
  
  return result
end