minimal_state_machine 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,4 +16,5 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
 
19
- .DS_Store
19
+ .DS_Store
20
+ minimal_state_machine.sqlite3
data/README.md CHANGED
@@ -1,24 +1,74 @@
1
1
  # MinimalStateMachine
2
2
 
3
- TODO: Write a gem description
3
+ A state machine for ActiveRecord implemented with the state pattern
4
4
 
5
- ## Installation
5
+ ## Usage
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Add to Gemfile:
8
8
 
9
- gem 'minimal_state_machine'
9
+ ```ruby
10
+ gem 'minimal_state_machine'
11
+ ```
10
12
 
11
- And then execute:
13
+ Run:
12
14
 
13
- $ bundle
15
+ ```ruby
16
+ bundle install
17
+ rails generate minimal_state_machine
18
+ rake db:migrate
19
+ ```
14
20
 
15
- Or install it yourself as:
21
+ ## Quick Start
16
22
 
17
- $ gem install minimal_state_machine
23
+ Let's say we have an Issue ActiveRecord model and we want to turn it into a state machine with 3 possible states: open, closed, solved
18
24
 
19
- ## Usage
25
+ ```ruby
26
+ class Issue < ActiveRecord::Base
27
+ include MinimalStateMachine
28
+
29
+ def self.states
30
+ { :open => IssueState::Open, :closed => IssueState::Closed, :solved => IssueState::Solved }
31
+ end
32
+
33
+ def self.initial_state
34
+ 'open'
35
+ end
36
+ end
37
+
38
+ class IssueState::Open < MinimalStateMachine::State
39
+ def self.valid_transition_states
40
+ %w(closed)
41
+ end
42
+ end
43
+
44
+ class IssueState::Closed < MinimalStateMachine::State
45
+ def self.valid_transition_states
46
+ %w(solved open)
47
+ end
48
+ end
49
+
50
+ class IssueState::Solved < MinimalStateMachine::State; end
51
+ ```
52
+
53
+ As you can se we define the possible states with an Hash including both the state names and the classes representing the states.
54
+ We can provide an optional `initial_state` class method to indicate the initial state (the default value is the first of the keys in the states Hash)
55
+
56
+ What would happen with this configuration:
57
+
58
+ ```ruby
59
+ Issue.new.state
60
+ => #<IssueState::Open id: nil, type: "IssueState::Open", state_machine_id: nil, state_machine_type: "Issue", created_at: nil, updated_at: nil>
61
+
62
+ issue = Issue.new
63
+ issue.save
64
+ issue.state_name = 'closed'
65
+ issue.state
66
+ => #<IssueState::Closed id: 1, type: "IssueState::Closed", state_machine_id: 1, state_machine_type: "Issue", created_at: "2013-01-03 19:13:31", updated_at: "2013-01-03 19:13:31">
67
+ ```
68
+
69
+ If you try to change the state to a non valid state a `MinimalStateMachine::InvalidTransitionError` will be raised.
20
70
 
21
- TODO: Write usage instructions here
71
+ If you try to set the state to a non declared state a `MinimalStateMachine::InvalidStateError` will be raised.
22
72
 
23
73
  ## Contributing
24
74
 
@@ -1,3 +1,3 @@
1
1
  module MinimalStateMachine
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -34,7 +34,6 @@ module MinimalStateMachine
34
34
  if state.class.valid_transition_states.include?(state_name)
35
35
  state.destroy
36
36
  self.state = self.class.states[state_name.to_sym].new
37
- save
38
37
  else
39
38
  raise InvalidTransitionError
40
39
  end
@@ -47,6 +47,7 @@ describe StateMachine do
47
47
 
48
48
  it 'changes state' do
49
49
  @state_machine.state_name = 'closed'
50
+ @state_machine.save
50
51
 
51
52
  @state_machine.state.should be_a(StateMachineClosed)
52
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimal_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -88,7 +88,6 @@ files:
88
88
  - LICENSE.txt
89
89
  - README.md
90
90
  - Rakefile
91
- - db/minimal_state_machine.sqlite3
92
91
  - lib/generators/minimal_state_machine/minimal_state_machine_generator.rb
93
92
  - lib/generators/minimal_state_machine/templates/create_msm_states.rb
94
93
  - lib/minimal_state_machine.rb
Binary file