Skip to content

Instantly share code, notes, and snippets.

@madbox
Created January 29, 2011 16:26
Show Gist options
  • Save madbox/801966 to your computer and use it in GitHub Desktop.
Save madbox/801966 to your computer and use it in GitHub Desktop.
ticket.rb
# -*- coding: utf-8 -*-
class Ticket
# Дложно выполняться условие:
# Пары Edges[0], Edges[2] должны быть уникальны.
Edges = [ [ :new, :open, 'confirm' ],
[ :new, :closed, 'throw_away' ],
[ :open, :assigned, 'assign' ],
[ :assigned, :solved, 'solve' ],
[ :solved, :closed, 'close' ],
[ :solved, :assigned, 'assign' ],
[ :closed, :open, 'reopen' ] ]
def initialize
@state = Edges[0].first
end
def available_actions
Edges.select{ |e| e.first == @state }
end
def method_missing method_name, *args
# Проверяем имя метода. Если 'do_*****', то дергаем +do_action+
action = method_name.to_s[3..-1]
if available_actions.map{ |a| a.last }.include? action
self.send 'do_action', action
else
super
end
end
private
def do_action action_name
puts "Performing '#{action_name}'"
@state = Edges.find{ |e| e.last == action_name }[1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment