state_machine 0.5.0 → 0.5.1
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/CHANGELOG.rdoc +7 -1
- data/Rakefile +2 -26
- data/examples/AutoShop_state.png +0 -0
- data/examples/Car_state.png +0 -0
- data/examples/TrafficLight_state.png +0 -0
- data/examples/Vehicle_state.png +0 -0
- data/lib/state_machine/integrations/active_record.rb +11 -22
- data/lib/state_machine/integrations/data_mapper.rb +4 -0
- data/lib/state_machine/integrations/sequel.rb +4 -0
- data/lib/state_machine/machine.rb +27 -9
- data/lib/state_machine/state.rb +1 -2
- data/lib/state_machine.rb +3 -0
- data/test/active_record.log +92 -12333
- data/test/sequel.log +23 -1591
- data/test/unit/integrations/active_record_test.rb +24 -7
- data/test/unit/integrations/data_mapper_test.rb +27 -7
- data/test/unit/integrations/sequel_test.rb +26 -6
- data/test/unit/state_test.rb +2 -6
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
== master
|
2
2
|
|
3
|
-
== 0.5.
|
3
|
+
== 0.5.1 / 2009-02-11
|
4
|
+
|
5
|
+
* Allow states to be drawn as ellipses to accommodate long names
|
6
|
+
* Fix rake tasks not being registered in Rails/Merb applications
|
7
|
+
* Never automatically define machine attribute accessors when using an integration
|
8
|
+
|
9
|
+
== 0.5.0 / 2009-01-11
|
4
10
|
|
5
11
|
* Add to_name and from_name to transition objects
|
6
12
|
* Add nicely formatted #inspect for transitions
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'state_machine'
|
8
|
-
s.version = '0.5.
|
8
|
+
s.version = '0.5.1'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Adds support for creating state machines for attributes on any Ruby class'
|
11
11
|
|
@@ -87,28 +87,4 @@ task :release => [:gem, :package] do
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
|
91
|
-
desc 'Draws a set of state machines using GraphViz. Target files to load with FILE=x,y,z; Machine class with CLASS=x,y,z; Font name with FONT=x; Image format with FORMAT=x'
|
92
|
-
task :draw do
|
93
|
-
# Load the library
|
94
|
-
$:.unshift(File.dirname(__FILE__) + '/lib')
|
95
|
-
require 'state_machine'
|
96
|
-
|
97
|
-
# Build drawing options
|
98
|
-
options = {}
|
99
|
-
options[:file] = ENV['FILE'] if ENV['FILE']
|
100
|
-
options[:path] = ENV['TARGET'] if ENV['TARGET']
|
101
|
-
options[:format] = ENV['FORMAT'] if ENV['FORMAT']
|
102
|
-
options[:font] = ENV['FONT'] if ENV['FONT']
|
103
|
-
|
104
|
-
StateMachine::Machine.draw(ENV['CLASS'], options)
|
105
|
-
end
|
106
|
-
|
107
|
-
namespace :draw do
|
108
|
-
desc 'Draws a set of state machines using GraphViz for a Ruby on Rails application. Target class with CLASS=x,y,z; Font name with FONT=x; Image format with FORMAT=x'
|
109
|
-
task :rails => [:environment, 'state_machine:draw']
|
110
|
-
|
111
|
-
desc 'Draws a set of state machines using GraphViz for a Merb application. Target class with CLASS=x,y,z; Font name with FONT=x; Image format with FORMAT=x'
|
112
|
-
task :merb => [:merb_env, 'state_machine:draw']
|
113
|
-
end
|
114
|
-
end
|
90
|
+
Dir['tasks/**/*.rake'].each {|rake| load rake}
|
data/examples/AutoShop_state.png
CHANGED
Binary file
|
data/examples/Car_state.png
CHANGED
Binary file
|
Binary file
|
data/examples/Vehicle_state.png
CHANGED
Binary file
|
@@ -193,32 +193,21 @@ module StateMachine
|
|
193
193
|
:save
|
194
194
|
end
|
195
195
|
|
196
|
-
#
|
197
|
-
# the reader/writer methods for the attribute are available
|
196
|
+
# Skips defining reader/writer methods since this is done automatically
|
198
197
|
def define_attribute_accessor
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
198
|
+
end
|
199
|
+
|
200
|
+
# Adds support for defining the attribute predicate, while providing
|
201
|
+
# compatibility with the default predicate which determines whether
|
202
|
+
# *anything* is set for the attribute's value
|
203
|
+
def define_attribute_predicate
|
204
|
+
attribute = self.attribute
|
203
205
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
# Support attribute predicate for ActiveRecord columns
|
208
|
-
if owner_class.column_names.include?(attribute.to_s)
|
209
|
-
attribute = self.attribute
|
210
|
-
|
211
|
-
owner_class.class_eval do
|
212
|
-
# Checks whether the current state is a given value. If there
|
213
|
-
# are no arguments, then this checks for the presence of the attribute.
|
214
|
-
define_method("#{attribute}?") do |*args|
|
215
|
-
args.empty? ? super(*args) : self.class.state_machines[attribute].state?(self, *args)
|
216
|
-
end
|
217
|
-
end
|
206
|
+
owner_class.class_eval do
|
207
|
+
define_method("#{attribute}?") do |*args|
|
208
|
+
args.empty? ? super(*args) : self.class.state_machines[attribute].state?(self, *args)
|
218
209
|
end
|
219
210
|
end
|
220
|
-
|
221
|
-
super
|
222
211
|
end
|
223
212
|
|
224
213
|
# Creates a scope for finding records *with* a particular state or
|
@@ -171,6 +171,10 @@ module StateMachine
|
|
171
171
|
:save
|
172
172
|
end
|
173
173
|
|
174
|
+
# Skips defining reader/writer methods since this is done automatically
|
175
|
+
def define_attribute_accessor
|
176
|
+
end
|
177
|
+
|
174
178
|
# Creates a scope for finding records *with* a particular state or
|
175
179
|
# states for the attribute
|
176
180
|
def create_with_scope(name)
|
@@ -141,6 +141,10 @@ module StateMachine
|
|
141
141
|
:save
|
142
142
|
end
|
143
143
|
|
144
|
+
# Skips defining reader/writer methods since this is done automatically
|
145
|
+
def define_attribute_accessor
|
146
|
+
end
|
147
|
+
|
144
148
|
# Creates a scope for finding records *with* a particular state or
|
145
149
|
# states for the attribute
|
146
150
|
def create_with_scope(name)
|
@@ -261,7 +261,7 @@ module StateMachine
|
|
261
261
|
|
262
262
|
# Set integration-specific configurations
|
263
263
|
@action = options.include?(:action) ? options[:action] : default_action
|
264
|
-
|
264
|
+
define_attribute_helpers
|
265
265
|
define_scopes(options[:plural])
|
266
266
|
|
267
267
|
# Call after hook for integration-specific extensions
|
@@ -970,24 +970,42 @@ module StateMachine
|
|
970
970
|
def default_action
|
971
971
|
end
|
972
972
|
|
973
|
-
# Adds
|
974
|
-
#
|
973
|
+
# Adds helper methods for interacting with this state machine's attribute,
|
974
|
+
# including reader, writer, and predicate methods
|
975
|
+
def define_attribute_helpers
|
976
|
+
define_attribute_accessor
|
977
|
+
define_attribute_predicate
|
978
|
+
|
979
|
+
attribute = self.attribute
|
980
|
+
|
981
|
+
owner_class.class_eval do
|
982
|
+
# Gets the state name for the current value
|
983
|
+
define_method("#{attribute}_name") do
|
984
|
+
self.class.state_machines[attribute].state_for(self).name
|
985
|
+
end
|
986
|
+
end
|
987
|
+
end
|
988
|
+
|
989
|
+
# Adds reader/writer methods for accessing the attribute
|
975
990
|
def define_attribute_accessor
|
976
991
|
attribute = self.attribute
|
977
992
|
|
978
993
|
owner_class.class_eval do
|
979
994
|
attr_reader attribute unless method_defined?(attribute) || private_method_defined?(attribute)
|
980
995
|
attr_writer attribute unless method_defined?("#{attribute}=") || private_method_defined?("#{attribute}=")
|
981
|
-
|
996
|
+
end
|
997
|
+
end
|
998
|
+
|
999
|
+
# Adds predicate method to the owner class for determining the name of the
|
1000
|
+
# current state
|
1001
|
+
def define_attribute_predicate
|
1002
|
+
attribute = self.attribute
|
1003
|
+
|
1004
|
+
owner_class.class_eval do
|
982
1005
|
# Checks whether the current state is a given value
|
983
1006
|
define_method("#{attribute}?") do |state|
|
984
1007
|
self.class.state_machines[attribute].state?(self, state)
|
985
1008
|
end unless method_defined?("#{attribute}?") || private_method_defined?("#{attribute}?")
|
986
|
-
|
987
|
-
# Gets the state name for the current value
|
988
|
-
define_method("#{attribute}_name") do
|
989
|
-
self.class.state_machines[attribute].state_for(self).name
|
990
|
-
end
|
991
1009
|
end
|
992
1010
|
end
|
993
1011
|
|
data/lib/state_machine/state.rb
CHANGED
data/lib/state_machine.rb
CHANGED