state-fu 0.13.0 → 0.13.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -7
- data/lib/method_factory.rb +16 -9
- data/lib/persistence/active_record.rb +1 -1
- data/lib/transition_query.rb +4 -12
- data/spec/spec.opts +0 -1
- metadata +2 -2
data/Rakefile
CHANGED
@@ -7,12 +7,6 @@ require "rubygems"
|
|
7
7
|
|
8
8
|
load File.join( File.dirname(__FILE__),"/lib/tasks/state_fu.rake" )
|
9
9
|
|
10
|
-
module Rakefile
|
11
|
-
def self.windows?
|
12
|
-
/djgpp|(cyg|ms|bcc)win|mingw/ =~ RUBY_PLATFORM
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
10
|
load 'lib/tasks/spec_last.rake'
|
17
11
|
load 'lib/tasks/state_fu.rake'
|
18
12
|
|
@@ -67,7 +61,7 @@ namespace :spec do
|
|
67
61
|
desc "Run all specs with profiling & backtrace"
|
68
62
|
Spec::Rake::SpecTask.new(:prof) do |t|
|
69
63
|
t.spec_files = FileList["spec/**/*_spec.rb"]
|
70
|
-
t.spec_opts = ['-c','-b','-
|
64
|
+
t.spec_opts = ['-c','-b','-f','profile','-R','-L','mtime']
|
71
65
|
end
|
72
66
|
|
73
67
|
desc "Print Specdoc for all specs (eaxcluding plugin specs)"
|
data/lib/method_factory.rb
CHANGED
@@ -10,7 +10,7 @@ module StateFu
|
|
10
10
|
attr_reader :binding, :machine
|
11
11
|
|
12
12
|
# An instance of MethodFactory is created to define methods on a specific StateFu::Binding, and
|
13
|
-
# on the object it is bound to.
|
13
|
+
# on the object / class it is bound to.
|
14
14
|
|
15
15
|
def initialize(_binding)
|
16
16
|
@binding = _binding
|
@@ -20,7 +20,7 @@ module StateFu
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.method_definitions_for(machine, name)
|
23
|
-
returning
|
23
|
+
returning Hash.new do |method_definitions|
|
24
24
|
simple_events, complex_events = machine.events.partition &:simple?
|
25
25
|
|
26
26
|
# simple event methods
|
@@ -40,7 +40,7 @@ module StateFu
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
# complex event methods
|
43
|
+
# "complex" event methods (for events with more than one possible target)
|
44
44
|
# the first argument is the target state
|
45
45
|
# any remaining arguments are passed into the transition / transition query
|
46
46
|
|
@@ -58,7 +58,7 @@ module StateFu
|
|
58
58
|
#
|
59
59
|
# as per the method above, except that it also fires the event
|
60
60
|
|
61
|
-
# object.
|
61
|
+
# object.can_<event_name>? [:target], *arguments
|
62
62
|
#
|
63
63
|
# tests that calling event_name or event_name! would not raise an error
|
64
64
|
# ie, the transition is legal and is valid with the arguments supplied
|
@@ -82,9 +82,10 @@ module StateFu
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
# methods
|
86
|
-
#
|
87
|
-
|
85
|
+
# methods for a "complex" event with a specific target
|
86
|
+
# eg progress_to_deleted!(*args)
|
87
|
+
# is equivalent to progress!(:deleted, *args)
|
88
|
+
|
88
89
|
(simple_events + complex_events).each do |event|
|
89
90
|
event.targets.each do |target|
|
90
91
|
method_definitions["#{event.name}_to_#{target.name}"] = lambda do |*args|
|
@@ -100,6 +101,8 @@ module StateFu
|
|
100
101
|
end
|
101
102
|
end unless event.targets.nil?
|
102
103
|
end
|
104
|
+
|
105
|
+
# sugar: query methods for determining the current state
|
103
106
|
|
104
107
|
machine.states.each do |state|
|
105
108
|
method_definitions["#{state.name}?"] = lambda do
|
@@ -113,8 +116,10 @@ module StateFu
|
|
113
116
|
# Class Methods
|
114
117
|
#
|
115
118
|
|
116
|
-
# This should be called once per
|
117
|
-
#
|
119
|
+
# This should be called once per machine bound to a class.
|
120
|
+
# It defines methods for the machine as standard methods,
|
121
|
+
# if the machine is the default machine or the options passed to the machine include
|
122
|
+
# :define_methods => true .
|
118
123
|
#
|
119
124
|
# Note this happens when a machine is first bound to the class,
|
120
125
|
# not when StateFu is included.
|
@@ -140,6 +145,8 @@ module StateFu
|
|
140
145
|
define_event_methods_on @binding.object if @binding.options[:define_methods] && @binding.options[:singleton]
|
141
146
|
end
|
142
147
|
|
148
|
+
private
|
149
|
+
|
143
150
|
#
|
144
151
|
# For each event, on the given object, define three methods.
|
145
152
|
# - The first method is the same as the event name.
|
@@ -8,7 +8,7 @@ module StateFu
|
|
8
8
|
|
9
9
|
# this adds a before_save hook to ensure that the field is initialized
|
10
10
|
# (and the initial state set) before create.
|
11
|
-
klass.send :
|
11
|
+
klass.send :before_validation_on_create, :state_fu!
|
12
12
|
|
13
13
|
# it's usually a good idea to do this:
|
14
14
|
# validates_presence_of _field_name
|
data/lib/transition_query.rb
CHANGED
@@ -15,8 +15,10 @@ module StateFu
|
|
15
15
|
end
|
16
16
|
|
17
17
|
# calling result() will cause the set of transitions to be calculated -
|
18
|
-
# the cat will then be either dead or alive; until then it's
|
19
|
-
|
18
|
+
# the cat will then be either dead or alive; until then it's anyone's guess.
|
19
|
+
|
20
|
+
# this is a cheap way of passing any call to #each, #length, etc to result().
|
21
|
+
# TODO: be explicit, eg using delegate, and remove method_missing.
|
20
22
|
def method_missing(method_name, *args, &block)
|
21
23
|
if result.respond_to?(method_name, true)
|
22
24
|
result.__send__(method_name, *args, &block)
|
@@ -33,16 +35,6 @@ module StateFu
|
|
33
35
|
self
|
34
36
|
end
|
35
37
|
|
36
|
-
# build a list of possible transition destinations ([event, target])
|
37
|
-
# without actually constructing any transition objects
|
38
|
-
# def all_destinations
|
39
|
-
# binding.events.inject([]){ |arr, evt| arr += evt.targets.map{|tgt| [evt,tgt] }; arr}.uniq
|
40
|
-
# end
|
41
|
-
#
|
42
|
-
# def all_destination_names
|
43
|
-
# all_destinations.map {|tuple| tuple.map(&:to_sym) }
|
44
|
-
# end
|
45
|
-
|
46
38
|
#
|
47
39
|
# Chainable Filters
|
48
40
|
#
|
data/spec/spec.opts
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state-fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.13.
|
4
|
+
version: 0.13.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Lee
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-18 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|