hsume2-state_machine 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
 
7
7
  spec = Gem::Specification.new do |s|
8
8
  s.name = 'hsume2-state_machine'
9
- s.version = '1.0.3'
9
+ s.version = '1.0.4'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.summary = 'Adds support for creating state machines for attributes on any Ruby class'
12
12
  s.description = s.summary
@@ -16,9 +16,10 @@ spec = Gem::Specification.new do |s|
16
16
  s.has_rdoc = true
17
17
  s.test_files = Dir['test/**/*_test.rb']
18
18
 
19
- s.author = 'Henry Hsu'
20
- s.email = 'hhsu@zendesk.com'
21
- s.homepage = 'http://github.com/hsume2/state_machine'
19
+ s.author = 'Aaron Pfeifer'
20
+ s.email = 'aaron@pluginaweek.org'
21
+ s.homepage = 'http://www.pluginaweek.org'
22
+ s.rubyforge_project = 'pluginaweek'
22
23
  end
23
24
 
24
25
  desc 'Default: run all tests.'
@@ -452,12 +452,13 @@ module StateMachine
452
452
  @use_transactions = options[:use_transactions]
453
453
  @initialize_state = options[:initialize]
454
454
  @syntax = options[:syntax]
455
+ @plural = options[:plural]
455
456
  self.owner_class = owner_class
456
457
  self.initial_state = options[:initial] unless owner_class.state_machines.any? {|name, machine| machine.attribute == attribute && machine != self}
457
458
 
458
459
  # Define class integration
459
460
  define_helpers
460
- define_scopes(options[:plural])
461
+ define_scopes(@plural)
461
462
  after_initialize
462
463
 
463
464
  # Evaluate DSL
@@ -477,6 +478,16 @@ module StateMachine
477
478
  @callbacks = {:before => @callbacks[:before].dup, :after => @callbacks[:after].dup, :failure => @callbacks[:failure].dup}
478
479
  end
479
480
 
481
+ def duplicate_to(clazz)
482
+ new_copy = self.clone
483
+ new_copy.owner_class = clazz
484
+ new_copy.define_helpers
485
+ new_copy.define_scopes(@plural)
486
+ new_copy.events.each { |event| event.send(:add_actions) }
487
+ new_copy.states.each { |state| state.send(:add_predicate) }
488
+ new_copy
489
+ end
490
+
480
491
  def evaluate_with_syntax(syntax, &block)
481
492
  if syntax == :alternate
482
493
  instance_eval(&alternate_syntax_eval(&block))
@@ -1,5 +1,9 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
+ class Sample
4
+
5
+ end
6
+
3
7
  class MachineByDefaultTest < Test::Unit::TestCase
4
8
  def setup
5
9
  @klass = Class.new
@@ -1005,6 +1009,80 @@ class MachineAfterBeingCopiedTest < Test::Unit::TestCase
1005
1009
  end
1006
1010
  end
1007
1011
 
1012
+ class MachineAfterBeingDuplicatedTest < Test::Unit::TestCase
1013
+ def setup
1014
+ @machine = StateMachine::Machine.new(Class.new, :state, :initial => :parked)
1015
+ @machine.event(:ignite) {}
1016
+ @machine.before_transition(lambda {})
1017
+ @machine.after_transition(lambda {})
1018
+ @machine.around_transition(lambda {})
1019
+ @machine.after_failure(lambda {})
1020
+
1021
+ @new_class = Class.new
1022
+
1023
+ @copied_machine = @machine.duplicate_to(@new_class)
1024
+ end
1025
+
1026
+ def test_should_copy_each_event_and_add_actions
1027
+ @new_instance = @new_class.new
1028
+ assert @new_instance.respond_to?(:ignite)
1029
+ assert @new_instance.respond_to?(:ignite!)
1030
+ end
1031
+
1032
+ def test_should_copy_each_event_and_add_predicate
1033
+ @new_instance = @new_class.new
1034
+ assert @new_instance.respond_to?(:parked?)
1035
+ end
1036
+
1037
+ def test_should_not_have_the_same_collection_of_states
1038
+ assert_not_same @copied_machine.states, @machine.states
1039
+ end
1040
+
1041
+ def test_should_copy_each_state
1042
+ assert_not_same @copied_machine.states[:parked], @machine.states[:parked]
1043
+ end
1044
+
1045
+ def test_should_update_machine_for_each_state
1046
+ assert_equal @copied_machine, @copied_machine.states[:parked].machine
1047
+ end
1048
+
1049
+ def test_should_not_update_machine_for_original_state
1050
+ assert_equal @machine, @machine.states[:parked].machine
1051
+ end
1052
+
1053
+ def test_should_not_have_the_same_collection_of_events
1054
+ assert_not_same @copied_machine.events, @machine.events
1055
+ end
1056
+
1057
+ def test_should_copy_each_event
1058
+ assert_not_same @copied_machine.events[:ignite], @machine.events[:ignite]
1059
+ end
1060
+
1061
+ def test_should_update_machine_for_each_event
1062
+ assert_equal @copied_machine, @copied_machine.events[:ignite].machine
1063
+ end
1064
+
1065
+ def test_should_not_update_machine_for_original_event
1066
+ assert_equal @machine, @machine.events[:ignite].machine
1067
+ end
1068
+
1069
+ def test_should_not_have_the_same_callbacks
1070
+ assert_not_same @copied_machine.callbacks, @machine.callbacks
1071
+ end
1072
+
1073
+ def test_should_not_have_the_same_before_callbacks
1074
+ assert_not_same @copied_machine.callbacks[:before], @machine.callbacks[:before]
1075
+ end
1076
+
1077
+ def test_should_not_have_the_same_after_callbacks
1078
+ assert_not_same @copied_machine.callbacks[:after], @machine.callbacks[:after]
1079
+ end
1080
+
1081
+ def test_should_not_have_the_same_failure_callbacks
1082
+ assert_not_same @copied_machine.callbacks[:failure], @machine.callbacks[:failure]
1083
+ end
1084
+ end
1085
+
1008
1086
  class MachineAfterChangingOwnerClassTest < Test::Unit::TestCase
1009
1087
  def setup
1010
1088
  @original_class = Class.new
metadata CHANGED
@@ -1,26 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hsume2-state_machine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 3
10
- version: 1.0.3
9
+ - 4
10
+ version: 1.0.4
11
11
  platform: ruby
12
12
  authors:
13
- - Henry Hsu
13
+ - Aaron Pfeifer
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-27 00:00:00 -07:00
18
+ date: 2011-08-01 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
22
  description: Adds support for creating state machines for attributes on any Ruby class
23
- email: hhsu@zendesk.com
23
+ email: aaron@pluginaweek.org
24
24
  executables: []
25
25
 
26
26
  extensions: []
@@ -138,7 +138,7 @@ files:
138
138
  - Rakefile
139
139
  - README.rdoc
140
140
  has_rdoc: true
141
- homepage: http://github.com/hsume2/state_machine
141
+ homepage: http://www.pluginaweek.org
142
142
  licenses: []
143
143
 
144
144
  post_install_message:
@@ -166,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  version: "0"
167
167
  requirements: []
168
168
 
169
- rubyforge_project:
169
+ rubyforge_project: pluginaweek
170
170
  rubygems_version: 1.5.3
171
171
  signing_key:
172
172
  specification_version: 3