ROR汇集---aasm
来源:互联网 发布:w7旗舰版系统优化教程 编辑:程序博客网 时间:2024/05/16 07:02
aasm的工作原理其实是状态机,与state_machine基本一致,关于state_machine的部分信息可以参考:http://flyingzl.javaeye.com/blog/362207
工作原理图:
以下内容转自:http://github.com/rubyist/aasm/tree/master
AASM - Ruby state machines
This package contains AASM, a library for adding finite state machines to Ruby classes.
AASM started as the acts_as_state_machine plugin but has evolved into a more generic library that no longer targets only ActiveRecord models.
AASM has the following features:
- States
- Machines
- Events
- Transitions
Download
The latest AASM can currently be pulled from the git repository on github.
- github.com/rubyist/aasm/tree/master
Installation
From GitHub hosted gems
% sudo gem sources -a http://gems.github.com # (you only need to do this once) % sudo gem install rubyist-aasm
Building your own gems
% rake gem % sudo gem install pkg/aasm-2.1.gem
Simple Example
Here’s a quick example highlighting some of the features.
A Slightly More Complex Example
This example uses a few of the more complex features available.
当状态机建立好后,我们来探讨一下它的用法:
1、与数据库关联起来。
如上所示,我们通过aasm_column把状态机与数据库表的aasm_state字段关联起来,当我们调用状态机事件时,aasm_state的值会发生相应的变化。
2、可以定义状态机的初始状态(aasm_initial_state),当插入数据时,如果不干预状态机,它会自动获取一个初始状态(预先定义的),并保存到数据库表。
3、Model.even与Model.even!的区别。
先看以下代码:
If you call the bang! form of the transition event method, the state will persist.
So callingbook.close
will set the state toclosed
, but will not automatically save. Callingbook.close!
will set the
state *and* automatically save the AR object.
4、其它用法:
参考:http://github.com/rubyist/aasm/blob/dfc2874972daf601f3eeb4cd2bff589290cc2521/spec/unit/aasm_spec.rb
注意:
倘若状态机跳转不符合要求。如事件wakeup触发的状态是从sleeping到showering,working的,但如果调用wakeup时,状态机当前的状态不为sleeping时,
就会出现异常。所以对于这些异常要注意捕获和处理。
- ROR汇集---aasm
- ROR汇集---Transaction
- ROR汇集---Routing
- ROR汇集---Rending
- ROR汇集---Layout
- ROR汇集---Render
- ROR汇集---will_paginate
- ROR汇集---Httparty
- ROR汇集---restful-authentication
- ROR汇集---memcached
- ROR汇集---easy-fckeditor
- ROR汇集---Permissions Control
- ROR汇集---Authlogic
- ROR
- ROR
- ROR
- ROR
- ror
- 用 Zend_Application 实现多模块 (multi-modules) 及多模板 (multi-templates) 应用程序
- [转]利用javascript实现web页面刷新的方法
- c++ Timer
- 发布了一个原创软件--代码生成器
- Linux基金会对Linux提出新规划 - Moblin2囊括其中
- ROR汇集---aasm
- apache mod_perl实现 url rewrite
- 推荐一部不错的电影 《当幸福来敲门》
- SelectSingleNode方法中的xpath用法
- C#Form打开一个新窗口的同时关闭这个窗体
- 序列化与反序列化及其应用举例
- 第四章 创建一个应用程序框架------------The Busy Coder's Guide to Android Development
- mp3 id3v2 中图片数据的获取
- windows句柄