state_machine 1.1.0 → 1.1.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/.yardopts +0 -1
- data/CHANGELOG.md +7 -0
- data/LICENSE +1 -1
- data/README.md +97 -5
- data/gemfiles/active_model-3.0.0.gemfile.lock +1 -1
- data/gemfiles/active_model-3.0.5.gemfile.lock +1 -1
- data/gemfiles/active_model-3.1.1.gemfile.lock +1 -1
- data/gemfiles/active_record-2.0.0.gemfile.lock +1 -1
- data/gemfiles/active_record-2.0.5.gemfile.lock +1 -1
- data/gemfiles/active_record-2.1.0.gemfile.lock +1 -1
- data/gemfiles/active_record-2.1.2.gemfile.lock +1 -1
- data/gemfiles/active_record-2.2.3.gemfile.lock +1 -1
- data/gemfiles/active_record-2.3.12.gemfile.lock +1 -1
- data/gemfiles/active_record-3.0.0.gemfile.lock +1 -1
- data/gemfiles/active_record-3.0.5.gemfile.lock +1 -1
- data/gemfiles/active_record-3.1.1.gemfile.lock +1 -1
- data/gemfiles/data_mapper-0.10.2.gemfile.lock +1 -1
- data/gemfiles/data_mapper-0.9.11.gemfile.lock +1 -1
- data/gemfiles/data_mapper-0.9.4.gemfile.lock +1 -1
- data/gemfiles/data_mapper-0.9.7.gemfile.lock +1 -1
- data/gemfiles/data_mapper-1.0.0.gemfile.lock +1 -1
- data/gemfiles/data_mapper-1.0.1.gemfile.lock +1 -1
- data/gemfiles/data_mapper-1.0.2.gemfile.lock +1 -1
- data/gemfiles/data_mapper-1.1.0.gemfile.lock +1 -1
- data/gemfiles/data_mapper-1.2.0.gemfile.lock +1 -1
- data/gemfiles/default.gemfile.lock +1 -1
- data/gemfiles/graphviz-0.9.0.gemfile.lock +1 -1
- data/gemfiles/graphviz-0.9.21.gemfile.lock +1 -1
- data/gemfiles/graphviz-1.0.0.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.10.0.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.5.5.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.5.8.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.6.0.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.6.10.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.7.0.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.7.5.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.8.0.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.8.3.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.8.4.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.8.6.gemfile.lock +1 -1
- data/gemfiles/mongo_mapper-0.9.0.gemfile.lock +1 -1
- data/gemfiles/mongoid-2.0.0.gemfile.lock +1 -1
- data/gemfiles/mongoid-2.1.4.gemfile.lock +1 -1
- data/gemfiles/mongoid-2.2.4.gemfile.lock +1 -1
- data/gemfiles/mongoid-2.3.3.gemfile.lock +1 -1
- data/gemfiles/sequel-2.11.0.gemfile.lock +1 -1
- data/gemfiles/sequel-2.12.0.gemfile.lock +1 -1
- data/gemfiles/sequel-2.8.0.gemfile.lock +1 -1
- data/gemfiles/sequel-3.0.0.gemfile.lock +1 -1
- data/gemfiles/sequel-3.13.0.gemfile.lock +1 -1
- data/gemfiles/sequel-3.14.0.gemfile.lock +1 -1
- data/gemfiles/sequel-3.23.0.gemfile.lock +1 -1
- data/gemfiles/sequel-3.24.0.gemfile.lock +1 -1
- data/gemfiles/sequel-3.29.0.gemfile.lock +1 -1
- data/lib/state_machine.rb +2 -499
- data/lib/state_machine/core.rb +2 -0
- data/lib/state_machine/core_ext.rb +1 -0
- data/lib/state_machine/core_ext/class/state_machine.rb +5 -0
- data/lib/state_machine/event.rb +0 -5
- data/lib/state_machine/event_collection.rb +1 -1
- data/lib/state_machine/integrations/mongo_mapper.rb +1 -1
- data/lib/state_machine/integrations/mongoid.rb +1 -1
- data/lib/state_machine/machine.rb +12 -0
- data/lib/state_machine/macro_methods.rb +493 -0
- data/lib/state_machine/node_collection.rb +50 -18
- data/lib/state_machine/state.rb +0 -5
- data/lib/state_machine/state_collection.rb +1 -1
- data/lib/state_machine/version.rb +1 -1
- data/test/unit/event_collection_test.rb +34 -0
- data/test/unit/integrations/data_mapper_test.rb +38 -0
- data/test/unit/integrations/mongo_mapper_test.rb +28 -0
- data/test/unit/integrations/mongoid_test.rb +28 -0
- data/test/unit/machine_test.rb +15 -0
- data/test/unit/node_collection_test.rb +37 -9
- data/test/unit/state_collection_test.rb +38 -0
- data/test/unit/state_test.rb +1 -1
- metadata +11 -6
data/.yardopts
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# master
|
2
2
|
|
3
|
+
## 1.1.1 / 2011-12-31
|
4
|
+
|
5
|
+
* Fix fields being defined for Mongoid / MongoMapper state attributes even if they're already defined in the model
|
6
|
+
* Raise error when states / events are referenced in a definition with different types (e.g. both Strings and Symbols)
|
7
|
+
* Allow all states / events to be looked up by their string / symbol equivalent
|
8
|
+
* Allow state_machine to be loaded without extensions to the Ruby core
|
9
|
+
|
3
10
|
## 1.1.0 / 2011-11-13
|
4
11
|
|
5
12
|
* Allow the transitions / known states for an event to be reset
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -632,14 +632,73 @@ end
|
|
632
632
|
For more information about the various behaviors added for Sequel state
|
633
633
|
machines, see `StateMachine::Integrations::Sequel`.
|
634
634
|
|
635
|
-
##
|
635
|
+
## Additional Topics
|
636
|
+
|
637
|
+
### Symbols vs. Strings
|
638
|
+
|
639
|
+
In all of the examples used throughout the documentation, you'll notice that
|
640
|
+
states and events are almost always referenced as symbols. This isn't a
|
641
|
+
requirement, but rather a suggested best practice.
|
642
|
+
|
643
|
+
You can very well define your state machine with Strings like so:
|
644
|
+
|
645
|
+
```ruby
|
646
|
+
class Vehicle
|
647
|
+
state_machine :initial => 'parked' do
|
648
|
+
event 'ignite' do
|
649
|
+
transition 'parked' => 'idling'
|
650
|
+
end
|
651
|
+
|
652
|
+
# ...
|
653
|
+
end
|
654
|
+
end
|
655
|
+
```
|
656
|
+
|
657
|
+
You could even use numbers as your state / event names. The **important** thing
|
658
|
+
to keep in mind is that the type being used for referencing states / events in
|
659
|
+
your machine definition must be **consistent**. If you're using Symbols, then
|
660
|
+
all states / events must use Symbols. Otherwise you'll encounter the following
|
661
|
+
error:
|
662
|
+
|
663
|
+
```ruby
|
664
|
+
class Vehicle
|
665
|
+
state_machine do
|
666
|
+
event :ignite do
|
667
|
+
transition :parked => 'idling'
|
668
|
+
end
|
669
|
+
end
|
670
|
+
end
|
671
|
+
|
672
|
+
# => ArgumentError: "idling" state defined as String, :parked defined as Symbol; all states must be consistent
|
673
|
+
```
|
674
|
+
|
675
|
+
There **is** an exception to this rule. The consistency is only required within
|
676
|
+
the definition itself. However, when the machine's helper methods are called
|
677
|
+
with input from external sources, such as a web form, state_machine will map
|
678
|
+
that input to a String / Symbol. For example:
|
679
|
+
|
680
|
+
```ruby
|
681
|
+
class Vehicle
|
682
|
+
state_machine :initial => :parked do
|
683
|
+
event :ignite do
|
684
|
+
transition :parked => :idling
|
685
|
+
end
|
686
|
+
end
|
687
|
+
end
|
688
|
+
|
689
|
+
v = Vehicle.new # => #<Vehicle:0xb71da5f8 @state="parked">
|
690
|
+
v.state?('parked') # => true
|
691
|
+
v.state?(:parked) # => true
|
692
|
+
```
|
693
|
+
|
694
|
+
### Syntax flexibility
|
636
695
|
|
637
696
|
Although state_machine introduces a simplified syntax, it still remains
|
638
697
|
backwards compatible with previous versions and other state-related libraries by
|
639
698
|
providing some flexibility around how transitions are defined. See below for an
|
640
699
|
overview of these syntaxes.
|
641
700
|
|
642
|
-
|
701
|
+
#### Verbose syntax
|
643
702
|
|
644
703
|
In general, it's recommended that state machines use the implicit syntax for
|
645
704
|
transitions. However, you can be a little more explicit and verbose about
|
@@ -663,7 +722,7 @@ class Vehicle
|
|
663
722
|
end
|
664
723
|
```
|
665
724
|
|
666
|
-
|
725
|
+
#### Transition context
|
667
726
|
|
668
727
|
Some flexibility is provided around the context in which transitions can be
|
669
728
|
defined. In almost all examples throughout the documentation, transitions are
|
@@ -699,7 +758,7 @@ class Vehicle
|
|
699
758
|
end
|
700
759
|
```
|
701
760
|
|
702
|
-
In the above example, there's no need to specify the
|
761
|
+
In the above example, there's no need to specify the `from` state for each
|
703
762
|
transition since it's inferred from the context.
|
704
763
|
|
705
764
|
You can also define transitions completely outside the context of a particular
|
@@ -727,7 +786,7 @@ Notice that in these alternative syntaxes:
|
|
727
786
|
* You can continue to define `from` states (when in the machine context) using
|
728
787
|
the `all`, `any`, and `same` helper methods
|
729
788
|
|
730
|
-
|
789
|
+
### Static / Dynamic definitions
|
731
790
|
|
732
791
|
In most cases, the definition of a state machine is **static**. That is to say,
|
733
792
|
the states, events and possible transitions are known ahead of time even though
|
@@ -816,6 +875,39 @@ As you can see, state_machine provides enough flexibility for you to be able
|
|
816
875
|
to create new machine definitions on the fly based on an external source of
|
817
876
|
transitions.
|
818
877
|
|
878
|
+
### Core Extensions
|
879
|
+
|
880
|
+
By default, state_machine extends the Ruby core with a `state_machine` method on
|
881
|
+
`Class`. All other parts of the library are confined within the `StateMachine`
|
882
|
+
namespace. While this isn't wholly necessary, it also doesn't have any performance
|
883
|
+
impact and makes it truly feel like an extension to the language. This is very
|
884
|
+
similar to the way that you'll find `yaml`, `json`, or other libraries adding a
|
885
|
+
simple method to all objects just by loading the library.
|
886
|
+
|
887
|
+
However, if you'd like to avoid having state_machine add this extension to the
|
888
|
+
Ruby core, you can do so like so:
|
889
|
+
|
890
|
+
```ruby
|
891
|
+
require 'state_machine/core'
|
892
|
+
|
893
|
+
class Vehicle
|
894
|
+
extend StateMachine::MacroMethods
|
895
|
+
|
896
|
+
state_machine do
|
897
|
+
# ...
|
898
|
+
end
|
899
|
+
end
|
900
|
+
```
|
901
|
+
|
902
|
+
If you're using a gem loader like Bundler, you can explicitly indicate which
|
903
|
+
file to load:
|
904
|
+
|
905
|
+
```ruby
|
906
|
+
# In Gemfile
|
907
|
+
...
|
908
|
+
gem 'state_machine', :require => 'state_machine/core'
|
909
|
+
```
|
910
|
+
|
819
911
|
## Tools
|
820
912
|
|
821
913
|
### Generating graphs
|