branston 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/branston/Gemfile +25 -0
- data/lib/branston/Gemfile.lock +76 -0
- data/lib/branston/app/controllers/application_controller.rb +1 -1
- data/lib/branston/app/controllers/outcomes_controller.rb +2 -0
- data/lib/branston/app/controllers/stories_controller.rb +82 -86
- data/lib/branston/app/controllers/users_controller.rb +69 -11
- data/lib/branston/app/helpers/iterations_helper.rb +13 -13
- data/lib/branston/app/models/iteration.rb +3 -1
- data/lib/branston/app/models/release.rb +0 -1
- data/lib/branston/app/models/story.rb +30 -28
- data/lib/branston/app/models/user.rb +46 -1
- data/lib/branston/app/views/layouts/_header.html.erb +8 -3
- data/lib/branston/app/views/layouts/user_roles.html.erb +5 -5
- data/lib/branston/app/views/sessions/new.html.erb +8 -14
- data/lib/branston/app/views/users/_admin_controls.html.erb +14 -0
- data/lib/branston/app/views/users/_form.html.erb +27 -0
- data/lib/branston/app/views/users/edit.html.erb +9 -0
- data/lib/branston/app/views/users/index.html.erb +14 -0
- data/lib/branston/app/views/users/new.html.erb +3 -22
- data/lib/branston/config/boot.rb +20 -0
- data/lib/branston/config/environment.rb +2 -7
- data/lib/branston/config/environments/test.rb +0 -8
- data/lib/branston/config/preinitializer.rb +21 -0
- data/lib/branston/config/routes.rb +15 -10
- data/lib/branston/db/development.sqlite3 +0 -0
- data/lib/branston/db/development_structure.sql +21 -8
- data/lib/branston/db/migrate/20100723161424_add_state_to_user.rb +12 -0
- data/lib/branston/db/migrate/20100726150322_add_activation_fields_to_user.rb +12 -0
- data/lib/branston/db/migrate/20100729125551_set_default_user_state_to_pending.rb +10 -0
- data/lib/branston/db/migrate/20100812133837_add_is_admin_property_to_user.rb +10 -0
- data/lib/branston/db/migrate/20100812140532_set_default_user_state_to_active.rb +10 -0
- data/lib/branston/db/migrate/20100812143455_add_default_admin_user.rb +17 -0
- data/lib/branston/db/migrate/20110408162438_remove_is_admin_property_and_add_role_instead.rb +12 -0
- data/lib/branston/db/pristine.sqlite3 +0 -0
- data/lib/branston/db/schema.rb +6 -8
- data/lib/branston/db/test.sqlite3 +0 -0
- data/lib/branston/log/development.log +1181 -433
- data/lib/branston/log/test.log +145306 -52026
- data/lib/branston/test/blueprints.rb +22 -28
- data/lib/branston/test/functional/iterations_controller_test.rb +149 -113
- data/lib/branston/test/functional/outcomes_controller_test.rb +94 -60
- data/lib/branston/test/functional/preconditions_controller_test.rb +101 -67
- data/lib/branston/test/functional/releases_controller_test.rb +85 -49
- data/lib/branston/test/functional/scenarios_controller_test.rb +104 -70
- data/lib/branston/test/functional/stories_controller_test.rb +41 -12
- data/lib/branston/test/functional/users_controller_test.rb +364 -43
- data/lib/branston/test/unit/iteration_test.rb +37 -6
- data/lib/branston/test/unit/outcome_test.rb +2 -2
- data/lib/branston/test/unit/participation_test.rb +2 -2
- data/lib/branston/test/unit/precondition_test.rb +3 -3
- data/lib/branston/test/unit/release_test.rb +4 -0
- data/lib/branston/test/unit/scenario_test.rb +4 -4
- data/lib/branston/test/unit/story_test.rb +62 -40
- data/lib/branston/test/unit/user_test.rb +195 -5
- metadata +136 -156
- data/lib/branston/app/controllers/user_roles_controller.rb +0 -105
- data/lib/branston/app/helpers/user_roles_helper.rb +0 -2
- data/lib/branston/app/models/user_role.rb +0 -21
- data/lib/branston/app/views/layouts/outcomes.html.erb +0 -17
- data/lib/branston/app/views/layouts/preconditions.html.erb +0 -17
- data/lib/branston/app/views/layouts/releases.html.erb +0 -17
- data/lib/branston/app/views/user_roles/edit.html.erb +0 -16
- data/lib/branston/app/views/user_roles/index.html.erb +0 -20
- data/lib/branston/app/views/user_roles/new.html.erb +0 -15
- data/lib/branston/app/views/user_roles/show.html.erb +0 -8
- data/lib/branston/coverage/app-controllers-application_controller_rb.html +0 -231
- data/lib/branston/coverage/app-controllers-iterations_controller_rb.html +0 -801
- data/lib/branston/coverage/app-controllers-outcomes_controller_rb.html +0 -759
- data/lib/branston/coverage/app-controllers-preconditions_controller_rb.html +0 -783
- data/lib/branston/coverage/app-controllers-releases_controller_rb.html +0 -705
- data/lib/branston/coverage/app-controllers-scenarios_controller_rb.html +0 -777
- data/lib/branston/coverage/app-controllers-sessions_controller_rb.html +0 -411
- data/lib/branston/coverage/app-controllers-stories_controller_rb.html +0 -1071
- data/lib/branston/coverage/app-controllers-user_roles_controller_rb.html +0 -693
- data/lib/branston/coverage/app-controllers-users_controller_rb.html +0 -315
- data/lib/branston/coverage/app-helpers-application_helper_rb.html +0 -327
- data/lib/branston/coverage/app-helpers-iterations_helper_rb.html +0 -363
- data/lib/branston/coverage/app-helpers-outcomes_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-preconditions_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-releases_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-sessions_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-stories_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-user_roles_helper_rb.html +0 -75
- data/lib/branston/coverage/app-models-iteration_rb.html +0 -321
- data/lib/branston/coverage/app-models-outcome_rb.html +0 -243
- data/lib/branston/coverage/app-models-participation_rb.html +0 -189
- data/lib/branston/coverage/app-models-precondition_rb.html +0 -243
- data/lib/branston/coverage/app-models-release_rb.html +0 -195
- data/lib/branston/coverage/app-models-scenario_rb.html +0 -231
- data/lib/branston/coverage/app-models-story_rb.html +0 -621
- data/lib/branston/coverage/app-models-user_rb.html +0 -513
- data/lib/branston/coverage/app-models-user_role_rb.html +0 -189
- data/lib/branston/coverage/index.html +0 -570
- data/lib/branston/coverage/jquery-1.3.2.min.js +0 -19
- data/lib/branston/coverage/jquery.tablesorter.min.js +0 -15
- data/lib/branston/coverage/lib-client_rb.html +0 -537
- data/lib/branston/coverage/lib-faker_extras_rb.html +0 -207
- data/lib/branston/coverage/lib-story_generator_rb.html +0 -873
- data/lib/branston/coverage/print.css +0 -12
- data/lib/branston/coverage/rcov.js +0 -42
- data/lib/branston/coverage/screen.css +0 -270
- data/lib/branston/db/migrate/20091127131037_create_user_roles.rb +0 -13
- data/lib/branston/db/migrate/20091127172950_add_story_id_to_user_role.rb +0 -10
- data/lib/branston/test/functional/user_roles_controller_test.rb +0 -71
- data/lib/branston/test/unit/helpers/user_roles_helper_test.rb +0 -4
- data/lib/branston/test/unit/user_role_test.rb +0 -9
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_flat.txt +0 -8
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_graph.html +0 -6718
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_tree.txt +0 -9942
- data/lib/branston/vendor/plugins/state_machine/CHANGELOG.rdoc +0 -298
- data/lib/branston/vendor/plugins/state_machine/LICENSE +0 -20
- data/lib/branston/vendor/plugins/state_machine/README.rdoc +0 -466
- data/lib/branston/vendor/plugins/state_machine/Rakefile +0 -98
- data/lib/branston/vendor/plugins/state_machine/examples/AutoShop_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/Car_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/TrafficLight_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/Vehicle_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/auto_shop.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/examples/car.rb +0 -19
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/controller.rb +0 -51
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/model.rb +0 -28
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_edit.html.erb +0 -24
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_index.html.erb +0 -23
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_new.html.erb +0 -13
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_show.html.erb +0 -17
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/controller.rb +0 -43
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/migration.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/model.rb +0 -23
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_edit.html.erb +0 -25
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_index.html.erb +0 -23
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_new.html.erb +0 -14
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_show.html.erb +0 -17
- data/lib/branston/vendor/plugins/state_machine/examples/traffic_light.rb +0 -7
- data/lib/branston/vendor/plugins/state_machine/examples/vehicle.rb +0 -31
- data/lib/branston/vendor/plugins/state_machine/init.rb +0 -1
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine.rb +0 -388
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/assertions.rb +0 -36
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/callback.rb +0 -189
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/condition_proxy.rb +0 -94
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/eval_helpers.rb +0 -67
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event.rb +0 -252
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event_collection.rb +0 -122
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/extensions.rb +0 -149
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/guard.rb +0 -230
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations.rb +0 -68
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record.rb +0 -492
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/locale.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb +0 -41
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper.rb +0 -351
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper/observer.rb +0 -139
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/sequel.rb +0 -322
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine.rb +0 -1467
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine_collection.rb +0 -155
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher.rb +0 -123
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher_helpers.rb +0 -54
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/node_collection.rb +0 -152
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state.rb +0 -249
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state_collection.rb +0 -112
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/transition.rb +0 -394
- data/lib/branston/vendor/plugins/state_machine/state_machine.gemspec +0 -30
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rake +0 -1
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rb +0 -30
- data/lib/branston/vendor/plugins/state_machine/test/classes/switch.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/test/functional/state_machine_test.rb +0 -941
- data/lib/branston/vendor/plugins/state_machine/test/test_helper.rb +0 -4
- data/lib/branston/vendor/plugins/state_machine/test/unit/assertions_test.rb +0 -40
- data/lib/branston/vendor/plugins/state_machine/test/unit/callback_test.rb +0 -455
- data/lib/branston/vendor/plugins/state_machine/test/unit/condition_proxy_test.rb +0 -328
- data/lib/branston/vendor/plugins/state_machine/test/unit/eval_helpers_test.rb +0 -120
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_collection_test.rb +0 -326
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_test.rb +0 -743
- data/lib/branston/vendor/plugins/state_machine/test/unit/guard_test.rb +0 -908
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/active_record_test.rb +0 -1367
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/data_mapper_test.rb +0 -962
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/sequel_test.rb +0 -859
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations_test.rb +0 -42
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_event_test.rb +0 -7
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_transition_test.rb +0 -7
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb +0 -938
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_test.rb +0 -2004
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_helpers_test.rb +0 -37
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_test.rb +0 -155
- data/lib/branston/vendor/plugins/state_machine/test/unit/node_collection_test.rb +0 -207
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_collection_test.rb +0 -280
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_machine_test.rb +0 -31
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_test.rb +0 -795
- data/lib/branston/vendor/plugins/state_machine/test/unit/transition_test.rb +0 -1212
@@ -1,36 +0,0 @@
|
|
1
|
-
module StateMachine
|
2
|
-
# Provides a set of helper methods for making assertions about the content
|
3
|
-
# of various objects
|
4
|
-
module Assertions
|
5
|
-
# Validates that the given hash *only* includes the specified valid keys.
|
6
|
-
# If any invalid keys are found, an ArgumentError will be raised.
|
7
|
-
#
|
8
|
-
# == Examples
|
9
|
-
#
|
10
|
-
# options = {:name => 'John Smith', :age => 30}
|
11
|
-
#
|
12
|
-
# assert_valid_keys(options, :name) # => ArgumentError: Invalid key(s): age
|
13
|
-
# assert_valid_keys(options, 'name', 'age') # => ArgumentError: Invalid key(s): age, name
|
14
|
-
# assert_valid_keys(options, :name, :age) # => nil
|
15
|
-
def assert_valid_keys(hash, *valid_keys)
|
16
|
-
invalid_keys = hash.keys - valid_keys
|
17
|
-
raise ArgumentError, "Invalid key(s): #{invalid_keys.join(', ')}" unless invalid_keys.empty?
|
18
|
-
end
|
19
|
-
|
20
|
-
# Validates that the given hash only includes at *most* one of a set of
|
21
|
-
# exclusive keys. If more than one key is found, an ArgumentError will be
|
22
|
-
# raised.
|
23
|
-
#
|
24
|
-
# == Examples
|
25
|
-
#
|
26
|
-
# options = {:only => :on, :except => :off}
|
27
|
-
# assert_exclusive_keys(options, :only) # => nil
|
28
|
-
# assert_exclusive_keys(options, :except) # => nil
|
29
|
-
# assert_exclusive_keys(options, :only, :except) # => ArgumentError: Conflicting keys: only, except
|
30
|
-
# assert_exclusive_keys(options, :only, :except, :with) # => ArgumentError: Conflicting keys: only, except
|
31
|
-
def assert_exclusive_keys(hash, *exclusive_keys)
|
32
|
-
conflicting_keys = exclusive_keys & hash.keys
|
33
|
-
raise ArgumentError, "Conflicting keys: #{conflicting_keys.join(', ')}" unless conflicting_keys.length <= 1
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,189 +0,0 @@
|
|
1
|
-
require 'state_machine/guard'
|
2
|
-
require 'state_machine/eval_helpers'
|
3
|
-
|
4
|
-
module StateMachine
|
5
|
-
# Callbacks represent hooks into objects that allow logic to be triggered
|
6
|
-
# before or after a specific transition occurs.
|
7
|
-
class Callback
|
8
|
-
include EvalHelpers
|
9
|
-
|
10
|
-
class << self
|
11
|
-
# Determines whether to automatically bind the callback to the object
|
12
|
-
# being transitioned. This only applies to callbacks that are defined as
|
13
|
-
# lambda blocks (or Procs). Some integrations, such as DataMapper, handle
|
14
|
-
# callbacks by executing them bound to the object involved, while other
|
15
|
-
# integrations, such as ActiveRecord, pass the object as an argument to
|
16
|
-
# the callback. This can be configured on an application-wide basis by
|
17
|
-
# setting this configuration to +true+ or +false+. The default value
|
18
|
-
# is +false+.
|
19
|
-
#
|
20
|
-
# *Note* that the DataMapper and Sequel integrations automatically
|
21
|
-
# configure this value on a per-callback basis, so it does not have to
|
22
|
-
# be enabled application-wide.
|
23
|
-
#
|
24
|
-
# == Examples
|
25
|
-
#
|
26
|
-
# When not bound to the object:
|
27
|
-
#
|
28
|
-
# class Vehicle
|
29
|
-
# state_machine do
|
30
|
-
# before_transition do |vehicle|
|
31
|
-
# vehicle.set_alarm
|
32
|
-
# end
|
33
|
-
# end
|
34
|
-
#
|
35
|
-
# def set_alarm
|
36
|
-
# ...
|
37
|
-
# end
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# When bound to the object:
|
41
|
-
#
|
42
|
-
# StateMachine::Callback.bind_to_object = true
|
43
|
-
#
|
44
|
-
# class Vehicle
|
45
|
-
# state_machine do
|
46
|
-
# before_transition do
|
47
|
-
# self.set_alarm
|
48
|
-
# end
|
49
|
-
# end
|
50
|
-
#
|
51
|
-
# def set_alarm
|
52
|
-
# ...
|
53
|
-
# end
|
54
|
-
# end
|
55
|
-
attr_accessor :bind_to_object
|
56
|
-
|
57
|
-
# The application-wide terminator to use for callbacks when not
|
58
|
-
# explicitly defined. Terminators determine whether to cancel a
|
59
|
-
# callback chain based on the return value of the callback.
|
60
|
-
#
|
61
|
-
# See StateMachine::Callback#terminator for more information.
|
62
|
-
attr_accessor :terminator
|
63
|
-
end
|
64
|
-
|
65
|
-
# An optional block for determining whether to cancel the callback chain
|
66
|
-
# based on the return value of the callback. By default, the callback
|
67
|
-
# chain never cancels based on the return value (i.e. there is no implicit
|
68
|
-
# terminator). Certain integrations, such as ActiveRecord and Sequel,
|
69
|
-
# change this default value.
|
70
|
-
#
|
71
|
-
# == Examples
|
72
|
-
#
|
73
|
-
# Canceling the callback chain without a terminator:
|
74
|
-
#
|
75
|
-
# class Vehicle
|
76
|
-
# state_machine do
|
77
|
-
# before_transition do |vehicle|
|
78
|
-
# throw :halt
|
79
|
-
# end
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# Canceling the callback chain with a terminator value of +false+:
|
84
|
-
#
|
85
|
-
# class Vehicle
|
86
|
-
# state_machine do
|
87
|
-
# before_transition do |vehicle|
|
88
|
-
# false
|
89
|
-
# end
|
90
|
-
# end
|
91
|
-
# end
|
92
|
-
attr_reader :terminator
|
93
|
-
|
94
|
-
# The guard that determines whether or not this callback can be invoked
|
95
|
-
# based on the context of the transition. The event, from state, and
|
96
|
-
# to state must all match in order for the guard to pass.
|
97
|
-
#
|
98
|
-
# See StateMachine::Guard for more information.
|
99
|
-
attr_reader :guard
|
100
|
-
|
101
|
-
# Creates a new callback that can get called based on the configured
|
102
|
-
# options.
|
103
|
-
#
|
104
|
-
# In addition to the possible configuration options for guards, the
|
105
|
-
# following options can be configured:
|
106
|
-
# * <tt>:bind_to_object</tt> - Whether to bind the callback to the object involved.
|
107
|
-
# If set to false, the object will be passed as a parameter instead.
|
108
|
-
# Default is integration-specific or set to the application default.
|
109
|
-
# * <tt>:terminator</tt> - A block/proc that determines what callback
|
110
|
-
# results should cause the callback chain to halt (if not using the
|
111
|
-
# default <tt>throw :halt</tt> technique).
|
112
|
-
#
|
113
|
-
# More information about how those options affect the behavior of the
|
114
|
-
# callback can be found in their attribute definitions.
|
115
|
-
def initialize(*args, &block)
|
116
|
-
options = args.last.is_a?(Hash) ? args.pop : {}
|
117
|
-
@methods = args
|
118
|
-
@methods.concat(Array(options.delete(:do)))
|
119
|
-
@methods << block if block_given?
|
120
|
-
|
121
|
-
raise ArgumentError, 'Method(s) for callback must be specified' unless @methods.any?
|
122
|
-
|
123
|
-
options = {:bind_to_object => self.class.bind_to_object, :terminator => self.class.terminator}.merge(options)
|
124
|
-
|
125
|
-
# Proxy lambda blocks so that they're bound to the object
|
126
|
-
bind_to_object = options.delete(:bind_to_object)
|
127
|
-
@methods.map! do |method|
|
128
|
-
bind_to_object && method.is_a?(Proc) ? bound_method(method) : method
|
129
|
-
end
|
130
|
-
|
131
|
-
@terminator = options.delete(:terminator)
|
132
|
-
@guard = Guard.new(options)
|
133
|
-
end
|
134
|
-
|
135
|
-
# Gets a list of the states known to this callback by looking at the
|
136
|
-
# guard's known states
|
137
|
-
def known_states
|
138
|
-
guard.known_states
|
139
|
-
end
|
140
|
-
|
141
|
-
# Runs the callback as long as the transition context matches the guard
|
142
|
-
# requirements configured for this callback.
|
143
|
-
#
|
144
|
-
# If a terminator has been configured and it matches the result from the
|
145
|
-
# evaluated method, then the callback chain should be halted.
|
146
|
-
def call(object, context = {}, *args)
|
147
|
-
if @guard.matches?(object, context)
|
148
|
-
@methods.each do |method|
|
149
|
-
result = evaluate_method(object, method, *args)
|
150
|
-
throw :halt if @terminator && @terminator.call(result)
|
151
|
-
end
|
152
|
-
|
153
|
-
true
|
154
|
-
else
|
155
|
-
false
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
private
|
160
|
-
# Generates a method that can be bound to the object being transitioned
|
161
|
-
# when the callback is invoked
|
162
|
-
def bound_method(block)
|
163
|
-
arity = block.arity
|
164
|
-
|
165
|
-
if RUBY_VERSION >= '1.9'
|
166
|
-
lambda do |object, *args|
|
167
|
-
object.instance_exec(*(arity == 0 ? [] : args), &block)
|
168
|
-
end
|
169
|
-
else
|
170
|
-
# Generate a thread-safe unbound method that can be used on any object.
|
171
|
-
# This is a workaround for not having Ruby 1.9's instance_exec
|
172
|
-
unbound_method = Object.class_eval do
|
173
|
-
time = Time.now
|
174
|
-
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
175
|
-
define_method(method_name, &block)
|
176
|
-
method = instance_method(method_name)
|
177
|
-
remove_method(method_name)
|
178
|
-
method
|
179
|
-
end
|
180
|
-
|
181
|
-
# Proxy calls to the method so that the method can be bound *and*
|
182
|
-
# the arguments are adjusted
|
183
|
-
lambda do |object, *args|
|
184
|
-
unbound_method.bind(object).call(*(arity == 0 ? [] : args))
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'state_machine/eval_helpers'
|
2
|
-
|
3
|
-
module StateMachine
|
4
|
-
# Represents a type of module in which class-level methods are proxied to
|
5
|
-
# another class, injecting a custom <tt>:if</tt> condition along with method.
|
6
|
-
#
|
7
|
-
# This is used for being able to automatically include conditionals which
|
8
|
-
# check the current state in class-level methods that have configuration
|
9
|
-
# options.
|
10
|
-
#
|
11
|
-
# == Examples
|
12
|
-
#
|
13
|
-
# class Vehicle
|
14
|
-
# class << self
|
15
|
-
# attr_accessor :validations
|
16
|
-
#
|
17
|
-
# def validate(options, &block)
|
18
|
-
# validations << options
|
19
|
-
# end
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# self.validations = []
|
23
|
-
# attr_accessor :state, :simulate
|
24
|
-
#
|
25
|
-
# def moving?
|
26
|
-
# self.class.validations.all? {|validation| validation[:if].call(self)}
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# In the above class, a simple set of validation behaviors have been defined.
|
31
|
-
# Each validation consists of a configuration like so:
|
32
|
-
#
|
33
|
-
# Vehicle.validate :unless => :simulate
|
34
|
-
# Vehicle.validate :if => lambda {|vehicle| ...}
|
35
|
-
#
|
36
|
-
# In order to scope conditions, a condition proxy can be created to the
|
37
|
-
# Vehicle class. For example,
|
38
|
-
#
|
39
|
-
# proxy = StateMachine::ConditionProxy.new(Vehicle, lambda {|vehicle| vehicle.state == 'first_gear'})
|
40
|
-
# proxy.validate(:unless => :simulate)
|
41
|
-
#
|
42
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7ce491c @simulate=nil, @state=nil>
|
43
|
-
# vehicle.moving? # => false
|
44
|
-
#
|
45
|
-
# vehicle.state = 'first_gear'
|
46
|
-
# vehicle.moving? # => true
|
47
|
-
#
|
48
|
-
# vehicle.simulate = true
|
49
|
-
# vehicle.moving? # => false
|
50
|
-
class ConditionProxy < Module
|
51
|
-
include EvalHelpers
|
52
|
-
|
53
|
-
# Creates a new proxy to the given class, merging in the given condition
|
54
|
-
def initialize(klass, condition)
|
55
|
-
@klass = klass
|
56
|
-
@condition = condition
|
57
|
-
end
|
58
|
-
|
59
|
-
# Hooks in condition-merging to methods that don't exist in this module
|
60
|
-
def method_missing(*args, &block)
|
61
|
-
# Get the configuration
|
62
|
-
if args.last.is_a?(Hash)
|
63
|
-
options = args.last
|
64
|
-
else
|
65
|
-
args << options = {}
|
66
|
-
end
|
67
|
-
|
68
|
-
# Get any existing condition that may need to be merged
|
69
|
-
if_condition = options.delete(:if)
|
70
|
-
unless_condition = options.delete(:unless)
|
71
|
-
|
72
|
-
# Provide scope access to configuration in case the block is evaluated
|
73
|
-
# within the object instance
|
74
|
-
proxy = self
|
75
|
-
proxy_condition = @condition
|
76
|
-
|
77
|
-
# Replace the configuration condition with the one configured for this
|
78
|
-
# proxy, merging together any existing conditions
|
79
|
-
options[:if] = lambda do |*args|
|
80
|
-
# Block may be executed within the context of the actual object, so
|
81
|
-
# it'll either be the first argument or the executing context
|
82
|
-
object = args.first || self
|
83
|
-
|
84
|
-
proxy.evaluate_method(object, proxy_condition) &&
|
85
|
-
Array(if_condition).all? {|condition| proxy.evaluate_method(object, condition)} &&
|
86
|
-
!Array(unless_condition).any? {|condition| proxy.evaluate_method(object, condition)}
|
87
|
-
end
|
88
|
-
|
89
|
-
# Evaluate the method on the original class with the condition proxied
|
90
|
-
# through
|
91
|
-
@klass.send(*args, &block)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
module StateMachine
|
2
|
-
# Provides a set of helper methods for evaluating methods within the context
|
3
|
-
# of an object.
|
4
|
-
module EvalHelpers
|
5
|
-
# Evaluates one of several different types of methods within the context
|
6
|
-
# of the given object. Methods can be one of the following types:
|
7
|
-
# * Symbol
|
8
|
-
# * Method / Proc
|
9
|
-
# * String
|
10
|
-
#
|
11
|
-
# == Examples
|
12
|
-
#
|
13
|
-
# Below are examples of the various ways that a method can be evaluated
|
14
|
-
# on an object:
|
15
|
-
#
|
16
|
-
# class Person
|
17
|
-
# def initialize(name)
|
18
|
-
# @name = name
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# def name
|
22
|
-
# @name
|
23
|
-
# end
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# class PersonCallback
|
27
|
-
# def self.run(person)
|
28
|
-
# person.name
|
29
|
-
# end
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# person = Person.new('John Smith')
|
33
|
-
#
|
34
|
-
# evaluate_method(person, :name) # => "John Smith"
|
35
|
-
# evaluate_method(person, PersonCallback.method(:run)) # => "John Smith"
|
36
|
-
# evaluate_method(person, Proc.new {|person| person.name}) # => "John Smith"
|
37
|
-
# evaluate_method(person, lambda {|person| person.name}) # => "John Smith"
|
38
|
-
# evaluate_method(person, '@name') # => "John Smith"
|
39
|
-
#
|
40
|
-
# == Additional arguments
|
41
|
-
#
|
42
|
-
# Additional arguments can be passed to the methods being evaluated. If
|
43
|
-
# the method defines additional arguments other than the object context,
|
44
|
-
# then all arguments are required.
|
45
|
-
#
|
46
|
-
# For example,
|
47
|
-
#
|
48
|
-
# person = Person.new('John Smith')
|
49
|
-
#
|
50
|
-
# evaluate_method(person, lambda {|person| person.name}, 21) # => "John Smith"
|
51
|
-
# evaluate_method(person, lambda {|person, age| "#{person.name} is #{age}"}, 21) # => "John Smith is 21"
|
52
|
-
# evaluate_method(person, lambda {|person, age| "#{person.name} is #{age}"}, 21, 'male') # => ArgumentError: wrong number of arguments (3 for 2)
|
53
|
-
def evaluate_method(object, method, *args)
|
54
|
-
case method
|
55
|
-
when Symbol
|
56
|
-
object.method(method).arity == 0 ? object.send(method) : object.send(method, *args)
|
57
|
-
when Proc, Method
|
58
|
-
args.unshift(object)
|
59
|
-
[0, 1].include?(method.arity) ? method.call(*args.slice(0, method.arity)) : method.call(*args)
|
60
|
-
when String
|
61
|
-
eval(method, object.instance_eval {binding})
|
62
|
-
else
|
63
|
-
raise ArgumentError, 'Methods must be a symbol denoting the method to call, a block to be invoked, or a string to be evaluated'
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,252 +0,0 @@
|
|
1
|
-
require 'state_machine/transition'
|
2
|
-
require 'state_machine/guard'
|
3
|
-
require 'state_machine/assertions'
|
4
|
-
require 'state_machine/matcher_helpers'
|
5
|
-
|
6
|
-
module StateMachine
|
7
|
-
# An invalid event was specified
|
8
|
-
class InvalidEvent < StandardError
|
9
|
-
end
|
10
|
-
|
11
|
-
# An event defines an action that transitions an attribute from one state to
|
12
|
-
# another. The state that an attribute is transitioned to depends on the
|
13
|
-
# guards configured for the event.
|
14
|
-
class Event
|
15
|
-
include Assertions
|
16
|
-
include MatcherHelpers
|
17
|
-
|
18
|
-
# The state machine for which this event is defined
|
19
|
-
attr_accessor :machine
|
20
|
-
|
21
|
-
# The name of the event
|
22
|
-
attr_reader :name
|
23
|
-
|
24
|
-
# The fully-qualified name of the event, scoped by the machine's namespace
|
25
|
-
attr_reader :qualified_name
|
26
|
-
|
27
|
-
# The list of guards that determine what state this event transitions
|
28
|
-
# objects to when fired
|
29
|
-
attr_reader :guards
|
30
|
-
|
31
|
-
# A list of all of the states known to this event using the configured
|
32
|
-
# guards/transitions as the source
|
33
|
-
attr_reader :known_states
|
34
|
-
|
35
|
-
# Creates a new event within the context of the given machine
|
36
|
-
def initialize(machine, name) #:nodoc:
|
37
|
-
@machine = machine
|
38
|
-
@name = name
|
39
|
-
@qualified_name = machine.namespace ? :"#{name}_#{machine.namespace}" : name
|
40
|
-
@guards = []
|
41
|
-
@known_states = []
|
42
|
-
|
43
|
-
add_actions
|
44
|
-
end
|
45
|
-
|
46
|
-
# Creates a copy of this event in addition to the list of associated
|
47
|
-
# guards to prevent conflicts across events within a class hierarchy.
|
48
|
-
def initialize_copy(orig) #:nodoc:
|
49
|
-
super
|
50
|
-
@guards = @guards.dup
|
51
|
-
@known_states = @known_states.dup
|
52
|
-
end
|
53
|
-
|
54
|
-
# Creates a new transition that determines what to change the current state
|
55
|
-
# to when this event fires.
|
56
|
-
#
|
57
|
-
# == Defining transitions
|
58
|
-
#
|
59
|
-
# The options for a new transition uses the Hash syntax to map beginning
|
60
|
-
# states to ending states. For example,
|
61
|
-
#
|
62
|
-
# transition :parked => :idling, :idling => :first_gear
|
63
|
-
#
|
64
|
-
# In this case, when the event is fired, this transition will cause the
|
65
|
-
# state to be +idling+ if it's current state is +parked+ or +first_gear+
|
66
|
-
# if it's current state is +idling+.
|
67
|
-
#
|
68
|
-
# To help define these implicit transitions, a set of helpers are available
|
69
|
-
# for slightly more complex matching:
|
70
|
-
# * <tt>all</tt> - Matches every state in the machine
|
71
|
-
# * <tt>all - [:parked, :idling, ...]</tt> - Matches every state except those specified
|
72
|
-
# * <tt>any</tt> - An alias for +all+ (matches every state in the machine)
|
73
|
-
# * <tt>same</tt> - Matches the same state being transitioned from
|
74
|
-
#
|
75
|
-
# See StateMachine::MatcherHelpers for more information.
|
76
|
-
#
|
77
|
-
# Examples:
|
78
|
-
#
|
79
|
-
# transition all => nil # Transitions to nil regardless of the current state
|
80
|
-
# transition all => :idling # Transitions to :idling regardless of the current state
|
81
|
-
# transition all - [:idling, :first_gear] => :idling # Transitions every state but :idling and :first_gear to :idling
|
82
|
-
# transition nil => :idling # Transitions to :idling from the nil state
|
83
|
-
# transition :parked => :idling # Transitions to :idling if :parked
|
84
|
-
# transition [:parked, :stalled] => :idling # Transitions to :idling if :parked or :stalled
|
85
|
-
#
|
86
|
-
# transition :parked => same # Loops :parked back to :parked
|
87
|
-
# transition [:parked, :stalled] => same # Loops either :parked or :stalled back to the same state
|
88
|
-
# transition all - :parked => same # Loops every state but :parked back to the same state
|
89
|
-
#
|
90
|
-
# == Verbose transitions
|
91
|
-
#
|
92
|
-
# Transitions can also be defined use an explicit set of deprecated
|
93
|
-
# configuration options:
|
94
|
-
# * <tt>:from</tt> - A state or array of states that can be transitioned from.
|
95
|
-
# If not specified, then the transition can occur for *any* state.
|
96
|
-
# * <tt>:to</tt> - The state that's being transitioned to. If not specified,
|
97
|
-
# then the transition will simply loop back (i.e. the state will not change).
|
98
|
-
# * <tt>:except_from</tt> - A state or array of states that *cannot* be
|
99
|
-
# transitioned from.
|
100
|
-
#
|
101
|
-
# Examples:
|
102
|
-
#
|
103
|
-
# transition :to => nil
|
104
|
-
# transition :to => :idling
|
105
|
-
# transition :except_from => [:idling, :first_gear], :to => :idling
|
106
|
-
# transition :from => nil, :to => :idling
|
107
|
-
# transition :from => [:parked, :stalled], :to => :idling
|
108
|
-
#
|
109
|
-
# transition :from => :parked
|
110
|
-
# transition :from => [:parked, :stalled]
|
111
|
-
# transition :except_from => :parked
|
112
|
-
#
|
113
|
-
# Notice that the above examples are the verbose equivalent of the examples
|
114
|
-
# described initially.
|
115
|
-
#
|
116
|
-
# == Conditions
|
117
|
-
#
|
118
|
-
# In addition to the state requirements for each transition, a condition
|
119
|
-
# can also be defined to help determine whether that transition is
|
120
|
-
# available. These options will work on both the normal and verbose syntax.
|
121
|
-
#
|
122
|
-
# Configuration options:
|
123
|
-
# * <tt>:if</tt> - A method, proc or string to call to determine if the
|
124
|
-
# transition should occur (e.g. :if => :moving?, or :if => lambda {|vehicle| vehicle.speed > 60}).
|
125
|
-
# The condition should return or evaluate to true or false.
|
126
|
-
# * <tt>:unless</tt> - A method, proc or string to call to determine if the
|
127
|
-
# transition should not occur (e.g. :unless => :stopped?, or :unless => lambda {|vehicle| vehicle.speed <= 60}).
|
128
|
-
# The condition should return or evaluate to true or false.
|
129
|
-
#
|
130
|
-
# Examples:
|
131
|
-
#
|
132
|
-
# transition :parked => :idling, :if => :moving?
|
133
|
-
# transition :parked => :idling, :unless => :stopped?
|
134
|
-
#
|
135
|
-
# transition :from => :parked, :to => :idling, :if => :moving?
|
136
|
-
# transition :from => :parked, :to => :idling, :unless => :stopped?
|
137
|
-
#
|
138
|
-
# == Order of operations
|
139
|
-
#
|
140
|
-
# Transitions are evaluated in the order in which they're defined. As a
|
141
|
-
# result, if more than one transition applies to a given object, then the
|
142
|
-
# first transition that matches will be performed.
|
143
|
-
def transition(options)
|
144
|
-
raise ArgumentError, 'Must specify as least one transition requirement' if options.empty?
|
145
|
-
|
146
|
-
# Only a certain subset of explicit options are allowed for transition
|
147
|
-
# requirements
|
148
|
-
assert_valid_keys(options, :from, :to, :except_from, :if, :unless) if (options.keys - [:from, :to, :on, :except_from, :except_to, :except_on, :if, :unless]).empty?
|
149
|
-
|
150
|
-
guards << guard = Guard.new(options.merge(:on => name))
|
151
|
-
@known_states |= guard.known_states
|
152
|
-
guard
|
153
|
-
end
|
154
|
-
|
155
|
-
# Determines whether any transitions can be performed for this event based
|
156
|
-
# on the current state of the given object.
|
157
|
-
#
|
158
|
-
# If the event can't be fired, then this will return false, otherwise true.
|
159
|
-
def can_fire?(object)
|
160
|
-
!transition_for(object).nil?
|
161
|
-
end
|
162
|
-
|
163
|
-
# Finds and builds the next transition that can be performed on the given
|
164
|
-
# object. If no transitions can be made, then this will return nil.
|
165
|
-
def transition_for(object, requirements = {})
|
166
|
-
requirements[:from] = machine.states.match!(object).name unless custom_from_state = requirements.include?(:from)
|
167
|
-
|
168
|
-
guards.each do |guard|
|
169
|
-
if match = guard.match(object, requirements)
|
170
|
-
# Guard allows for the transition to occur
|
171
|
-
from = requirements[:from]
|
172
|
-
to = match[:to].values.empty? ? from : match[:to].values.first
|
173
|
-
|
174
|
-
return Transition.new(object, machine, name, from, to, !custom_from_state)
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
# No transition matched
|
179
|
-
nil
|
180
|
-
end
|
181
|
-
|
182
|
-
# Attempts to perform the next available transition on the given object.
|
183
|
-
# If no transitions can be made, then this will return false, otherwise
|
184
|
-
# true.
|
185
|
-
#
|
186
|
-
# Any additional arguments are passed to the StateMachine::Transition#perform
|
187
|
-
# instance method.
|
188
|
-
def fire(object, *args)
|
189
|
-
machine.reset(object)
|
190
|
-
|
191
|
-
if transition = transition_for(object)
|
192
|
-
transition.perform(*args)
|
193
|
-
else
|
194
|
-
machine.invalidate(object, :state, :invalid_transition, [[:event, name]])
|
195
|
-
false
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
# Draws a representation of this event on the given graph. This will
|
200
|
-
# create 1 or more edges on the graph for each guard (i.e. transition)
|
201
|
-
# configured.
|
202
|
-
#
|
203
|
-
# A collection of the generated edges will be returned.
|
204
|
-
def draw(graph)
|
205
|
-
valid_states = machine.states.by_priority.map {|state| state.name}
|
206
|
-
guards.collect {|guard| guard.draw(graph, name, valid_states)}.flatten
|
207
|
-
end
|
208
|
-
|
209
|
-
# Generates a nicely formatted description of this event's contents.
|
210
|
-
#
|
211
|
-
# For example,
|
212
|
-
#
|
213
|
-
# event = StateMachine::Event.new(machine, :park)
|
214
|
-
# event.transition all - :idling => :parked, :idling => same
|
215
|
-
# event # => #<StateMachine::Event name=:park transitions=[all - :idling => :parked, :idling => same]>
|
216
|
-
def inspect
|
217
|
-
transitions = guards.map do |guard|
|
218
|
-
guard.state_requirements.map do |state_requirement|
|
219
|
-
"#{state_requirement[:from].description} => #{state_requirement[:to].description}"
|
220
|
-
end * ', '
|
221
|
-
end
|
222
|
-
|
223
|
-
"#<#{self.class} name=#{name.inspect} transitions=[#{transitions * ', '}]>"
|
224
|
-
end
|
225
|
-
|
226
|
-
protected
|
227
|
-
# Add the various instance methods that can transition the object using
|
228
|
-
# the current event
|
229
|
-
def add_actions
|
230
|
-
# Checks whether the event can be fired on the current object
|
231
|
-
machine.define_instance_method("can_#{qualified_name}?") do |machine, object|
|
232
|
-
machine.event(name).can_fire?(object)
|
233
|
-
end
|
234
|
-
|
235
|
-
# Gets the next transition that would be performed if the event were
|
236
|
-
# fired now
|
237
|
-
machine.define_instance_method("#{qualified_name}_transition") do |machine, object|
|
238
|
-
machine.event(name).transition_for(object)
|
239
|
-
end
|
240
|
-
|
241
|
-
# Fires the event
|
242
|
-
machine.define_instance_method(qualified_name) do |machine, object, *args|
|
243
|
-
machine.event(name).fire(object, *args)
|
244
|
-
end
|
245
|
-
|
246
|
-
# Fires the event, raising an exception if it fails
|
247
|
-
machine.define_instance_method("#{qualified_name}!") do |machine, object, *args|
|
248
|
-
object.send(qualified_name, *args) || raise(StateMachine::InvalidTransition, "Cannot transition #{machine.name} via :#{name} from #{machine.states.match!(object).name.inspect}")
|
249
|
-
end
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|