hsume2-state_machine 1.0.1 → 1.0.2

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.1'
9
+ s.version = '1.0.2'
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
@@ -160,7 +160,10 @@ module StateMachine
160
160
  # Generate an edge between each from and to state
161
161
  from_states.each do |from_state|
162
162
  from_state = from_state ? from_state.to_s : 'nil'
163
- edges << graph.add_edge(from_state, loopback ? from_state : to_state, :label => event.to_s)
163
+ label = event.to_s
164
+ label += " (#{if_condition})" if if_condition
165
+ label += " (not #{unless_condition})" if unless_condition
166
+ edges << graph.add_edge(from_state, loopback ? from_state : to_state, :label => label)
164
167
  end
165
168
 
166
169
  edges
@@ -328,13 +328,7 @@ module StateMachine
328
328
  end
329
329
 
330
330
  # Evaluate DSL
331
- if block_given?
332
- if options[:syntax] == :alternate
333
- machine.instance_eval(&machine.alternate_syntax_eval(&block))
334
- else
335
- machine.instance_eval(&block)
336
- end
337
- end
331
+ machine.evaluate_with_syntax(&block) if block_given?
338
332
  else
339
333
  # No existing machine: create a new one
340
334
  machine = new(owner_class, name, options, &block)
@@ -467,13 +461,7 @@ module StateMachine
467
461
  after_initialize
468
462
 
469
463
  # Evaluate DSL
470
- if block_given?
471
- if @syntax == :alternate
472
- instance_eval(&alternate_syntax_eval(&block))
473
- else
474
- instance_eval(&block)
475
- end
476
- end
464
+ evaluate_with_syntax(&block) if block_given?
477
465
  end
478
466
 
479
467
  # Creates a copy of this machine in addition to copies of each associated
@@ -489,6 +477,14 @@ module StateMachine
489
477
  @callbacks = {:before => @callbacks[:before].dup, :after => @callbacks[:after].dup, :failure => @callbacks[:failure].dup}
490
478
  end
491
479
 
480
+ def evaluate_with_syntax(&block)
481
+ if @syntax == :alternate
482
+ instance_eval(&alternate_syntax_eval(&block))
483
+ else
484
+ instance_eval(&block)
485
+ end
486
+ end
487
+
492
488
  def alternate_syntax_eval(&block)
493
489
  alternate = AlternateMachine.new(&block)
494
490
  alternate.to_state_machine
@@ -885,6 +885,38 @@ begin
885
885
  assert_equal 'nil', @edges.first.instance_variable_get('@xNodeTwo')
886
886
  end
887
887
  end
888
+
889
+ class BranchDrawingWithIfConditionTest < Test::Unit::TestCase
890
+ def setup
891
+ @machine = StateMachine::Machine.new(Class.new)
892
+
893
+ graph = GraphViz.new('G')
894
+ graph.add_node('parked')
895
+
896
+ @branch = StateMachine::Branch.new(:from => :idling, :to => nil, :if => :have_keys?)
897
+ @edges = @branch.draw(graph, :park, [nil, :idling])
898
+ end
899
+
900
+ def test_should_use_event_name_and_if_condition_as_label
901
+ assert_equal 'park (have_keys?)', @edges.first['label'].to_s.gsub('"', '')
902
+ end
903
+ end
904
+
905
+ class BranchDrawingWithUnlessConditionTest < Test::Unit::TestCase
906
+ def setup
907
+ @machine = StateMachine::Machine.new(Class.new)
908
+
909
+ graph = GraphViz.new('G')
910
+ graph.add_node('parked')
911
+
912
+ @branch = StateMachine::Branch.new(:from => :idling, :to => nil, :unless => :missing_keys?)
913
+ @edges = @branch.draw(graph, :park, [nil, :idling])
914
+ end
915
+
916
+ def test_should_use_event_name_and_unless_condition_as_label
917
+ assert_equal 'park (not missing_keys?)', @edges.first['label'].to_s.gsub('"', '')
918
+ end
919
+ end
888
920
  rescue LoadError
889
921
  $stderr.puts 'Skipping GraphViz StateMachine::Branch tests. `gem install ruby-graphviz` >= v0.9.0 and try again.'
890
922
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hsume2-state_machine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aaron Pfeifer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-26 00:00:00 -07:00
18
+ date: 2011-07-27 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21