Class: Keisanjaku::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/keisanjaku/engine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/keisanjaku/engine.rb', line 5

def index
  @index
end

#planObject (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.align_slide_index(step.side)
  when :move_slide_to
    state.align_slide_value(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

Returns:

  • (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.slide + (to.slide - from.slide) * ratio,
      cursor: from.cursor + (to.cursor - from.cursor) * ratio,
      face: to.face
    )
  end
end

#run_to_endObject



42
43
44
45
# File 'lib/keisanjaku/engine.rb', line 42

def run_to_end
  step_forward until done?
  state
end

#stateObject



15
16
17
# File 'lib/keisanjaku/engine.rb', line 15

def state
  @history[@index]
end

#step_backObject



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_forwardObject



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

#summaryObject



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