enum_state_machine 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.gitignore +12 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- metadata +83 -130
- data/.rvmrc +0 -1
- data/enum_state_machine.gemspec +0 -25
- data/lib/enum_state_machine.rb +0 -9
- data/lib/enum_state_machine/assertions.rb +0 -36
- data/lib/enum_state_machine/branch.rb +0 -225
- data/lib/enum_state_machine/callback.rb +0 -232
- data/lib/enum_state_machine/core.rb +0 -12
- data/lib/enum_state_machine/core_ext.rb +0 -2
- data/lib/enum_state_machine/core_ext/class/state_machine.rb +0 -5
- data/lib/enum_state_machine/error.rb +0 -13
- data/lib/enum_state_machine/eval_helpers.rb +0 -87
- data/lib/enum_state_machine/event.rb +0 -257
- data/lib/enum_state_machine/event_collection.rb +0 -141
- data/lib/enum_state_machine/extensions.rb +0 -149
- data/lib/enum_state_machine/graph.rb +0 -92
- data/lib/enum_state_machine/helper_module.rb +0 -17
- data/lib/enum_state_machine/initializers.rb +0 -4
- data/lib/enum_state_machine/initializers/rails.rb +0 -22
- data/lib/enum_state_machine/integrations.rb +0 -97
- data/lib/enum_state_machine/integrations/active_model.rb +0 -585
- data/lib/enum_state_machine/integrations/active_model/locale.rb +0 -11
- data/lib/enum_state_machine/integrations/active_model/observer.rb +0 -33
- data/lib/enum_state_machine/integrations/active_model/observer_update.rb +0 -42
- data/lib/enum_state_machine/integrations/active_model/versions.rb +0 -31
- data/lib/enum_state_machine/integrations/active_record.rb +0 -548
- data/lib/enum_state_machine/integrations/active_record/locale.rb +0 -20
- data/lib/enum_state_machine/integrations/active_record/versions.rb +0 -123
- data/lib/enum_state_machine/integrations/base.rb +0 -100
- data/lib/enum_state_machine/machine.rb +0 -2292
- data/lib/enum_state_machine/machine_collection.rb +0 -86
- data/lib/enum_state_machine/macro_methods.rb +0 -518
- data/lib/enum_state_machine/matcher.rb +0 -123
- data/lib/enum_state_machine/matcher_helpers.rb +0 -54
- data/lib/enum_state_machine/node_collection.rb +0 -222
- data/lib/enum_state_machine/path.rb +0 -120
- data/lib/enum_state_machine/path_collection.rb +0 -90
- data/lib/enum_state_machine/state.rb +0 -297
- data/lib/enum_state_machine/state_collection.rb +0 -112
- data/lib/enum_state_machine/state_context.rb +0 -138
- data/lib/enum_state_machine/state_enum.rb +0 -23
- data/lib/enum_state_machine/transition.rb +0 -470
- data/lib/enum_state_machine/transition_collection.rb +0 -245
- data/lib/enum_state_machine/version.rb +0 -3
- data/lib/enum_state_machine/yard.rb +0 -8
- data/lib/enum_state_machine/yard/handlers.rb +0 -12
- data/lib/enum_state_machine/yard/handlers/base.rb +0 -32
- data/lib/enum_state_machine/yard/handlers/event.rb +0 -25
- data/lib/enum_state_machine/yard/handlers/machine.rb +0 -344
- data/lib/enum_state_machine/yard/handlers/state.rb +0 -25
- data/lib/enum_state_machine/yard/handlers/transition.rb +0 -47
- data/lib/enum_state_machine/yard/templates.rb +0 -3
- data/lib/enum_state_machine/yard/templates/default/class/html/setup.rb +0 -30
- data/lib/enum_state_machine/yard/templates/default/class/html/state_machines.erb +0 -12
- data/lib/tasks/enum_state_machine.rake +0 -1
- data/lib/tasks/enum_state_machine.rb +0 -24
- data/lib/yard-enum_state_machine.rb +0 -2
- data/test/functional/state_machine_test.rb +0 -1066
- data/test/unit/integrations/active_model_test.rb +0 -1245
- data/test/unit/integrations/active_record_test.rb +0 -2551
- data/test/unit/integrations/base_test.rb +0 -104
- data/test/unit/integrations_test.rb +0 -71
- data/test/unit/invalid_event_test.rb +0 -20
- data/test/unit/invalid_parallel_transition_test.rb +0 -18
- data/test/unit/invalid_transition_test.rb +0 -115
- data/test/unit/machine_collection_test.rb +0 -603
- data/test/unit/machine_test.rb +0 -3395
- data/test/unit/state_machine_test.rb +0 -31
@@ -1,141 +0,0 @@
|
|
1
|
-
require 'enum_state_machine/node_collection'
|
2
|
-
|
3
|
-
module EnumStateMachine
|
4
|
-
# Represents a collection of events in a state machine
|
5
|
-
class EventCollection < NodeCollection
|
6
|
-
def initialize(machine) #:nodoc:
|
7
|
-
super(machine, :index => [:name, :qualified_name])
|
8
|
-
end
|
9
|
-
|
10
|
-
# Gets the list of events that can be fired on the given object.
|
11
|
-
#
|
12
|
-
# Valid requirement options:
|
13
|
-
# * <tt>:from</tt> - One or more states being transitioned from. If none
|
14
|
-
# are specified, then this will be the object's current state.
|
15
|
-
# * <tt>:to</tt> - One or more states being transitioned to. If none are
|
16
|
-
# specified, then this will match any to state.
|
17
|
-
# * <tt>:on</tt> - One or more events that fire the transition. If none
|
18
|
-
# are specified, then this will match any event.
|
19
|
-
# * <tt>:guard</tt> - Whether to guard transitions with the if/unless
|
20
|
-
# conditionals defined for each one. Default is true.
|
21
|
-
#
|
22
|
-
# == Examples
|
23
|
-
#
|
24
|
-
# class Vehicle
|
25
|
-
# state_machine :initial => :parked do
|
26
|
-
# event :park do
|
27
|
-
# transition :idling => :parked
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# event :ignite do
|
31
|
-
# transition :parked => :idling
|
32
|
-
# end
|
33
|
-
# end
|
34
|
-
# end
|
35
|
-
#
|
36
|
-
# events = Vehicle.state_machine(:state).events
|
37
|
-
#
|
38
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
39
|
-
# events.valid_for(vehicle) # => [#<EnumStateMachine::Event name=:ignite transitions=[:parked => :idling]>]
|
40
|
-
#
|
41
|
-
# vehicle.state = 'idling'
|
42
|
-
# events.valid_for(vehicle) # => [#<EnumStateMachine::Event name=:park transitions=[:idling => :parked]>]
|
43
|
-
def valid_for(object, requirements = {})
|
44
|
-
match(requirements).select {|event| event.can_fire?(object, requirements)}
|
45
|
-
end
|
46
|
-
|
47
|
-
# Gets the list of transitions that can be run on the given object.
|
48
|
-
#
|
49
|
-
# Valid requirement options:
|
50
|
-
# * <tt>:from</tt> - One or more states being transitioned from. If none
|
51
|
-
# are specified, then this will be the object's current state.
|
52
|
-
# * <tt>:to</tt> - One or more states being transitioned to. If none are
|
53
|
-
# specified, then this will match any to state.
|
54
|
-
# * <tt>:on</tt> - One or more events that fire the transition. If none
|
55
|
-
# are specified, then this will match any event.
|
56
|
-
# * <tt>:guard</tt> - Whether to guard transitions with the if/unless
|
57
|
-
# conditionals defined for each one. Default is true.
|
58
|
-
#
|
59
|
-
# == Examples
|
60
|
-
#
|
61
|
-
# class Vehicle
|
62
|
-
# state_machine :initial => :parked do
|
63
|
-
# event :park do
|
64
|
-
# transition :idling => :parked
|
65
|
-
# end
|
66
|
-
#
|
67
|
-
# event :ignite do
|
68
|
-
# transition :parked => :idling
|
69
|
-
# end
|
70
|
-
# end
|
71
|
-
# end
|
72
|
-
#
|
73
|
-
# events = Vehicle.state_machine.events
|
74
|
-
#
|
75
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
76
|
-
# events.transitions_for(vehicle) # => [#<EnumStateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
|
77
|
-
#
|
78
|
-
# vehicle.state = 'idling'
|
79
|
-
# events.transitions_for(vehicle) # => [#<EnumStateMachine::Transition attribute=:state event=:park from="idling" from_name=:idling to="parked" to_name=:parked>]
|
80
|
-
#
|
81
|
-
# # Search for explicit transitions regardless of the current state
|
82
|
-
# events.transitions_for(vehicle, :from => :parked) # => [#<EnumStateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
|
83
|
-
def transitions_for(object, requirements = {})
|
84
|
-
match(requirements).map {|event| event.transition_for(object, requirements)}.compact
|
85
|
-
end
|
86
|
-
|
87
|
-
# Gets the transition that should be performed for the event stored in the
|
88
|
-
# given object's event attribute. This also takes an additional parameter
|
89
|
-
# for automatically invalidating the object if the event or transition are
|
90
|
-
# invalid. By default, this is turned off.
|
91
|
-
#
|
92
|
-
# *Note* that if a transition has already been generated for the event, then
|
93
|
-
# that transition will be used.
|
94
|
-
#
|
95
|
-
# == Examples
|
96
|
-
#
|
97
|
-
# class Vehicle < ActiveRecord::Base
|
98
|
-
# state_machine :initial => :parked do
|
99
|
-
# event :ignite do
|
100
|
-
# transition :parked => :idling
|
101
|
-
# end
|
102
|
-
# end
|
103
|
-
# end
|
104
|
-
#
|
105
|
-
# vehicle = Vehicle.new # => #<Vehicle id: nil, state: "parked">
|
106
|
-
# events = Vehicle.state_machine.events
|
107
|
-
#
|
108
|
-
# vehicle.state_event = nil
|
109
|
-
# events.attribute_transition_for(vehicle) # => nil # Event isn't defined
|
110
|
-
#
|
111
|
-
# vehicle.state_event = 'invalid'
|
112
|
-
# events.attribute_transition_for(vehicle) # => false # Event is invalid
|
113
|
-
#
|
114
|
-
# vehicle.state_event = 'ignite'
|
115
|
-
# events.attribute_transition_for(vehicle) # => #<EnumStateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>
|
116
|
-
def attribute_transition_for(object, invalidate = false)
|
117
|
-
return unless machine.action
|
118
|
-
|
119
|
-
result = machine.read(object, :event_transition) || if event_name = machine.read(object, :event)
|
120
|
-
if event = self[event_name.to_sym, :name]
|
121
|
-
event.transition_for(object) || begin
|
122
|
-
# No valid transition: invalidate
|
123
|
-
machine.invalidate(object, :event, :invalid_event, [[:state, machine.states.match!(object).human_name(object.class)]]) if invalidate
|
124
|
-
false
|
125
|
-
end
|
126
|
-
else
|
127
|
-
# Event is unknown: invalidate
|
128
|
-
machine.invalidate(object, :event, :invalid) if invalidate
|
129
|
-
false
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
result
|
134
|
-
end
|
135
|
-
|
136
|
-
private
|
137
|
-
def match(requirements) #:nodoc:
|
138
|
-
requirements && requirements[:on] ? [fetch(requirements.delete(:on))] : self
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
@@ -1,149 +0,0 @@
|
|
1
|
-
require 'enum_state_machine/machine_collection'
|
2
|
-
|
3
|
-
module EnumStateMachine
|
4
|
-
module ClassMethods
|
5
|
-
def self.extended(base) #:nodoc:
|
6
|
-
base.class_eval do
|
7
|
-
@state_machines = MachineCollection.new
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
# Gets the current list of state machines defined for this class. This
|
12
|
-
# class-level attribute acts like an inheritable attribute. The attribute
|
13
|
-
# is available to each subclass, each having a copy of its superclass's
|
14
|
-
# attribute.
|
15
|
-
#
|
16
|
-
# The hash of state machines maps <tt>:attribute</tt> => +machine+, e.g.
|
17
|
-
#
|
18
|
-
# Vehicle.state_machines # => {:state => #<EnumStateMachine::Machine:0xb6f6e4a4 ...>}
|
19
|
-
def state_machines
|
20
|
-
@state_machines ||= superclass.state_machines.dup
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
module InstanceMethods
|
25
|
-
# Runs one or more events in parallel. All events will run through the
|
26
|
-
# following steps:
|
27
|
-
# * Before callbacks
|
28
|
-
# * Persist state
|
29
|
-
# * Invoke action
|
30
|
-
# * After callbacks
|
31
|
-
#
|
32
|
-
# For example, if two events (for state machines A and B) are run in
|
33
|
-
# parallel, the order in which steps are run is:
|
34
|
-
# * A - Before transition callbacks
|
35
|
-
# * B - Before transition callbacks
|
36
|
-
# * A - Persist new state
|
37
|
-
# * B - Persist new state
|
38
|
-
# * A - Invoke action
|
39
|
-
# * B - Invoke action (only if different than A's action)
|
40
|
-
# * A - After transition callbacks
|
41
|
-
# * B - After transition callbacks
|
42
|
-
#
|
43
|
-
# *Note* that multiple events on the same state machine / attribute cannot
|
44
|
-
# be run in parallel. If this is attempted, an ArgumentError will be
|
45
|
-
# raised.
|
46
|
-
#
|
47
|
-
# == Halting callbacks
|
48
|
-
#
|
49
|
-
# When running multiple events in parallel, special consideration should
|
50
|
-
# be taken with regard to how halting within callbacks affects the flow.
|
51
|
-
#
|
52
|
-
# For *before* callbacks, any <tt>:halt</tt> error that's thrown will
|
53
|
-
# immediately cancel the perform for all transitions. As a result, it's
|
54
|
-
# possible for one event's transition to affect the continuation of
|
55
|
-
# another.
|
56
|
-
#
|
57
|
-
# On the other hand, any <tt>:halt</tt> error that's thrown within an
|
58
|
-
# *after* callback with only affect that event's transition. Other
|
59
|
-
# transitions will continue to run their own callbacks.
|
60
|
-
#
|
61
|
-
# == Example
|
62
|
-
#
|
63
|
-
# class Vehicle
|
64
|
-
# state_machine :initial => :parked do
|
65
|
-
# event :ignite do
|
66
|
-
# transition :parked => :idling
|
67
|
-
# end
|
68
|
-
#
|
69
|
-
# event :park do
|
70
|
-
# transition :idling => :parked
|
71
|
-
# end
|
72
|
-
# end
|
73
|
-
#
|
74
|
-
# state_machine :alarm_state, :namespace => 'alarm', :initial => :on do
|
75
|
-
# event :enable do
|
76
|
-
# transition all => :active
|
77
|
-
# end
|
78
|
-
#
|
79
|
-
# event :disable do
|
80
|
-
# transition all => :off
|
81
|
-
# end
|
82
|
-
# end
|
83
|
-
# end
|
84
|
-
#
|
85
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c02850 @state="parked", @alarm_state="active">
|
86
|
-
# vehicle.state # => "parked"
|
87
|
-
# vehicle.alarm_state # => "active"
|
88
|
-
#
|
89
|
-
# vehicle.fire_events(:ignite, :disable_alarm) # => true
|
90
|
-
# vehicle.state # => "idling"
|
91
|
-
# vehicle.alarm_state # => "off"
|
92
|
-
#
|
93
|
-
# # If any event fails, the entire event chain fails
|
94
|
-
# vehicle.fire_events(:ignite, :enable_alarm) # => false
|
95
|
-
# vehicle.state # => "idling"
|
96
|
-
# vehicle.alarm_state # => "off"
|
97
|
-
#
|
98
|
-
# # Exception raised on invalid event
|
99
|
-
# vehicle.fire_events(:park, :invalid) # => EnumStateMachine::InvalidEvent: :invalid is an unknown event
|
100
|
-
# vehicle.state # => "idling"
|
101
|
-
# vehicle.alarm_state # => "off"
|
102
|
-
def fire_events(*events)
|
103
|
-
self.class.state_machines.fire_events(self, *events)
|
104
|
-
end
|
105
|
-
|
106
|
-
# Run one or more events in parallel. If any event fails to run, then
|
107
|
-
# a EnumStateMachine::InvalidTransition exception will be raised.
|
108
|
-
#
|
109
|
-
# See EnumStateMachine::InstanceMethods#fire_events for more information.
|
110
|
-
#
|
111
|
-
# == Example
|
112
|
-
#
|
113
|
-
# class Vehicle
|
114
|
-
# state_machine :initial => :parked do
|
115
|
-
# event :ignite do
|
116
|
-
# transition :parked => :idling
|
117
|
-
# end
|
118
|
-
#
|
119
|
-
# event :park do
|
120
|
-
# transition :idling => :parked
|
121
|
-
# end
|
122
|
-
# end
|
123
|
-
#
|
124
|
-
# state_machine :alarm_state, :namespace => 'alarm', :initial => :active do
|
125
|
-
# event :enable do
|
126
|
-
# transition all => :active
|
127
|
-
# end
|
128
|
-
#
|
129
|
-
# event :disable do
|
130
|
-
# transition all => :off
|
131
|
-
# end
|
132
|
-
# end
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c02850 @state="parked", @alarm_state="active">
|
136
|
-
# vehicle.fire_events(:ignite, :disable_alarm) # => true
|
137
|
-
#
|
138
|
-
# vehicle.fire_events!(:ignite, :disable_alarm) # => EnumStateMachine::InvalidTranstion: Cannot run events in parallel: ignite, disable_alarm
|
139
|
-
def fire_events!(*events)
|
140
|
-
run_action = [true, false].include?(events.last) ? events.pop : true
|
141
|
-
fire_events(*(events + [run_action])) || raise(EnumStateMachine::InvalidParallelTransition.new(self, events))
|
142
|
-
end
|
143
|
-
|
144
|
-
protected
|
145
|
-
def initialize_state_machines(options = {}, &block) #:nodoc:
|
146
|
-
self.class.state_machines.initialize_states(self, options, &block)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
@@ -1,92 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'rubygems'
|
3
|
-
gem 'ruby-graphviz', '>=0.9.17'
|
4
|
-
require 'graphviz'
|
5
|
-
rescue LoadError => ex
|
6
|
-
$stderr.puts "Cannot draw the machine (#{ex.message}). `gem install ruby-graphviz` >= v0.9.17 and try again."
|
7
|
-
raise
|
8
|
-
end
|
9
|
-
|
10
|
-
require 'enum_state_machine/assertions'
|
11
|
-
|
12
|
-
module EnumStateMachine
|
13
|
-
# Provides a set of higher-order features on top of the raw GraphViz graphs
|
14
|
-
class Graph < GraphViz
|
15
|
-
include Assertions
|
16
|
-
|
17
|
-
# The name of the font to draw state names in
|
18
|
-
attr_reader :font
|
19
|
-
|
20
|
-
# The graph's full filename
|
21
|
-
attr_reader :file_path
|
22
|
-
|
23
|
-
# The image format to generate the graph in
|
24
|
-
attr_reader :file_format
|
25
|
-
|
26
|
-
# Creates a new graph with the given name.
|
27
|
-
#
|
28
|
-
# Configuration options:
|
29
|
-
# * <tt>:path</tt> - The path to write the graph file to. Default is the
|
30
|
-
# current directory (".").
|
31
|
-
# * <tt>:format</tt> - The image format to generate the graph in.
|
32
|
-
# Default is "png'.
|
33
|
-
# * <tt>:font</tt> - The name of the font to draw state names in.
|
34
|
-
# Default is "Arial".
|
35
|
-
# * <tt>:orientation</tt> - The direction of the graph ("portrait" or
|
36
|
-
# "landscape"). Default is "portrait".
|
37
|
-
def initialize(name, options = {})
|
38
|
-
options = {:path => '.', :format => 'png', :font => 'Arial', :orientation => 'portrait'}.merge(options)
|
39
|
-
assert_valid_keys(options, :path, :format, :font, :orientation)
|
40
|
-
|
41
|
-
@font = options[:font]
|
42
|
-
@file_path = File.join(options[:path], "#{name}.#{options[:format]}")
|
43
|
-
@file_format = options[:format]
|
44
|
-
|
45
|
-
super('G', :rankdir => options[:orientation] == 'landscape' ? 'LR' : 'TB')
|
46
|
-
end
|
47
|
-
|
48
|
-
# Generates the actual image file based on the nodes / edges added to the
|
49
|
-
# graph. The path to the file is based on the configuration options for
|
50
|
-
# this graph.
|
51
|
-
def output
|
52
|
-
super(@file_format => @file_path)
|
53
|
-
end
|
54
|
-
|
55
|
-
# Adds a new node to the graph. The font for the node will be automatically
|
56
|
-
# set based on the graph configuration. The generated node will be returned.
|
57
|
-
#
|
58
|
-
# For example,
|
59
|
-
#
|
60
|
-
# graph = EnumStateMachine::Graph.new('test')
|
61
|
-
# graph.add_nodes('parked', :label => 'Parked', :width => '1', :height => '1', :shape => 'ellipse')
|
62
|
-
def add_nodes(*args)
|
63
|
-
node = v0_api? ? add_node(*args) : super
|
64
|
-
node.fontname = @font
|
65
|
-
node
|
66
|
-
end
|
67
|
-
|
68
|
-
# Adds a new edge to the graph. The font for the edge will be automatically
|
69
|
-
# set based on the graph configuration. The generated edge will be returned.
|
70
|
-
#
|
71
|
-
# For example,
|
72
|
-
#
|
73
|
-
# graph = EnumStateMachine::Graph.new('test')
|
74
|
-
# graph.add_edges('parked', 'idling', :label => 'ignite')
|
75
|
-
def add_edges(*args)
|
76
|
-
edge = v0_api? ? add_edge(*args) : super
|
77
|
-
edge.fontname = @font
|
78
|
-
edge
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
# Determines whether the old v0 api is in use
|
83
|
-
def v0_api?
|
84
|
-
version[0] == '0' || version[0] == '1' && version[1] == '0' && version[2] <= '2'
|
85
|
-
end
|
86
|
-
|
87
|
-
# The ruby-graphviz version data
|
88
|
-
def version
|
89
|
-
Constants::RGV_VERSION.split('.')
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module EnumStateMachine
|
2
|
-
# Represents a type of module that defines instance / class methods for a
|
3
|
-
# state machine
|
4
|
-
class HelperModule < Module #:nodoc:
|
5
|
-
def initialize(machine, kind)
|
6
|
-
@machine = machine
|
7
|
-
@kind = kind
|
8
|
-
end
|
9
|
-
|
10
|
-
# Provides a human-readable description of the module
|
11
|
-
def to_s
|
12
|
-
owner_class = @machine.owner_class
|
13
|
-
owner_class_name = owner_class.name && !owner_class.name.empty? ? owner_class.name : owner_class.to_s
|
14
|
-
"#{owner_class_name} #{@machine.name.inspect} #{@kind} helpers"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
if defined?(Rails)
|
2
|
-
# Track all of the applicable locales to load
|
3
|
-
locale_paths = []
|
4
|
-
EnumStateMachine::Integrations.all.each do |integration|
|
5
|
-
locale_paths << integration.locale_path if integration.available? && integration.locale_path
|
6
|
-
end
|
7
|
-
|
8
|
-
if defined?(Rails::Engine)
|
9
|
-
# Rails 3.x
|
10
|
-
class EnumStateMachine::RailsEngine < Rails::Engine
|
11
|
-
rake_tasks do
|
12
|
-
load 'tasks/enum_state_machine.rb'
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 0
|
17
|
-
EnumStateMachine::RailsEngine.paths.config.locales = locale_paths
|
18
|
-
else
|
19
|
-
EnumStateMachine::RailsEngine.paths['config/locales'] = locale_paths
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,97 +0,0 @@
|
|
1
|
-
# Load each available integration
|
2
|
-
require 'enum_state_machine/integrations/base'
|
3
|
-
Dir["#{File.dirname(__FILE__)}/integrations/*.rb"].sort.each do |path|
|
4
|
-
require "enum_state_machine/integrations/#{File.basename(path)}"
|
5
|
-
end
|
6
|
-
|
7
|
-
require 'enum_state_machine/error'
|
8
|
-
|
9
|
-
module EnumStateMachine
|
10
|
-
# An invalid integration was specified
|
11
|
-
class IntegrationNotFound < Error
|
12
|
-
def initialize(name)
|
13
|
-
super(nil, "#{name.inspect} is an invalid integration")
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
# Integrations allow state machines to take advantage of features within the
|
18
|
-
# context of a particular library. This is currently most useful with
|
19
|
-
# database libraries. For example, the various database integrations allow
|
20
|
-
# state machines to hook into features like:
|
21
|
-
# * Saving
|
22
|
-
# * Transactions
|
23
|
-
# * Observers
|
24
|
-
# * Scopes
|
25
|
-
# * Callbacks
|
26
|
-
# * Validation errors
|
27
|
-
#
|
28
|
-
# This type of integration allows the user to work with state machines in a
|
29
|
-
# fashion similar to other object models in their application.
|
30
|
-
#
|
31
|
-
# The integration interface is loosely defined by various unimplemented
|
32
|
-
# methods in the EnumStateMachine::Machine class. See that class or the various
|
33
|
-
# built-in integrations for more information about how to define additional
|
34
|
-
# integrations.
|
35
|
-
module Integrations
|
36
|
-
# Attempts to find an integration that matches the given class. This will
|
37
|
-
# look through all of the built-in integrations under the EnumStateMachine::Integrations
|
38
|
-
# namespace and find one that successfully matches the class.
|
39
|
-
#
|
40
|
-
# == Examples
|
41
|
-
#
|
42
|
-
# class Vehicle
|
43
|
-
# end
|
44
|
-
#
|
45
|
-
# class ActiveModelVehicle
|
46
|
-
# include ActiveModel::Observing
|
47
|
-
# include ActiveModel::Validations
|
48
|
-
# end
|
49
|
-
#
|
50
|
-
# class ActiveRecordVehicle < ActiveRecord::Base
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
#
|
54
|
-
# EnumStateMachine::Integrations.match(Vehicle) # => nil
|
55
|
-
# EnumStateMachine::Integrations.match(ActiveModelVehicle) # => EnumStateMachine::Integrations::ActiveModel
|
56
|
-
# EnumStateMachine::Integrations.match(ActiveRecordVehicle) # => EnumStateMachine::Integrations::ActiveRecord
|
57
|
-
def self.match(klass)
|
58
|
-
all.detect {|integration| integration.matches?(klass)}
|
59
|
-
end
|
60
|
-
|
61
|
-
# Attempts to find an integration that matches the given list of ancestors.
|
62
|
-
# This will look through all of the built-in integrations under the EnumStateMachine::Integrations
|
63
|
-
# namespace and find one that successfully matches one of the ancestors.
|
64
|
-
#
|
65
|
-
# == Examples
|
66
|
-
#
|
67
|
-
# EnumStateMachine::Integrations.match([]) # => nil
|
68
|
-
# EnumStateMachine::Integrations.match(['ActiveRecord::Base') # => EnumStateMachine::Integrations::ActiveModel
|
69
|
-
def self.match_ancestors(ancestors)
|
70
|
-
all.detect {|integration| integration.matches_ancestors?(ancestors)}
|
71
|
-
end
|
72
|
-
|
73
|
-
# Finds an integration with the given name. If the integration cannot be
|
74
|
-
# found, then a NameError exception will be raised.
|
75
|
-
#
|
76
|
-
# == Examples
|
77
|
-
#
|
78
|
-
# EnumStateMachine::Integrations.find_by_name(:active_record) # => EnumStateMachine::Integrations::ActiveRecord
|
79
|
-
# EnumStateMachine::Integrations.find_by_name(:active_model) # => EnumStateMachine::Integrations::ActiveModel
|
80
|
-
# EnumStateMachine::Integrations.find_by_name(:invalid) # => EnumStateMachine::IntegrationNotFound: :invalid is an invalid integration
|
81
|
-
def self.find_by_name(name)
|
82
|
-
all.detect {|integration| integration.integration_name == name} || raise(IntegrationNotFound.new(name))
|
83
|
-
end
|
84
|
-
|
85
|
-
# Gets a list of all of the available integrations for use. This will
|
86
|
-
# always list the ActiveModel integration last.
|
87
|
-
#
|
88
|
-
# == Example
|
89
|
-
#
|
90
|
-
# EnumStateMachine::Integrations.all
|
91
|
-
# # => [EnumStateMachine::Integrations::ActiveRecord, EnumStateMachine::Integrations::ActiveModel]
|
92
|
-
def self.all
|
93
|
-
constants = self.constants.map {|c| c.to_s}.select {|c| c != 'ActiveModel'}.sort << 'ActiveModel'
|
94
|
-
constants.map {|c| const_get(c)}
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|