public
Rubygem
Description: Make your classes stately.
Clone URL: git://github.com/jbarnette/stateful.git
All acess to a model's current state goes through the persister.
jbarnette (author)
Wed Jul 09 12:45:49 -0700 2008
commit  25e9ecc304a3ba747837ebfde4af1a63185ac504
tree    53509d9c5e4084942293e56471bfd0ad5fc296ea
parent  c35136d8df020dcb21423b926bb79fd924e9570d
...
7
8
9
10
 
 
11
12
13
...
7
8
9
 
10
11
12
13
14
0
@@ -7,7 +7,8 @@ module Stateful
0
 
0
   class BadTransition < StandardError
0
     def initialize(model, event)
0
- super("Can't #{event.name} while #{model.current_state}: #{model.inspect}")
0
+ current = model.class.statefully.persister.state_of(model)
0
+ super("Can't #{event.name} while #{current}: #{model.inspect}")
0
     end
0
   end
0
   
...
27
28
29
30
 
31
32
33
...
49
50
51
52
 
53
54
55
...
27
28
29
 
30
31
32
33
...
49
50
51
 
52
53
54
55
0
@@ -27,7 +27,7 @@ module Stateful
0
         unless target.method_defined?("#{name}?")
0
           target.class_eval <<-RUBY
0
             def #{name}?
0
- current_state == #{name.inspect}
0
+ self.class.statefully.persister.state_of(self) == #{name.inspect}
0
             end
0
           RUBY
0
         end
0
@@ -49,7 +49,7 @@ module Stateful
0
     def execute(model, name)
0
       raise Stateful::BadModel.new(model) unless model.class.stateful?
0
       
0
- now = model.current_state
0
+ now = persister.state_of(model)
0
       event = events[name] or raise EventNotFound.new(name)
0
       from = states[now] or raise StateNotFound.new(now)
0
       dest = event.transitions[now] or raise BadTransition.new(model, event)
...
4
5
6
 
 
 
 
7
8
9
...
4
5
6
7
8
9
10
11
12
13
0
@@ -4,6 +4,10 @@ module Stateful
0
       def persist(model, state)
0
         model.current_state = state
0
       end
0
+
0
+ def state_of(model)
0
+ model.current_state
0
+ end
0
 
0
       def accessorize(target)
0
         unless target.respond_to?(:current_state)
...
1
2
3
4
5
6
7
8
 
 
 
 
 
 
9
10
11
...
1
2
3
 
 
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1,11 +1,15 @@
0
 module Stateful
0
   module Persisters
0
     class Default
0
- def accessorize(target) end
0
-
0
       def persist(model, state)
0
         raise "I don't know how to persist state changes. Sorry!"
0
       end
0
+
0
+ def state_of(model)
0
+ raise "I don't know how to persist state changes. Sorry!"
0
+ end
0
+
0
+ def accessorize(target) end
0
     end
0
   end
0
 end

Comments

    No one has commented yet.