state_machines 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/cssxfire.xml +9 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +12 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/state_machine2.iml +34 -0
- data/.idea/vcs.xml +9 -0
- data/.idea/workspace.xml +1156 -0
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +23 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/state_machines/assertions.rb +40 -0
- data/lib/state_machines/branch.rb +187 -0
- data/lib/state_machines/callback.rb +220 -0
- data/lib/state_machines/core.rb +25 -0
- data/lib/state_machines/core_ext/class/state_machine.rb +5 -0
- data/lib/state_machines/core_ext.rb +2 -0
- data/lib/state_machines/error.rb +13 -0
- data/lib/state_machines/eval_helpers.rb +87 -0
- data/lib/state_machines/event.rb +246 -0
- data/lib/state_machines/event_collection.rb +141 -0
- data/lib/state_machines/extensions.rb +148 -0
- data/lib/state_machines/helper_module.rb +17 -0
- data/lib/state_machines/integrations/base.rb +100 -0
- data/lib/state_machines/integrations.rb +113 -0
- data/lib/state_machines/machine.rb +2234 -0
- data/lib/state_machines/machine_collection.rb +84 -0
- data/lib/state_machines/macro_methods.rb +520 -0
- data/lib/state_machines/matcher.rb +123 -0
- data/lib/state_machines/matcher_helpers.rb +54 -0
- data/lib/state_machines/node_collection.rb +221 -0
- data/lib/state_machines/path.rb +120 -0
- data/lib/state_machines/path_collection.rb +90 -0
- data/lib/state_machines/state.rb +276 -0
- data/lib/state_machines/state_collection.rb +112 -0
- data/lib/state_machines/state_context.rb +138 -0
- data/lib/state_machines/transition.rb +470 -0
- data/lib/state_machines/transition_collection.rb +245 -0
- data/lib/state_machines/version.rb +3 -0
- data/lib/state_machines/yard.rb +8 -0
- data/lib/state_machines.rb +3 -0
- data/spec/errors/default_spec.rb +14 -0
- data/spec/errors/with_message_spec.rb +39 -0
- data/spec/helpers/helper_spec.rb +14 -0
- data/spec/internal/app/models/auto_shop.rb +31 -0
- data/spec/internal/app/models/car.rb +19 -0
- data/spec/internal/app/models/model_base.rb +6 -0
- data/spec/internal/app/models/motorcycle.rb +9 -0
- data/spec/internal/app/models/traffic_light.rb +47 -0
- data/spec/internal/app/models/vehicle.rb +123 -0
- data/spec/machine_spec.rb +3167 -0
- data/spec/matcher_helpers_spec.rb +39 -0
- data/spec/matcher_spec.rb +157 -0
- data/spec/models/auto_shop_spec.rb +41 -0
- data/spec/models/car_spec.rb +90 -0
- data/spec/models/motorcycle_spec.rb +44 -0
- data/spec/models/traffic_light_spec.rb +56 -0
- data/spec/models/vehicle_spec.rb +580 -0
- data/spec/node_collection_spec.rb +371 -0
- data/spec/path_collection_spec.rb +271 -0
- data/spec/path_spec.rb +488 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/state_collection_spec.rb +352 -0
- data/spec/state_context_spec.rb +442 -0
- data/spec/state_machine_spec.rb +29 -0
- data/spec/state_spec.rb +970 -0
- data/spec/support/migration_helpers.rb +50 -0
- data/spec/support/models.rb +6 -0
- data/spec/transition_collection_spec.rb +2199 -0
- data/spec/transition_spec.rb +1558 -0
- data/state_machines.gemspec +23 -0
- metadata +194 -0
@@ -0,0 +1,113 @@
|
|
1
|
+
module StateMachines
|
2
|
+
# An invalid integration was specified
|
3
|
+
class IntegrationNotFound < Error
|
4
|
+
def initialize(name)
|
5
|
+
super(nil, "#{name.inspect} is an invalid integration")
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Integrations allow state machines to take advantage of features within the
|
10
|
+
# context of a particular library. This is currently most useful with
|
11
|
+
# database libraries. For example, the various database integrations allow
|
12
|
+
# state machines to hook into features like:
|
13
|
+
# * Saving
|
14
|
+
# * Transactions
|
15
|
+
# * Observers
|
16
|
+
# * Scopes
|
17
|
+
# * Callbacks
|
18
|
+
# * Validation errors
|
19
|
+
#
|
20
|
+
# This type of integration allows the user to work with state machines in a
|
21
|
+
# fashion similar to other object models in their application.
|
22
|
+
#
|
23
|
+
# The integration interface is loosely defined by various unimplemented
|
24
|
+
# methods in the StateMachines::Machine class. See that class or the various
|
25
|
+
# built-in integrations for more information about how to define additional
|
26
|
+
# integrations.
|
27
|
+
module Integrations
|
28
|
+
# Attempts to find an integration that matches the given class. This will
|
29
|
+
# look through all of the built-in integrations under the StateMachines::Integrations
|
30
|
+
# namespace and find one that successfully matches the class.
|
31
|
+
#
|
32
|
+
# == Examples
|
33
|
+
#
|
34
|
+
# class Vehicle
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# class ActiveModelVehicle
|
38
|
+
# include ActiveModel::Observing
|
39
|
+
# include ActiveModel::Validations
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# class ActiveRecordVehicle < ActiveRecord::Base
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# class DataMapperVehicle
|
46
|
+
# include DataMapper::Resource
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# class MongoidVehicle
|
50
|
+
# include Mongoid::Document
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# class MongoMapperVehicle
|
54
|
+
# include MongoMapper::Document
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# class SequelVehicle < Sequel::Model
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# StateMachines::Integrations.match(Vehicle) # => nil
|
61
|
+
# StateMachines::Integrations.match(ActiveModelVehicle) # => StateMachines::Integrations::ActiveModel
|
62
|
+
# StateMachines::Integrations.match(ActiveRecordVehicle) # => StateMachines::Integrations::ActiveRecord
|
63
|
+
# StateMachines::Integrations.match(DataMapperVehicle) # => StateMachines::Integrations::DataMapper
|
64
|
+
# StateMachines::Integrations.match(MongoidVehicle) # => StateMachines::Integrations::Mongoid
|
65
|
+
# StateMachines::Integrations.match(MongoMapperVehicle) # => StateMachines::Integrations::MongoMapper
|
66
|
+
# StateMachines::Integrations.match(SequelVehicle) # => StateMachines::Integrations::Sequel
|
67
|
+
def self.match(klass)
|
68
|
+
all.detect {|integration| integration.matches?(klass)}
|
69
|
+
end
|
70
|
+
|
71
|
+
# Attempts to find an integration that matches the given list of ancestors.
|
72
|
+
# This will look through all of the built-in integrations under the StateMachines::Integrations
|
73
|
+
# namespace and find one that successfully matches one of the ancestors.
|
74
|
+
#
|
75
|
+
# == Examples
|
76
|
+
#
|
77
|
+
# StateMachines::Integrations.match([]) # => nil
|
78
|
+
# StateMachines::Integrations.match(['ActiveRecord::Base') # => StateMachines::Integrations::ActiveModel
|
79
|
+
def self.match_ancestors(ancestors)
|
80
|
+
all.detect {|integration| integration.matches_ancestors?(ancestors)}
|
81
|
+
end
|
82
|
+
|
83
|
+
# Finds an integration with the given name. If the integration cannot be
|
84
|
+
# found, then a NameError exception will be raised.
|
85
|
+
#
|
86
|
+
# == Examples
|
87
|
+
#
|
88
|
+
# StateMachines::Integrations.find_by_name(:active_record) # => StateMachines::Integrations::ActiveRecord
|
89
|
+
# StateMachines::Integrations.find_by_name(:active_model) # => StateMachines::Integrations::ActiveModel
|
90
|
+
# StateMachines::Integrations.find_by_name(:data_mapper) # => StateMachines::Integrations::DataMapper
|
91
|
+
# StateMachines::Integrations.find_by_name(:mongoid) # => StateMachines::Integrations::Mongoid
|
92
|
+
# StateMachines::Integrations.find_by_name(:mongo_mapper) # => StateMachines::Integrations::MongoMapper
|
93
|
+
# StateMachines::Integrations.find_by_name(:sequel) # => StateMachines::Integrations::Sequel
|
94
|
+
# StateMachines::Integrations.find_by_name(:invalid) # => StateMachines::IntegrationNotFound: :invalid is an invalid integration
|
95
|
+
def self.find_by_name(name)
|
96
|
+
all.detect {|integration| integration.integration_name == name} || raise(IntegrationNotFound.new(name))
|
97
|
+
end
|
98
|
+
|
99
|
+
# Gets a list of all of the available integrations for use. This will
|
100
|
+
# always list the ActiveModel integration last.
|
101
|
+
#
|
102
|
+
# == Example
|
103
|
+
#
|
104
|
+
# StateMachines::Integrations.all
|
105
|
+
# # => [StateMachines::Integrations::ActiveRecord, StateMachines::Integrations::DataMapper
|
106
|
+
# # StateMachines::Integrations::Mongoid, StateMachines::Integrations::MongoMapper,
|
107
|
+
# # StateMachines::Integrations::Sequel, StateMachines::Integrations::ActiveModel]
|
108
|
+
def self.all
|
109
|
+
constants = self.constants.map {|c| c.to_s}.select {|c| c }.sort
|
110
|
+
constants.map {|c| const_get(c)}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|