stateflow 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest CHANGED
@@ -2,10 +2,14 @@ LICENCE
2
2
  Manifest
3
3
  README.rdoc
4
4
  Rakefile
5
+ examples/robot.rb
5
6
  examples/test.rb
6
7
  init.rb
7
8
  lib/stateflow.rb
8
9
  lib/stateflow/event.rb
9
10
  lib/stateflow/machine.rb
11
+ lib/stateflow/persistence.rb
12
+ lib/stateflow/persistence/active_record.rb
13
+ lib/stateflow/persistence/mongo_mapper.rb
10
14
  lib/stateflow/state.rb
11
15
  lib/stateflow/transition.rb
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('stateflow', '0.0.1') do |p|
5
+ Echoe.new('stateflow', '0.0.2') do |p|
6
6
  p.description = "State machine that allows dynamic transitions for business workflows"
7
7
  p.url = "http://github.com/ryanza/stateflow"
8
8
  p.author = "Ryan Oberholzer"
data/examples/robot.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'stateflow'
3
+
4
+ class Robot
5
+ include Stateflow
6
+
7
+ stateflow do
8
+ initial :green
9
+
10
+ state :green, :yellow, :red
11
+
12
+ event :change_color do
13
+ transitions :from => :green, :to => :yellow
14
+ transitions :from => :yellow, :to => :red
15
+ transitions :from => :red, :to => :green
16
+ end
17
+ end
18
+ end
data/examples/test.rb CHANGED
@@ -35,7 +35,7 @@ class Test
35
35
  end
36
36
 
37
37
  def likes_ice_cream?
38
- rand(10) > 5 ? :mixed : :hate
38
+ rand(10) > 5 ? :mixeds : :hate
39
39
  end
40
40
 
41
41
  def exit_love
data/lib/stateflow.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  module Stateflow
2
2
  def self.included(base)
3
3
  base.send :include, InstanceMethods
4
+ Stateflow::Persistence.set(base)
4
5
  base.extend ClassMethods
5
6
  end
6
7
 
7
8
  def self.persistence
8
- @@persistence ||= "active_record"
9
+ @@persistence ||= :active_record
9
10
  end
10
11
 
11
12
  def self.persistence=(persistence)
@@ -34,11 +35,12 @@ module Stateflow
34
35
  end
35
36
 
36
37
  module InstanceMethods
37
- def current_state
38
- @current_state ||= machine.initial_state
38
+ def current_state
39
+ @current_state ||= load_from_persistence.nil? ? machine.initial_state : machine.states[load_from_persistence.to_sym]
39
40
  end
40
41
 
41
42
  def current_state=(new_state)
43
+ save_to_persistence(new_state.name.to_s)
42
44
  @current_state = new_state
43
45
  end
44
46
 
@@ -58,4 +60,5 @@ module Stateflow
58
60
  autoload :State, 'stateflow/state'
59
61
  autoload :Event, 'stateflow/event'
60
62
  autoload :Transition, 'stateflow/transition'
61
- end
63
+ autoload :Persistence, 'stateflow/persistence'
64
+ end
@@ -6,6 +6,10 @@ module Stateflow
6
6
  @states, @events = Hash.new, Hash.new
7
7
  instance_eval(&machine)
8
8
  end
9
+
10
+ def state_column(name = nil)
11
+ @state_column ||= name.nil? ? :state : name
12
+ end
9
13
 
10
14
  private
11
15
  def initial(name)
@@ -0,0 +1,15 @@
1
+ module Stateflow
2
+ module Persistence
3
+ def self.set(base)
4
+ case Stateflow.persistence
5
+ when :mongo_mapper
6
+ Stateflow::Persistence::MongoMapper.install(base)
7
+ when :active_record
8
+ Stateflow::Persistence::ActiveRecord.install(base)
9
+ end
10
+ end
11
+
12
+ autoload :MongoMapper, 'stateflow/persistence/mongo_mapper'
13
+ autoload :ActiveRecord, 'stateflow/persistence/active_record'
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module Stateflow
2
+ module Persistence
3
+ module ActiveRecord
4
+ def self.install(base)
5
+ base.respond_to?(:before_validation_on_create) ? base.before_validation_on_create(:ensure_initial_state) : base.before_validation(:ensure_initial_state, :on => :create)
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module InstanceMethods
10
+ def load_from_persistence
11
+ self.send machine.state_column.to_sym
12
+ end
13
+
14
+ def save_to_persistence(new_state)
15
+ self.update_attribute(machine.state_column.to_sym, new_state)
16
+ end
17
+
18
+ def ensure_initial_state
19
+ send("#{self.machine.state_column.to_s}=", self.current_state.name.to_s) if send(self.machine.state_column.to_s).blank?
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Stateflow
2
+ module Persistence
3
+ module MongoMapper
4
+ def self.install(base)
5
+ base.send :key, :state, String
6
+ base.before_validation_on_create :ensure_initial_state
7
+ base.send :include, InstanceMethods
8
+ end
9
+
10
+ module InstanceMethods
11
+ def load_from_persistence
12
+ self.send machine.state_column.to_sym
13
+ end
14
+
15
+ def save_to_persistence(new_state)
16
+ self.update_attributes(machine.state_column.to_sym => new_state)
17
+ end
18
+
19
+ def ensure_initial_state
20
+ send("#{self.machine.state_column.to_s}=", self.current_state.name.to_s) if send(self.machine.state_column.to_s).blank?
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
data/stateflow.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{stateflow}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ryan Oberholzer"]
9
- s.date = %q{2010-03-22}
9
+ s.date = %q{2010-03-24}
10
10
  s.description = %q{State machine that allows dynamic transitions for business workflows}
11
11
  s.email = %q{ryan@platform45.com}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/stateflow.rb", "lib/stateflow/event.rb", "lib/stateflow/machine.rb", "lib/stateflow/state.rb", "lib/stateflow/transition.rb"]
13
- s.files = ["LICENCE", "Manifest", "README.rdoc", "Rakefile", "examples/test.rb", "init.rb", "lib/stateflow.rb", "lib/stateflow/event.rb", "lib/stateflow/machine.rb", "lib/stateflow/state.rb", "lib/stateflow/transition.rb", "stateflow.gemspec"]
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/stateflow.rb", "lib/stateflow/event.rb", "lib/stateflow/machine.rb", "lib/stateflow/persistence.rb", "lib/stateflow/persistence/active_record.rb", "lib/stateflow/persistence/mongo_mapper.rb", "lib/stateflow/state.rb", "lib/stateflow/transition.rb"]
13
+ s.files = ["LICENCE", "Manifest", "README.rdoc", "Rakefile", "examples/robot.rb", "examples/test.rb", "init.rb", "lib/stateflow.rb", "lib/stateflow/event.rb", "lib/stateflow/machine.rb", "lib/stateflow/persistence.rb", "lib/stateflow/persistence/active_record.rb", "lib/stateflow/persistence/mongo_mapper.rb", "lib/stateflow/state.rb", "lib/stateflow/transition.rb", "stateflow.gemspec"]
14
14
  s.homepage = %q{http://github.com/ryanza/stateflow}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Stateflow", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stateflow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Oberholzer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-22 00:00:00 +02:00
12
+ date: 2010-03-24 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,9 @@ extra_rdoc_files:
24
24
  - lib/stateflow.rb
25
25
  - lib/stateflow/event.rb
26
26
  - lib/stateflow/machine.rb
27
+ - lib/stateflow/persistence.rb
28
+ - lib/stateflow/persistence/active_record.rb
29
+ - lib/stateflow/persistence/mongo_mapper.rb
27
30
  - lib/stateflow/state.rb
28
31
  - lib/stateflow/transition.rb
29
32
  files:
@@ -31,11 +34,15 @@ files:
31
34
  - Manifest
32
35
  - README.rdoc
33
36
  - Rakefile
37
+ - examples/robot.rb
34
38
  - examples/test.rb
35
39
  - init.rb
36
40
  - lib/stateflow.rb
37
41
  - lib/stateflow/event.rb
38
42
  - lib/stateflow/machine.rb
43
+ - lib/stateflow/persistence.rb
44
+ - lib/stateflow/persistence/active_record.rb
45
+ - lib/stateflow/persistence/mongo_mapper.rb
39
46
  - lib/stateflow/state.rb
40
47
  - lib/stateflow/transition.rb
41
48
  - stateflow.gemspec