davidlee-state-fu 0.11.1 → 0.12.0

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.
@@ -873,5 +873,93 @@ describe "sitting at a poker machine" do
873
873
  end
874
874
  end
875
875
 
876
+ describe "Chameleon" do
877
+
878
+ before do
879
+ make_pristine_class('Chameleon') do
880
+
881
+ machine :location do
882
+ initial_state :outside
883
+
884
+ event :go_outside, :from => {:inside => :outside}
885
+ event :go_inside, :from => {:outside => :inside}
886
+ end
887
+
888
+ # With :define_methods => true, we can create methods for the :skin
889
+ # machine directly on our Chameleon, so we can type e.g.
890
+ #
891
+ # @chameleon.comoflage! :bark
892
+ # instead of:
893
+ # @chameleon.skin.camoflage! :bark
894
+ #
895
+ # This is the usual behaviour for the default machine, but not for any
896
+ # machine given an explicit name. Otherwise, it would cause confusion
897
+ # when (like the PokerMachine example) multiple machines would compete
898
+ # for the same methods.
899
+ #
900
+ # Hint for the masochistic: state / event methods will never overwrite a
901
+ # pre-existing method, so in the event of overlap, the first machine
902
+ # defined will take precedence.
903
+
904
+ machine :skin, :define_methods => true do
905
+ initial_state :green
906
+
907
+ states :plaid, :paisley, :tartan, :location => :indoors
908
+ states :bark, :pebbles, :foliage, :location => :outdoors
909
+
910
+ define :change_according_to_surroundings? do |transition|
911
+ if transition.cycle?
912
+ false
913
+ else
914
+ case transition.target[:location]
915
+ when :indoors
916
+ location.inside?
917
+ when :outdoors
918
+ location.outside?
919
+ else
920
+ true
921
+ end
922
+ end
923
+ end
924
+
925
+ event :camoflage, :from => states.all, :to => states.all do
926
+ requires :change_according_to_surroundings?, :message => lambda { |t| "It's no good looking like #{t.target.name} when you're #{t.object.location.current_state_name}!"}
927
+ end
928
+
929
+ end
930
+ end # Chameleon
931
+ end # before
932
+
933
+ describe "changing its skin" do
934
+ before do
935
+ @chameleon = Chameleon.new
936
+ end
937
+
938
+ it "should change its skin according to its surroundings" do
939
+ @chameleon.current_state(:location).should == :outside
940
+ @chameleon.current_state(:skin).should == :green
941
+
942
+ @chameleon.location.outside?.should == true
943
+
944
+ @chameleon.skin.valid_transitions.targets.names.should == [:bark, :pebbles, :foliage]
945
+ @chameleon.camoflage!(:bark)
946
+ @chameleon.skin.should == :bark
947
+
948
+ @chameleon.location.go_inside!
949
+ @chameleon.skin.valid_transitions.targets.names.should == [:green, :plaid, :paisley, :tartan]
950
+
951
+ @chameleon.camoflage!(:tartan)
952
+ @chameleon.skin.should == :tartan
953
+
954
+ @chameleon.camoflage!(:green)
955
+ @chameleon.skin.should == :green
956
+
957
+ lambda { @chameleon.camoflage!(:bark) }.should raise_error(StateFu::RequirementError)
958
+ @chameleon.camoflage(:bark).error_messages.should == ["It's no good looking like bark when you're inside!"]
959
+ end
960
+
961
+ end
962
+
963
+ end
876
964
 
877
965
 
@@ -14,12 +14,6 @@ describe StateFu::Binding do
14
14
  end
15
15
 
16
16
 
17
- #
18
- #
19
- #
20
-
21
-
22
-
23
17
  #
24
18
  #
25
19
  #
@@ -33,8 +27,10 @@ describe StateFu::Binding do
33
27
 
34
28
  describe "constructor" do
35
29
  before do
36
- mock(Klass).state_fu_field_names.at_most(1) do
37
- { :example => :example_field }
30
+ mock(Klass).state_fu_options.at_most(2) do
31
+ {
32
+ :example => {:field_name => :example_field}
33
+ }
38
34
  end
39
35
  end
40
36
 
@@ -50,7 +46,7 @@ describe StateFu::Binding do
50
46
  b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example,
51
47
  :colour => :red,
52
48
  :style => [:robust, :fruity] )
53
- b.options.should == { :colour => :red, :style => [:robust, :fruity] }
49
+ b.options.should == { :colour => :red, :style => [:robust, :fruity], :field_name => :example_field }
54
50
  end
55
51
 
56
52
  describe "persister initialization" do
@@ -63,10 +59,12 @@ describe StateFu::Binding do
63
59
  end
64
60
 
65
61
  describe "when StateFu::Persistence.active_record_column? is true" do
62
+
66
63
  before do
67
64
  mock( StateFu::Persistence ).active_record_column?(Klass, :example_field).times(1) { true }
68
65
  mock( Klass ).before_create( :state_fu!) { }
69
66
  end
67
+
70
68
  it "should get an ActiveRecord persister" do
71
69
  mock( StateFu::Persistence::ActiveRecord ).new( anything, :example_field ) { @p }
72
70
  b = StateFu::Binding.new( Klass.state_fu_machine, @obj, :example )
@@ -20,22 +20,19 @@ describe StateFu::Machine do
20
20
  before do
21
21
  reset!
22
22
  make_pristine_class 'Klass'
23
- # mock( Klass ).machines() { {} }
24
23
  end
25
24
 
26
25
  it "should create a new machine and bind! it" do
27
26
  @machine = Object.new
28
- mock( @machine ).bind!( Klass, :moose, nil )
29
- mock( StateFu::Machine ).new( {} ) { @machine }
30
- StateFu::Machine.for_class( Klass, :moose )
27
+ mock(@machine).bind!(Klass, :moose, {})
28
+ mock(StateFu::Machine).new({}) { @machine }
29
+ StateFu::Machine.for_class Klass, :moose
31
30
  end
32
31
 
33
32
  it "should apply the block (via lathe) if one is given" do
34
- @m = StateFu::Machine.for_class( Klass, :snoo ) do
33
+ @m = StateFu::Machine.for_class Klass, :snoo do
35
34
  state :porpoise
36
35
  end
37
- # mock( Klass ).machines() { {} }
38
- # @m.states.map(&:name).should == [:porpoise]
39
36
  end
40
37
  end
41
38
 
@@ -85,7 +82,7 @@ describe StateFu::Machine do
85
82
  name = :StinkJuice
86
83
  field_name = 'stink_juice_field'
87
84
  @m.bind!( Klass, name )
88
- Klass.state_fu_field_names[name].should == 'stink_juice_field'
85
+ Klass.state_fu_options[name][:field_name].should == 'stink_juice_field'
89
86
  end
90
87
  end
91
88
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: davidlee-state-fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.12.0
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-08-26 00:00:00 -07:00
12
+ date: 2009-09-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -73,6 +73,7 @@ files:
73
73
  - lib/transition_query.rb
74
74
  - spec/custom_formatter.rb
75
75
  - spec/features/binding_and_transition_helper_mixin_spec.rb
76
+ - spec/features/machine_alias_spec.rb
76
77
  - spec/features/method_missing_only_once_spec.rb
77
78
  - spec/features/not_requirements_spec.rb
78
79
  - spec/features/plotter_spec.rb
@@ -80,6 +81,7 @@ files:
80
81
  - spec/features/singleton_machine_spec.rb
81
82
  - spec/features/state_and_array_options_accessor_spec.rb
82
83
  - spec/features/transition_boolean_comparison_spec.rb
84
+ - spec/features/when_methods_are_defined_spec.rb
83
85
  - spec/helper.rb
84
86
  - spec/integration/active_record_persistence_spec.rb
85
87
  - spec/integration/binding_extension_spec.rb
@@ -137,6 +139,7 @@ summary: A rich library for state-oriented programming with state machines / wor
137
139
  test_files:
138
140
  - spec/custom_formatter.rb
139
141
  - spec/features/binding_and_transition_helper_mixin_spec.rb
142
+ - spec/features/machine_alias_spec.rb
140
143
  - spec/features/method_missing_only_once_spec.rb
141
144
  - spec/features/not_requirements_spec.rb
142
145
  - spec/features/plotter_spec.rb
@@ -144,6 +147,7 @@ test_files:
144
147
  - spec/features/singleton_machine_spec.rb
145
148
  - spec/features/state_and_array_options_accessor_spec.rb
146
149
  - spec/features/transition_boolean_comparison_spec.rb
150
+ - spec/features/when_methods_are_defined_spec.rb
147
151
  - spec/helper.rb
148
152
  - spec/integration/active_record_persistence_spec.rb
149
153
  - spec/integration/binding_extension_spec.rb