Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/camertron/ohm-stateful-model
Integrate state machines (from the state_machine gem) into your Ohm models.
https://github.com/camertron/ohm-stateful-model
Last synced: 18 days ago
JSON representation
Integrate state machines (from the state_machine gem) into your Ohm models.
- Host: GitHub
- URL: https://github.com/camertron/ohm-stateful-model
- Owner: camertron
- License: apache-2.0
- Created: 2013-09-14T06:12:09.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-09-14T07:06:17.000Z (over 11 years ago)
- Last Synced: 2024-11-04T00:42:04.242Z (2 months ago)
- Language: Ruby
- Size: 137 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: History.txt
- License: LICENSE
Awesome Lists containing this project
README
ohm-stateful-model
==================Integrate state machines (from the state_machine gem) into your Ohm models.
### Setup
Require the gem:
```ruby
require 'ohm/stateful_model'
```Define your state machine and inherit from `Ohm::State`:
```ruby
class VehicleState < Ohm::State
state_machine :state, :initial => :parked do
event :park do
transition :idling => :parked
end
event :start_car do
transition :parked => :idling
end
end
end
```Define your Ohm model by inheriting from `Ohm::StatefulModel`. Specify the state machine to use via the `use_state_machine` class method.
```ruby
class Vehicle < Ohm::StatefulModel
use_state_machine VehicleState
end
```You can specify an optional `:attribute_name` flag to be used as the state's model attribute. By default, the name of the state machine is used:
```ruby
class Vehicle < Ohm::StatefulModel
use_state_machine VehicleState, :attribute_name => :my_state
end
```### Usage
Once your state machine has been integrated into your model, you will be able to interact with it as if it were a model attribute. You will also be able to transition between states, persist the current state along with the rest of the model, and call any `StateMachine::Machine` methods:
```ruby
v = Vehicle.new
v.state # => "parked"v.can_start_car? # => true
v.start_car
v.state # => "idling"
v.idling? # => true
v.can_start_car? # => falsev.new? # => true
v.save
v.new? # => false# fetch persisted data
v2 = Vehicle[v.id]
v2.state # => "idling"
v2.idling? # => truev2.can_park? # => true
v2.park
v2.parked? # => true
v2.save
```### Requirements
ohm-stateful-model depends on the ohm gem, which requires a running redis instance.
### Running Tests
`bundle exec rspec`
### Authors
* Cameron C. Dutro: http://github.com/camertron
### Links
* state_machine gem: [https://github.com/pluginaweek/state_machine](https://github.com/pluginaweek/state_machine)
* ohm gem: [https://github.com/soveran/ohm](https://github.com/soveran/ohm)### License
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0