Class: EnumStateMachine::YARD::Handlers::Machine

Inherits:
Base
  • Object
show all
Defined in:
lib/enum_state_machine/yard/handlers/machine.rb

Overview

Handles and processes #state_machine

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#machineObject (readonly)

The generated state machine



12
13
14
# File 'lib/enum_state_machine/yard/handlers/machine.rb', line 12

def machine
  @machine
end

Instance Method Details

#processObject



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
# File 'lib/enum_state_machine/yard/handlers/machine.rb', line 14

def process
  # Cross-file storage for state machines
  globals.state_machines ||= Hash.new {|h, k| h[k] = {}}
  namespace['state_machines'] ||= {}
  
  # Create new machine
  klass = inherited_machine ? Class.new(inherited_machine.owner_class) : Class.new { extend EnumStateMachine::MacroMethods }
  @machine = klass.state_machine(name, options) {}
  
  # Track the state machine
  globals.state_machines[namespace.name][name] = machine
  namespace['state_machines'][name] = {:name => name, :description => statement.docstring}
  
  # Parse the block
  parse_block(statement.last.last, :owner => machine)
  
  # Draw the machine for reference in the template
  file = Tempfile.new(['enum_state_machine', '.png'])
  begin
    if machine.draw(:name => File.basename(file.path, '.png'), :path => File.dirname(file.path), :orientation => 'landscape')
      namespace['state_machines'][name][:image] = file.read
    end
  ensure
    # Clean up tempfile
    file.close
    file.unlink
  end
  
  # Define auto-generated methods
  define_macro_methods
  define_state_methods
  define_event_methods
end