state_machine 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,6 +1,12 @@
1
1
  == master
2
2
 
3
- == 0.5.0 / 2008-01-11
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.0'
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
- namespace :state_machine do
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}
Binary file
Binary file
Binary file
Binary file
@@ -193,32 +193,21 @@ module StateMachine
193
193
  :save
194
194
  end
195
195
 
196
- # Forces all attribute methods to be generated for the model so that
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
- # If an exception is raised while trying to access the connection, then
200
- # the assumption is that there's an issue with the database (most likely
201
- # doesn't exist yet), so we won't be able to check the table properties
202
- connection_exists = begin; owner_class.connection; true; rescue Exception; false; end
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
- if connection_exists && owner_class.table_exists?
205
- owner_class.define_attribute_methods
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
- define_attribute_accessor
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 reader/writer/predicate methods for accessing the attribute that
974
- # this state machine is defined for.
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
 
@@ -184,8 +184,7 @@ module StateMachine
184
184
  :label => description,
185
185
  :width => '1',
186
186
  :height => '1',
187
- :fixedsize => 'true',
188
- :shape => initial ? 'doublecircle' : 'circle'
187
+ :shape => initial ? 'doublecircle' : 'ellipse'
189
188
  )
190
189
  end
191
190
 
data/lib/state_machine.rb CHANGED
@@ -314,3 +314,6 @@ end
314
314
  Class.class_eval do
315
315
  include StateMachine::MacroMethods
316
316
  end
317
+
318
+ # Register rake tasks for supported libraries
319
+ Merb::Plugins.add_rakefiles("#{File.dirname(__FILE__)}/../tasks/state_machine") if defined?(Merb::Plugins)