Class: Keisanjaku::Engine
- Inherits:
-
Object
- Object
- Keisanjaku::Engine
- Defined in:
- lib/keisanjaku/engine.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#plan ⇒ Object
readonly
Returns the value of attribute plan.
Class Method Summary collapse
Instance Method Summary collapse
- #done? ⇒ Boolean
- #frames_for(step, from:, to:, count: 8) ⇒ Object
-
#initialize(plan, initial_state: RuleState.new, animate: true) ⇒ Engine
constructor
A new instance of Engine.
- #run_to_end ⇒ Object
- #state ⇒ Object
- #step_back ⇒ Object
- #step_forward ⇒ Object
- #summary ⇒ Object
Constructor Details
#initialize(plan, initial_state: RuleState.new, animate: true) ⇒ Engine
Returns a new instance of Engine.
7 8 9 10 11 12 13 |
# File 'lib/keisanjaku/engine.rb', line 7 def initialize(plan, initial_state: RuleState.new, animate: true) @plan = plan @initial_state = initial_state.dup @history = [@initial_state.dup] @index = 0 @animate = animate end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
5 6 7 |
# File 'lib/keisanjaku/engine.rb', line 5 def index @index end |
#plan ⇒ Object (readonly)
Returns the value of attribute plan.
5 6 7 |
# File 'lib/keisanjaku/engine.rb', line 5 def plan @plan end |
Class Method Details
.apply_step(state, step) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/keisanjaku/engine.rb', line 74 def self.apply_step(state, step) case step.primitive when :move_cursor state.move_cursor_to(step.scale, step.value) when :move_slide_index state.(step.side) when :move_slide_to state.(step.scale, step.value) when :flip_slide state.flip! when :read state else raise ArgumentError, "unknown step primitive: #{step.primitive}" end state end |
Instance Method Details
#done? ⇒ Boolean
19 20 21 |
# File 'lib/keisanjaku/engine.rb', line 19 def done? @index >= plan.steps.length end |
#frames_for(step, from:, to:, count: 8) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/keisanjaku/engine.rb', line 47 def frames_for(step, from:, to:, count: 8) return [to] unless @animate && movement_step?(step) (1..count).map do |n| ratio = n.to_f / count RuleState.new( slide: from. + (to. - from.) * ratio, cursor: from.cursor + (to.cursor - from.cursor) * ratio, face: to.face ) end end |
#run_to_end ⇒ Object
42 43 44 45 |
# File 'lib/keisanjaku/engine.rb', line 42 def run_to_end step_forward until done? state end |
#state ⇒ Object
15 16 17 |
# File 'lib/keisanjaku/engine.rb', line 15 def state @history[@index] end |
#step_back ⇒ Object
35 36 37 38 39 40 |
# File 'lib/keisanjaku/engine.rb', line 35 def step_back return nil if @index.zero? @index -= 1 plan.steps[@index] end |
#step_forward ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/keisanjaku/engine.rb', line 23 def step_forward return nil if done? next_state = state.dup step = plan.steps[@index] self.class.apply_step(next_state, step) @history = @history.take(@index + 1) @history << next_state @index += 1 step end |
#summary ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/keisanjaku/engine.rb', line 60 def summary error = if plan.true_value.zero? 0.0 else ((plan.read_value - plan.true_value) / plan.true_value * 100.0).abs end { read_value: plan.read_value, true_value: plan.true_value, error_percent: error, place_explanation: plan.place_explanation } end |