-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_generator.rb
executable file
·48 lines (39 loc) · 1.13 KB
/
code_generator.rb
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'state_machine'
require 'transition'
require 'erb'
class CodeGenerator
def initialize(state_machine)
@state_machine = state_machine
@steps = state_machine.states.keys
@initial_state = state_machine.get_initial
@actions = all_actions()
@handler_class_name = "DefaultHandler"
@stepper_class_name = "DefaultStepper"
@transitions = all_transitions()
end
def generate
handler_renderer = ERB.new(File.read("handler.rb.template"))
stepper_renderer = ERB.new(File.read("stepper.rb.template"))
File.open("#{@handler_class_name}.rb", 'w+') do |f|
f.write(handler_renderer.result(binding))
f.close
end
File.open("#{@stepper_class_name}.rb", 'w+') do |f|
f.write(stepper_renderer.result(binding))
f.close
end
end
private
def all_actions
actions = Array.new
all_transitions().each do |transition|
actions.push(transition.action)
end
return actions
end
def all_transitions
transitions = Array.new
@state_machine.to_state_array.each { |state| transitions.push(state.transitions.values) }
return transitions.flatten
end
end