micro-max-box 0.0.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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/aasm-6.0.0/CHANGELOG.md +497 -0
  3. data/aasm-6.0.0/LICENSE +20 -0
  4. data/aasm-6.0.0/README.md +1534 -0
  5. data/aasm-6.0.0/lib/aasm/aasm.rb +208 -0
  6. data/aasm-6.0.0/lib/aasm/base.rb +300 -0
  7. data/aasm-6.0.0/lib/aasm/configuration.rb +48 -0
  8. data/aasm-6.0.0/lib/aasm/core/event.rb +178 -0
  9. data/aasm-6.0.0/lib/aasm/core/invoker.rb +129 -0
  10. data/aasm-6.0.0/lib/aasm/core/invokers/base_invoker.rb +85 -0
  11. data/aasm-6.0.0/lib/aasm/core/invokers/class_invoker.rb +75 -0
  12. data/aasm-6.0.0/lib/aasm/core/invokers/literal_invoker.rb +90 -0
  13. data/aasm-6.0.0/lib/aasm/core/invokers/proc_invoker.rb +94 -0
  14. data/aasm-6.0.0/lib/aasm/core/state.rb +91 -0
  15. data/aasm-6.0.0/lib/aasm/core/transition.rb +83 -0
  16. data/aasm-6.0.0/lib/aasm/dsl_helper.rb +32 -0
  17. data/aasm-6.0.0/lib/aasm/errors.rb +22 -0
  18. data/aasm-6.0.0/lib/aasm/instance_base.rb +153 -0
  19. data/aasm-6.0.0/lib/aasm/localizer.rb +64 -0
  20. data/aasm-6.0.0/lib/aasm/minitest/allow_event.rb +13 -0
  21. data/aasm-6.0.0/lib/aasm/minitest/allow_transition_to.rb +13 -0
  22. data/aasm-6.0.0/lib/aasm/minitest/have_state.rb +13 -0
  23. data/aasm-6.0.0/lib/aasm/minitest/transition_from.rb +21 -0
  24. data/aasm-6.0.0/lib/aasm/minitest.rb +5 -0
  25. data/aasm-6.0.0/lib/aasm/minitest_spec.rb +15 -0
  26. data/aasm-6.0.0/lib/aasm/persistence/active_record_persistence.rb +174 -0
  27. data/aasm-6.0.0/lib/aasm/persistence/base.rb +89 -0
  28. data/aasm-6.0.0/lib/aasm/persistence/core_data_query_persistence.rb +94 -0
  29. data/aasm-6.0.0/lib/aasm/persistence/dynamoid_persistence.rb +92 -0
  30. data/aasm-6.0.0/lib/aasm/persistence/mongoid_persistence.rb +126 -0
  31. data/aasm-6.0.0/lib/aasm/persistence/no_brainer_persistence.rb +105 -0
  32. data/aasm-6.0.0/lib/aasm/persistence/orm.rb +154 -0
  33. data/aasm-6.0.0/lib/aasm/persistence/plain_persistence.rb +26 -0
  34. data/aasm-6.0.0/lib/aasm/persistence/redis_persistence.rb +112 -0
  35. data/aasm-6.0.0/lib/aasm/persistence/sequel_persistence.rb +83 -0
  36. data/aasm-6.0.0/lib/aasm/persistence.rb +54 -0
  37. data/aasm-6.0.0/lib/aasm/rspec/allow_event.rb +26 -0
  38. data/aasm-6.0.0/lib/aasm/rspec/allow_transition_to.rb +26 -0
  39. data/aasm-6.0.0/lib/aasm/rspec/have_state.rb +22 -0
  40. data/aasm-6.0.0/lib/aasm/rspec/transition_from.rb +36 -0
  41. data/aasm-6.0.0/lib/aasm/rspec.rb +5 -0
  42. data/aasm-6.0.0/lib/aasm/state_machine.rb +53 -0
  43. data/aasm-6.0.0/lib/aasm/state_machine_store.rb +77 -0
  44. data/aasm-6.0.0/lib/aasm/version.rb +3 -0
  45. data/aasm-6.0.0/lib/aasm.rb +21 -0
  46. data/aasm-6.0.0/lib/generators/aasm/aasm_generator.rb +16 -0
  47. data/aasm-6.0.0/lib/generators/aasm/orm_helpers.rb +41 -0
  48. data/aasm-6.0.0/lib/generators/active_record/aasm_generator.rb +40 -0
  49. data/aasm-6.0.0/lib/generators/active_record/templates/migration.rb +8 -0
  50. data/aasm-6.0.0/lib/generators/active_record/templates/migration_existing.rb +5 -0
  51. data/aasm-6.0.0/lib/generators/mongoid/aasm_generator.rb +28 -0
  52. data/aasm-6.0.0/lib/generators/nobrainer/aasm_generator.rb +28 -0
  53. data/aasm-6.0.0/lib/motion-aasm.rb +37 -0
  54. data/micro-max-box.gemspec +12 -0
  55. metadata +94 -0
@@ -0,0 +1,154 @@
1
+ module AASM
2
+ module Persistence
3
+ # This module adds transactional support for any database that supports it.
4
+ # This includes rollback capability and rollback/commit callbacks.
5
+ module ORM
6
+
7
+ # Writes <tt>state</tt> to the state column and persists it to the database
8
+ #
9
+ # foo = Foo.find(1)
10
+ # foo.aasm.current_state # => :opened
11
+ # foo.close!
12
+ # foo.aasm.current_state # => :closed
13
+ # Foo.find(1).aasm.current_state # => :closed
14
+ #
15
+ # NOTE: intended to be called from an event
16
+ def aasm_write_state(state, name=:default)
17
+ attribute_name = self.class.aasm(name).attribute_name
18
+ old_value = aasm_read_attribute(attribute_name)
19
+ aasm_write_state_attribute state, name
20
+
21
+ success = if aasm_skipping_validations(name)
22
+ aasm_update_column(attribute_name, aasm_raw_attribute_value(state, name))
23
+ else
24
+ aasm_save
25
+ end
26
+
27
+ unless success
28
+ aasm_rollback(name, old_value)
29
+ aasm_raise_invalid_record if aasm_whiny_persistence(name)
30
+ end
31
+
32
+ success
33
+ end
34
+
35
+ # Writes <tt>state</tt> to the state field, but does not persist it to the database
36
+ #
37
+ # foo = Foo.find(1)
38
+ # foo.aasm.current_state # => :opened
39
+ # foo.close
40
+ # foo.aasm.current_state # => :closed
41
+ # Foo.find(1).aasm.current_state # => :opened
42
+ # foo.save
43
+ # foo.aasm.current_state # => :closed
44
+ # Foo.find(1).aasm.current_state # => :closed
45
+ #
46
+ # NOTE: intended to be called from an event
47
+ def aasm_write_state_without_persistence(state, name=:default)
48
+ aasm_write_state_attribute(state, name)
49
+ end
50
+
51
+ private
52
+
53
+ # Save the record and return true if it succeeded/false otherwise.
54
+ def aasm_save
55
+ raise("Define #aasm_save_without_error in the AASM Persistence class.")
56
+ end
57
+
58
+ def aasm_raise_invalid_record
59
+ raise("Define #aasm_raise_invalid_record in the AASM Persistence class.")
60
+ end
61
+
62
+ # Update only the column without running validations.
63
+ def aasm_update_column(attribute_name, value)
64
+ raise("Define #aasm_update_column in the AASM Persistence class.")
65
+ end
66
+
67
+ def aasm_read_attribute(name)
68
+ raise("Define #aasm_read_attribute the AASM Persistence class.")
69
+ end
70
+
71
+ def aasm_write_attribute(name, value)
72
+ raise("Define #aasm_write_attribute in the AASM Persistence class.")
73
+ end
74
+
75
+ # Returns true or false if transaction completed successfully.
76
+ def aasm_transaction(requires_new, requires_lock)
77
+ raise("Define #aasm_transaction the AASM Persistence class.")
78
+ end
79
+
80
+ def aasm_supports_transactions?
81
+ true
82
+ end
83
+
84
+ def aasm_execute_after_commit
85
+ yield
86
+ end
87
+
88
+ def aasm_write_state_attribute(state, name=:default)
89
+ aasm_write_attribute(self.class.aasm(name).attribute_name, aasm_raw_attribute_value(state, name))
90
+ end
91
+
92
+ def aasm_raw_attribute_value(state, _name=:default)
93
+ state.to_s
94
+ end
95
+
96
+ def aasm_rollback(name, old_value)
97
+ aasm_write_attribute(self.class.aasm(name).attribute_name, old_value)
98
+ false
99
+ end
100
+
101
+ def aasm_whiny_persistence(state_machine_name)
102
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.whiny_persistence
103
+ end
104
+
105
+ def aasm_skipping_validations(state_machine_name)
106
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.skip_validation_on_save
107
+ end
108
+
109
+ def use_transactions?(state_machine_name)
110
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.use_transactions
111
+ end
112
+
113
+ def requires_new?(state_machine_name)
114
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.requires_new_transaction
115
+ end
116
+
117
+ def requires_lock?(state_machine_name)
118
+ AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.requires_lock
119
+ end
120
+
121
+ # Returns true if event was fired successfully and transaction completed.
122
+ def aasm_fire_event(state_machine_name, name, options, *args, &block)
123
+ return super unless aasm_supports_transactions? && options[:persist]
124
+
125
+ event = self.class.aasm(state_machine_name).state_machine.events[name]
126
+ event.fire_callbacks(:before_transaction, self, *args)
127
+ event.fire_global_callbacks(:before_all_transactions, self, *args)
128
+
129
+ begin
130
+ success = if options[:persist] && use_transactions?(state_machine_name)
131
+ aasm_transaction(requires_new?(state_machine_name), requires_lock?(state_machine_name)) do
132
+ super
133
+ end
134
+ else
135
+ super
136
+ end
137
+
138
+ if success && !(event.options.keys & [:after_commit, :after_all_commits]).empty?
139
+ aasm_execute_after_commit do
140
+ event.fire_callbacks(:after_commit, self, *args)
141
+ event.fire_global_callbacks(:after_all_commits, self, *args)
142
+ end
143
+ end
144
+
145
+ success
146
+ ensure
147
+ event.fire_callbacks(:after_transaction, self, *args)
148
+ event.fire_global_callbacks(:after_all_transactions, self, *args)
149
+ end
150
+ end
151
+
152
+ end # Transactional
153
+ end # Persistence
154
+ end # AASM
@@ -0,0 +1,26 @@
1
+ module AASM
2
+ module Persistence
3
+ module PlainPersistence
4
+
5
+ # may be overwritten by persistence mixins
6
+ def aasm_read_state(name=:default)
7
+ # all the following lines behave like @current_state ||= aasm(name).enter_initial_state
8
+ current = aasm(name).instance_variable_defined?("@current_state_#{name}") &&
9
+ aasm(name).instance_variable_get("@current_state_#{name}")
10
+ return current if current
11
+ aasm(name).instance_variable_set("@current_state_#{name}", aasm(name).enter_initial_state)
12
+ end
13
+
14
+ # may be overwritten by persistence mixins
15
+ def aasm_write_state(new_state, name=:default)
16
+ true
17
+ end
18
+
19
+ # may be overwritten by persistence mixins
20
+ def aasm_write_state_without_persistence(new_state, name=:default)
21
+ aasm(name).instance_variable_set("@current_state_#{name}", new_state)
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,112 @@
1
+ module AASM
2
+ module Persistence
3
+ module RedisPersistence
4
+
5
+ def self.included(base)
6
+ base.send(:include, AASM::Persistence::Base)
7
+ base.send(:include, AASM::Persistence::RedisPersistence::InstanceMethods)
8
+ end
9
+
10
+ module InstanceMethods
11
+ # Initialize with default values
12
+ #
13
+ # Redis::Objects removes the key from Redis when set to `nil`
14
+ def initialize(*args)
15
+ super
16
+ aasm_ensure_initial_state
17
+ end
18
+ # Returns the value of the aasm.attribute_name - called from <tt>aasm.current_state</tt>
19
+ #
20
+ # If it's a new record, and the aasm state column is blank it returns the initial state
21
+ #
22
+ # class Foo
23
+ # include Redis::Objects
24
+ # include AASM
25
+ # aasm :column => :status do
26
+ # state :opened
27
+ # state :closed
28
+ # end
29
+ # end
30
+ #
31
+ # foo = Foo.new
32
+ # foo.current_state # => :opened
33
+ # foo.close
34
+ # foo.current_state # => :closed
35
+ #
36
+ # foo = Foo[1]
37
+ # foo.current_state # => :opened
38
+ # foo.aasm_state = nil
39
+ # foo.current_state # => nil
40
+ #
41
+ # NOTE: intended to be called from an event
42
+ #
43
+ # This allows for nil aasm states - be sure to add validation to your model
44
+ def aasm_read_state(name=:default)
45
+ state = send(self.class.aasm(name).attribute_name)
46
+
47
+ if state.value.nil?
48
+ nil
49
+ else
50
+ state.value.to_sym
51
+ end
52
+ end
53
+
54
+ # Ensures that if the aasm_state column is nil and the record is new
55
+ # that the initial state gets populated before validation on create
56
+ #
57
+ # foo = Foo.new
58
+ # foo.aasm_state # => nil
59
+ # foo.valid?
60
+ # foo.aasm_state # => "open" (where :open is the initial state)
61
+ #
62
+ #
63
+ # foo = Foo.find(:first)
64
+ # foo.aasm_state # => 1
65
+ # foo.aasm_state = nil
66
+ # foo.valid?
67
+ # foo.aasm_state # => nil
68
+ #
69
+ def aasm_ensure_initial_state
70
+ AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |name|
71
+ aasm_column = self.class.aasm(name).attribute_name
72
+ aasm(name).enter_initial_state if !send(aasm_column).value || send(aasm_column).value.empty?
73
+ end
74
+ end
75
+
76
+ # Writes <tt>state</tt> to the state column and persists it to the database
77
+ #
78
+ # foo = Foo[1]
79
+ # foo.aasm.current_state # => :opened
80
+ # foo.close!
81
+ # foo.aasm.current_state # => :closed
82
+ # Foo[1].aasm.current_state # => :closed
83
+ #
84
+ # NOTE: intended to be called from an event
85
+ def aasm_write_state(state, name=:default)
86
+ aasm_column = self.class.aasm(name).attribute_name
87
+ send("#{aasm_column}").value = state
88
+ end
89
+
90
+ # Writes <tt>state</tt> to the state column, but does not persist it to the database
91
+ # (but actually it still does)
92
+ #
93
+ # With Redis::Objects it's not possible to skip persisting - it's not an ORM,
94
+ # it does not operate like an AR model and does not know how to postpone changes.
95
+ #
96
+ # foo = Foo[1]
97
+ # foo.aasm.current_state # => :opened
98
+ # foo.close
99
+ # foo.aasm.current_state # => :closed
100
+ # Foo[1].aasm.current_state # => :opened
101
+ # foo.save
102
+ # foo.aasm.current_state # => :closed
103
+ # Foo[1].aasm.current_state # => :closed
104
+ #
105
+ # NOTE: intended to be called from an event
106
+ def aasm_write_state_without_persistence(state, name=:default)
107
+ aasm_write_state(state, name)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,83 @@
1
+ require 'aasm/persistence/orm'
2
+ module AASM
3
+ module Persistence
4
+ module SequelPersistence
5
+ def self.included(base)
6
+ base.send(:include, AASM::Persistence::Base)
7
+ base.send(:include, AASM::Persistence::ORM)
8
+ base.send(:include, AASM::Persistence::SequelPersistence::InstanceMethods)
9
+ end
10
+
11
+ module InstanceMethods
12
+ def before_validation
13
+ aasm_ensure_initial_state
14
+ super
15
+ end
16
+
17
+ def before_create
18
+ super
19
+ end
20
+
21
+ def aasm_raise_invalid_record
22
+ raise Sequel::ValidationFailed.new(self)
23
+ end
24
+
25
+ def aasm_new_record?
26
+ new?
27
+ end
28
+
29
+ # Returns nil if fails silently
30
+ # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-save
31
+ def aasm_save
32
+ !save(raise_on_failure: false).nil?
33
+ end
34
+
35
+ def aasm_read_attribute(name)
36
+ send(name)
37
+ end
38
+
39
+ def aasm_write_attribute(name, value)
40
+ send("#{name}=", value)
41
+ end
42
+
43
+ def aasm_transaction(requires_new, requires_lock)
44
+ self.class.db.transaction(savepoint: requires_new) do
45
+ if requires_lock
46
+ # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-lock-21
47
+ requires_lock.is_a?(String) ? lock!(requires_lock) : lock!
48
+ end
49
+ yield
50
+ end
51
+ end
52
+
53
+ def aasm_update_column(attribute_name, value)
54
+ this.update(attribute_name => value)
55
+ end
56
+
57
+ # Ensures that if the aasm_state column is nil and the record is new
58
+ # that the initial state gets populated before validation on create
59
+ #
60
+ # foo = Foo.new
61
+ # foo.aasm_state # => nil
62
+ # foo.valid?
63
+ # foo.aasm_state # => "open" (where :open is the initial state)
64
+ #
65
+ #
66
+ # foo = Foo.find(:first)
67
+ # foo.aasm_state # => 1
68
+ # foo.aasm_state = nil
69
+ # foo.valid?
70
+ # foo.aasm_state # => nil
71
+ #
72
+ def aasm_ensure_initial_state
73
+ AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
74
+ aasm(state_machine_name).enter_initial_state if
75
+ (new? || values.key?(self.class.aasm(state_machine_name).attribute_name)) &&
76
+ send(self.class.aasm(state_machine_name).attribute_name).to_s.strip.empty?
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,54 @@
1
+ module AASM
2
+ module Persistence
3
+ class << self
4
+
5
+ def load_persistence(base)
6
+ # Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
7
+ hierarchy = base.ancestors.map {|klass| klass.to_s}
8
+
9
+ if hierarchy.include?("ActiveRecord::Base")
10
+ require_persistence :active_record
11
+ include_persistence base, :active_record
12
+ elsif hierarchy.include?("Mongoid::Document")
13
+ require_persistence :mongoid
14
+ include_persistence base, :mongoid
15
+ elsif hierarchy.include?("NoBrainer::Document")
16
+ require_persistence :no_brainer
17
+ include_persistence base, :no_brainer
18
+ elsif hierarchy.include?("Sequel::Model")
19
+ require_persistence :sequel
20
+ include_persistence base, :sequel
21
+ elsif hierarchy.include?("Dynamoid::Document")
22
+ require_persistence :dynamoid
23
+ include_persistence base, :dynamoid
24
+ elsif hierarchy.include?("Redis::Objects")
25
+ require_persistence :redis
26
+ include_persistence base, :redis
27
+ elsif hierarchy.include?("CDQManagedObject")
28
+ include_persistence base, :core_data_query
29
+ else
30
+ include_persistence base, :plain
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def require_persistence(type)
37
+ require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence")
38
+ end
39
+
40
+ def include_persistence(base, type)
41
+ base.send(:include, constantize("#{capitalize(type)}Persistence"))
42
+ end
43
+
44
+ def capitalize(string_or_symbol)
45
+ string_or_symbol.to_s.split('_').map {|segment| segment[0].upcase + segment[1..-1]}.join('')
46
+ end
47
+
48
+ def constantize(string)
49
+ AASM::Persistence.const_get(string)
50
+ end
51
+
52
+ end # class << self
53
+ end
54
+ end # AASM
@@ -0,0 +1,26 @@
1
+ RSpec::Matchers.define :allow_event do |event|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).may_fire_event?(event, *@args)
5
+ end
6
+
7
+ chain :on do |state_machine_name|
8
+ @state_machine_name = state_machine_name
9
+ end
10
+
11
+ chain :with do |*args|
12
+ @args = args
13
+ end
14
+
15
+ description do
16
+ "allow event #{expected} (on :#{@state_machine_name})"
17
+ end
18
+
19
+ failure_message do |obj|
20
+ "expected that the event :#{expected} would be allowed (on :#{@state_machine_name})"
21
+ end
22
+
23
+ failure_message_when_negated do |obj|
24
+ "expected that the event :#{expected} would not be allowed (on :#{@state_machine_name})"
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ RSpec::Matchers.define :allow_transition_to do |state|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).states({:permitted => true}, *@args).include?(state)
5
+ end
6
+
7
+ chain :on do |state_machine_name|
8
+ @state_machine_name = state_machine_name
9
+ end
10
+
11
+ chain :with do |*args|
12
+ @args = args
13
+ end
14
+
15
+ description do
16
+ "allow transition to #{expected} (on :#{@state_machine_name})"
17
+ end
18
+
19
+ failure_message do |obj|
20
+ "expected that the state :#{expected} would be reachable (on :#{@state_machine_name})"
21
+ end
22
+
23
+ failure_message_when_negated do |obj|
24
+ "expected that the state :#{expected} would not be reachable (on :#{@state_machine_name})"
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ RSpec::Matchers.define :have_state do |state|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).current_state == state.to_sym
5
+ end
6
+
7
+ chain :on do |state_machine_name|
8
+ @state_machine_name = state_machine_name
9
+ end
10
+
11
+ description do
12
+ "have state #{expected} (on :#{@state_machine_name})"
13
+ end
14
+
15
+ failure_message do |obj|
16
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{expected} (on :#{@state_machine_name})"
17
+ end
18
+
19
+ failure_message_when_negated do |obj|
20
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{expected} (on :#{@state_machine_name})"
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ RSpec::Matchers.define :transition_from do |from_state|
2
+ match do |obj|
3
+ @state_machine_name ||= :default
4
+ obj.aasm(@state_machine_name).current_state = from_state.to_sym
5
+ begin
6
+ obj.send(@event, *@args) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
7
+ rescue AASM::InvalidTransition
8
+ false
9
+ end
10
+ end
11
+
12
+ chain :on do |state_machine_name|
13
+ @state_machine_name = state_machine_name
14
+ end
15
+
16
+ chain :to do |state|
17
+ @to_state = state
18
+ end
19
+
20
+ chain :on_event do |event, *args|
21
+ @event = event
22
+ @args = args
23
+ end
24
+
25
+ description do
26
+ "transition state to :#{@to_state} from :#{expected} on event :#{@event}, with params: #{@args} (on :#{@state_machine_name})"
27
+ end
28
+
29
+ failure_message do |obj|
30
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{@to_state} (on :#{@state_machine_name})"
31
+ end
32
+
33
+ failure_message_when_negated do |obj|
34
+ "expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{@to_state} (on :#{@state_machine_name})"
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # relative-require all rspec files
2
+ Dir[File.dirname(__FILE__) + '/rspec/*.rb'].each do |file|
3
+ require 'aasm/rspec/' + File.basename(file, File.extname(file))
4
+ end
5
+
@@ -0,0 +1,53 @@
1
+ module AASM
2
+ class StateMachine
3
+ # the following four methods provide the storage of all state machines
4
+
5
+ attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
6
+
7
+ def initialize(name)
8
+ @initial_state = nil
9
+ @states = []
10
+ @events = {}
11
+ @global_callbacks = {}
12
+ @config = AASM::Configuration.new
13
+ @name = name
14
+ end
15
+
16
+ # called internally by Ruby 1.9 after clone()
17
+ def initialize_copy(orig)
18
+ super
19
+ @states = orig.states.collect { |state| state.clone }
20
+ @events = {}
21
+ orig.events.each_pair { |name, event| @events[name] = event.clone }
22
+ @global_callbacks = @global_callbacks.dup
23
+ end
24
+
25
+ def add_state(state_name, klass, options)
26
+ set_initial_state(state_name, options)
27
+
28
+ # allow reloading, extending or redefining a state
29
+ @states.delete(state_name) if @states.include?(state_name)
30
+
31
+ @states << AASM::Core::State.new(state_name, klass, self, options)
32
+ end
33
+
34
+ def add_event(name, options, &block)
35
+ @events[name] = AASM::Core::Event.new(name, self, options, &block)
36
+ end
37
+
38
+ def add_global_callbacks(name, *callbacks, &block)
39
+ @global_callbacks[name] ||= []
40
+ callbacks.each do |callback|
41
+ @global_callbacks[name] << callback unless @global_callbacks[name].include? callback
42
+ end
43
+ @global_callbacks[name] << block if block
44
+ end
45
+
46
+ private
47
+
48
+ def set_initial_state(name, options)
49
+ @initial_state = name if options[:initial] || !initial_state
50
+ end
51
+
52
+ end # StateMachine
53
+ end # AASM
@@ -0,0 +1,77 @@
1
+ require 'concurrent/map'
2
+
3
+ module AASM
4
+ class StateMachineStore
5
+ @stores = Concurrent::Map.new
6
+
7
+ class << self
8
+ def stores
9
+ @stores
10
+ end
11
+
12
+ # do not overwrite existing state machines, which could have been created by
13
+ # inheritance, see AASM::ClassMethods method inherited
14
+ def register(klass, overwrite = false, state_machine = nil)
15
+ raise "Cannot register #{klass}" unless klass.is_a?(Class)
16
+
17
+ case name = template = overwrite
18
+ when FalseClass then stores[klass.to_s] ||= new
19
+ when TrueClass then stores[klass.to_s] = new
20
+ when Class then stores[klass.to_s] = stores[template.to_s].clone
21
+ when Symbol then stores[klass.to_s].register(name, state_machine)
22
+ when String then stores[klass.to_s].register(name, state_machine)
23
+ else raise "Don't know what to do with #{overwrite}"
24
+ end
25
+ end
26
+ alias_method :[]=, :register
27
+
28
+ def fetch(klass, fallback = nil)
29
+ stores[klass.to_s] || fallback && begin
30
+ match = klass.ancestors.find do |ancestor|
31
+ ancestor.include? AASM and stores[ancestor.to_s]
32
+ end
33
+
34
+ stores[match.to_s]
35
+ end
36
+ end
37
+ alias_method :[], :fetch
38
+
39
+ def unregister(klass)
40
+ stores.delete(klass.to_s)
41
+ end
42
+ end
43
+
44
+ def initialize
45
+ @machines = Concurrent::Map.new
46
+ end
47
+
48
+ def clone
49
+ StateMachineStore.new.tap do |store|
50
+ @machines.each_pair do |name, machine|
51
+ store.register(name, machine.clone)
52
+ end
53
+ end
54
+ end
55
+
56
+ def machine(name)
57
+ @machines[name.to_s]
58
+ end
59
+ alias_method :[], :machine
60
+
61
+ def machine_names
62
+ @machines.keys
63
+ end
64
+ alias_method :keys, :machine_names
65
+
66
+ def register(name, machine, force = false)
67
+ raise "Cannot use #{name.inspect} for machine name" unless name.is_a?(Symbol) or name.is_a?(String)
68
+ raise "Cannot use #{machine.inspect} as a machine" unless machine.is_a?(AASM::StateMachine)
69
+
70
+ if force
71
+ @machines[name.to_s] = machine
72
+ else
73
+ @machines[name.to_s] ||= machine
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module AASM
2
+ VERSION = "6.0.0"
3
+ end