Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
71328a98
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
71328a98
编写于
1月 18, 2010
作者:
M
Mikel Lindsaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixing up state machine docs
上级
6e45bf53
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
141 addition
and
88 deletion
+141
-88
activemodel/README
activemodel/README
+13
-0
activemodel/lib/active_model/state_machine.rb
activemodel/lib/active_model/state_machine.rb
+128
-88
未找到文件。
activemodel/README
浏览文件 @
71328a98
...
...
@@ -154,3 +154,16 @@ functionality from the following modules:
{Learn more}[link:classes/ActiveModel/Serialization.html]
* Turning your object into a finite State Machine
ActiveModel::StateMachine provides a clean way to include all the methods
you need to transform your object into a finite State Machine...
light = TrafficLight.new
light.current_state #=> :red
light.change_color! #=> true
light.current_state #=> :green
{Learn more}[link:classes/ActiveModel/StateMachine.html]
activemodel/lib/active_model/state_machine.rb
浏览文件 @
71328a98
module
ActiveModel
# ==== Examples
#
# class TrafficLight
# ActiveModel::StateMachine provides methods that turn your object into a
# finite state machine, able to move from one state to another.
#
# A minimal implementation could be:
#
# class EmailMessage
# include ActiveModel::StateMachine
#
#
# state_machine do
# state :red
# state :green
# state :yellow
# state :blink
#
# event :change_color do
# transitions :to => :red, :from => [:yellow],
# :on_transition => :catch_runners
# transitions :to => :green, :from => [:red]
# transitions :to => :yellow, :from => [:green]
# end
#
# event :defect do
# transitions :to => :blink, :from => [:yellow, :red, :green]
# end
#
# event :repair do
# transitions :to => :red, :from => [:blink]
# end
# state :unread
# state :read
# end
#
#
def catch_runners
#
puts "That'll be $250."
#
#
event :open_email do
#
transitions :to => :read, :from => :unread
# end
# end
#
# light = TrafficLight.new
# light.current_state # => :red
# light.change_color # => true
# light.current_state # => :green
# light.green? # => true
# light.change_color! # => true
# light.current_state # => :yellow
# light.red? # => false
# light.change_color # => true
# "That'll be $250."
#
#
# * The initial state for TrafficLight is red which is the first state defined.
#
# # Want to know the initial_state?
# TrafficLight.state_machine.initial_state # => :red
#
# * On a succesful transition to red (from yellow), the local +catch_runners+
# method is executed
#
# * The object acts differently depending on its current state, for instance,
# the change_color! method has a different action depending on the current
# color of the light
#
# * Get the possible events for a state
#
# TrafficLight.state_machine.events_for(:red) # => [:change_color, :defect]
# TrafficLight.state_machine.events_for(:blink) # => [:repair]
#
#
# === Examples
#
# class TrafficLight
# include ActiveModel::StateMachine
#
# attr_reader :runners_caught
#
# def initialize
# @runners_caught = 0
# end
#
# state_machine do
# state :red
# state :green
# state :yellow
# state :blink
#
# event :change_color do
# transitions :to => :red, :from => [:yellow],
# :on_transition => :catch_runners
# transitions :to => :green, :from => [:red]
# transitions :to => :yellow, :from => [:green]
# end
#
# event :defect do
# transitions :to => :blink, :from => [:yellow, :red, :green]
# end
#
# event :repair do
# transitions :to => :red, :from => [:blink]
# end
# end
#
# def catch_runners
# @runners_caught += 1
# end
# end
#
# light = TrafficLight.new
# light.current_state # => :red
# light.change_color! # => true
# light.current_state # => :green
# light.green? # => true
# light.change_color! # => true
# light.current_state # => :yellow
# light.red? # => false
# light.change_color! # => true
# light.runners_caught # => 1
#
# * The initial state for TrafficLight is red which is the first state defined.
#
# TrafficLight.state_machine.initial_state # => :red
#
# * Call an event to transition a state machine, e.g. <tt>change_color!</tt>.
# You can call the event with or without the exclamation mark, however, the common Ruby
# idiom is to name methods that directly change the state of the receivier with
# an exclamation mark, so <tt>change_color!</tt> is preferred over <tt>change_color</tt>.
#
# light.current_state #=> :green
# light.change_color! #=> true
# light.current_state #=> :yellow
#
# * On a succesful transition to red (from yellow), the local +catch_runners+
# method is executed
#
# light.current_state #=> :red
# light.change_color! #=> true
# light.runners_caught #=> 1
#
# * The object acts differently depending on its current state, for instance,
# the change_color! method has a different action depending on the current
# color of the light
#
# light.change_color! #=> true
# light.current_state #=> :red
# light.change_color! #=> true
# light.current_state #=> :green
#
# * Get the possible events for a state
#
# TrafficLight.state_machine.events_for(:red) # => [:change_color, :defect]
# TrafficLight.state_machine.events_for(:blink) # => [:repair]
#
# The StateMachine also supports the following features :
#
#
* Success callbacks on event transition
# * Success callbacks on event transition
#
# event :sample, :success => :we_win do
# ...
# end
#
# * Enter and exit callbacks par state
# event :sample, :success => :we_win do
# ...
# end
#
#
state :open, :enter => [:alert_twitter, :send_emails], :exit => :alert_twitter
#
* Enter and exit callbacks par state
#
#
* Guards on transition
#
state :open, :enter => [:alert_twitter, :send_emails], :exit => :alert_twitter
#
# event :close do
# # You may only close the store if the safe is locked!!
# transitions :to => :closed, :from => :open, :guard => :safe_locked?
# end
# * Guards on transition
#
# * Setting the initial state
#
# state_machine :initial => :yellow do
# ...
# end
# event :close do
# # You may only close the store if the safe is locked!!
# transitions :to => :closed, :from => :open, :guard => :safe_locked?
# end
#
#
* Named the state machine, to have more than on
e
#
* Setting the initial stat
e
#
# class Stated
# include ActiveModel::StateMachine
# state_machine :initial => :yellow do
# ...
# end
#
# strate_machine :name => :ontest do
# end
# * Named the state machine, to have more than one
#
# state_machine do
# end
# class Stated
# include ActiveModel::StateMachine
#
# strate_machine :name => :ontest do
# end
#
# state_machine do
# end
# end
#
# # Get the state of the <tt>:ontest</tt> state machine
# stat.current_state(:ontest)
# # Get the initial state
# Stated.state_machine(:ontest).initial_state
#
# # Get the state of the <tt>:ontest</tt> state machine
# stat.current_state(:ontest)
# # Get the initial state
# Stated.state_machine(:ontest).initial_state
#
# * Changing the state
# * Changing the state
#
#
stat.current_state(:default, :astate) # => :astate
#
# But you must give the name of the state machine, here <tt>:default</tt>
# stat.current_state(:default, :astate) # => :astate
# # But you must give the name of the state machine, here <tt>:default</tt>
#
module
StateMachine
autoload
:Event
,
'active_model/state_machine/event'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录