Class: FSM::Transition

Inherits:
Object
  • Object
show all
Includes:
Options::InstanceMethods
Defined in:
lib/fsm/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options::InstanceMethods

#assert_options

Constructor Details

#initialize(name, from, to, options = {}) ⇒ Transition

Returns a new instance of Transition.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/fsm/transition.rb', line 5

def initialize(name, from, to, options = {})
  raise ArgumentError.new("name, from and to are required but were '#{name}', '#{from}' and '#{to}'") unless name && from && to
  assert_options(options, [:event, :guard])
  self.name = name
  self.from = from
  self.to = to
  self.event = Executable.new options[:event] if options.has_key?(:event)
  self.guard = Executable.new options[:guard] if options.has_key?(:guard)
end

Instance Attribute Details

#eventObject

Returns the value of attribute event.



4
5
6
# File 'lib/fsm/transition.rb', line 4

def event
  @event
end

#fromObject

Returns the value of attribute from.



4
5
6
# File 'lib/fsm/transition.rb', line 4

def from
  @from
end

#guardObject

Returns the value of attribute guard.



4
5
6
# File 'lib/fsm/transition.rb', line 4

def guard
  @guard
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/fsm/transition.rb', line 4

def name
  @name
end

#toObject

Returns the value of attribute to.



4
5
6
# File 'lib/fsm/transition.rb', line 4

def to
  @to
end

Instance Method Details

#fire?(target, args) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fsm/transition.rb', line 19

def fire?(target, args)
  self.guard ? self.guard.execute(target, *args) : true
end

#fire_event(target, args) ⇒ Object



15
16
17
# File 'lib/fsm/transition.rb', line 15

def fire_event(target, args)
  self.event.execute(target, *args) if self.event
end

#to_dot(options = {}) ⇒ Object



27
28
29
# File 'lib/fsm/transition.rb', line 27

def to_dot(options = {})
  "#{self.from.name} -> #{self.to.name} [label=\"#{self.name}\"]"
end

#to_sObject



23
24
25
# File 'lib/fsm/transition.rb', line 23

def to_s
  "Transition from #{self.from.name} -> #{self.to.name} with event #{self.event}"
end