state_machinable 0.0.4 → 0.0.5
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/lib/state_machinable/base.rb +61 -0
- data/lib/state_machinable/event_not_handled_exception.rb +7 -0
- data/lib/state_machinable/model.rb +40 -0
- data/lib/state_machinable/version.rb +1 -1
- data/lib/state_machinable.rb +4 -40
- data/state_machinable.gemspec +0 -1
- metadata +4 -2
- data/lib/base_state_machine.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 478c68fcd3ae95aefb882ef5d4a1aa36721e92ed
|
4
|
+
data.tar.gz: 8e125f87148afe885013797496b7763fa362357f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19132527321ed15b2a05e1d07186a592984e27e5da851a224566fff9a7d94473354476c706dde814ad779dfe81a97c1f97547231b0601fae2c9e6577d5fec7d7
|
7
|
+
data.tar.gz: e53ceb952d1feb6ac3dc88c46d95e94104fa8ee9897a539dd82042cd85775c47ffc816cc2786115dc62383ec6a6f655043f4cd8cc2d9f40b80fdad5a10f99fd2
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module StateMachinable
|
2
|
+
module Base
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def state_class(state)
|
7
|
+
"#{self}::#{state.classify}".constantize
|
8
|
+
rescue NameError
|
9
|
+
nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
included do
|
14
|
+
include Statesman::Machine
|
15
|
+
|
16
|
+
state :initial, :initial => true
|
17
|
+
|
18
|
+
before_transition do |obj, _transition|
|
19
|
+
state_class = obj.state_machine.class.state_class(obj.state_machine.current_state)
|
20
|
+
if state_class.present? && state_class.respond_to?(:exit)
|
21
|
+
state_class.exit(obj)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
after_transition do |obj, transition|
|
26
|
+
obj.update_column(:current_state, transition.to_state)
|
27
|
+
|
28
|
+
state_class = obj.state_machine.class.state_class(transition.to_state)
|
29
|
+
if state_class.present? && state_class.respond_to?(:enter)
|
30
|
+
state_class.enter(obj)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(name, *args, &block)
|
35
|
+
begin
|
36
|
+
events = "#{self.class}::EVENTS".constantize
|
37
|
+
rescue NameError
|
38
|
+
events = []
|
39
|
+
end
|
40
|
+
|
41
|
+
events.concat([:ev_before_save, :ev_after_save]).uniq
|
42
|
+
clean_name = name.to_s.chomp('!').to_sym
|
43
|
+
|
44
|
+
if events.include?(clean_name)
|
45
|
+
state_class = self.class.state_class(self.current_state)
|
46
|
+
if state_class.present? && state_class.respond_to?(clean_name)
|
47
|
+
state_class.send(clean_name, self.object, *args)
|
48
|
+
else
|
49
|
+
if name.to_s.last == '!'
|
50
|
+
raise StateMachinable::EventNotHandledException.new(:event => clean_name, :state => self.current_state)
|
51
|
+
else
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
else
|
56
|
+
super
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module StateMachinable
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def state_machine_class
|
7
|
+
"#{self}StateMachine".constantize
|
8
|
+
end
|
9
|
+
|
10
|
+
def transition_class
|
11
|
+
"#{self}Transition".constantize
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
included do
|
16
|
+
before_save :send_ev_before_save, :if => Proc.new { |obj| obj.changed? }
|
17
|
+
after_save :send_ev_after_save, :if => Proc.new { |obj| !obj.id_changed? && obj.changed? }
|
18
|
+
after_save :transition_to_initial_state, :if => Proc.new { |obj| obj.id_changed? }
|
19
|
+
delegate :can_transition_to?, :transition_to!, :transition_to, :to => :state_machine
|
20
|
+
|
21
|
+
def state_machine
|
22
|
+
@state_machine ||= self.class.state_machine_class.new(self, :transition_class => self.class.transition_class)
|
23
|
+
end
|
24
|
+
|
25
|
+
def send_ev_before_save
|
26
|
+
self.state_machine.send('ev_before_save')
|
27
|
+
end
|
28
|
+
|
29
|
+
def send_ev_after_save
|
30
|
+
self.state_machine.send('ev_after_save')
|
31
|
+
end
|
32
|
+
|
33
|
+
private def transition_to_initial_state
|
34
|
+
if !self.respond_to?(:skip_state_machine?) || !self.skip_state_machine?
|
35
|
+
self.transition_to!(self.state_machine.class.successors['initial'].first)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/state_machinable.rb
CHANGED
@@ -1,41 +1,5 @@
|
|
1
|
-
require "state_machinable/version"
|
2
1
|
require 'active_support'
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
class_methods do
|
8
|
-
def state_machine_class
|
9
|
-
"#{self}StateMachine".constantize
|
10
|
-
end
|
11
|
-
|
12
|
-
def transition_class
|
13
|
-
"#{self}Transition".constantize
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
included do
|
18
|
-
before_save :send_ev_before_save, :if => Proc.new { |obj| obj.changed? }
|
19
|
-
after_save :send_ev_after_save, :if => Proc.new { |obj| !obj.id_changed? && obj.changed? }
|
20
|
-
after_save :transition_to_initial_state, :if => Proc.new { |obj| obj.id_changed? }
|
21
|
-
delegate :can_transition_to?, :transition_to!, :transition_to, :to => :state_machine
|
22
|
-
|
23
|
-
def state_machine
|
24
|
-
@state_machine ||= self.class.state_machine_class.new(self, :transition_class => self.class.transition_class)
|
25
|
-
end
|
26
|
-
|
27
|
-
def send_ev_before_save
|
28
|
-
self.state_machine.send('ev_before_save')
|
29
|
-
end
|
30
|
-
|
31
|
-
def send_ev_after_save
|
32
|
-
self.state_machine.send('ev_after_save')
|
33
|
-
end
|
34
|
-
|
35
|
-
private def transition_to_initial_state
|
36
|
-
if !self.respond_to?(:skip_state_machine?) || !self.skip_state_machine?
|
37
|
-
self.transition_to!(self.state_machine.class.successors['initial'].first)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
2
|
+
require "state_machinable/event_not_handled_exception"
|
3
|
+
require "state_machinable/version"
|
4
|
+
require "state_machinable/base"
|
5
|
+
require "state_machinable/model"
|
data/state_machinable.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_machinable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santiago Herrera
|
@@ -103,8 +103,10 @@ files:
|
|
103
103
|
- Rakefile
|
104
104
|
- bin/console
|
105
105
|
- bin/setup
|
106
|
-
- lib/base_state_machine.rb
|
107
106
|
- lib/state_machinable.rb
|
107
|
+
- lib/state_machinable/base.rb
|
108
|
+
- lib/state_machinable/event_not_handled_exception.rb
|
109
|
+
- lib/state_machinable/model.rb
|
108
110
|
- lib/state_machinable/version.rb
|
109
111
|
- state_machinable.gemspec
|
110
112
|
homepage: https://github.com/bodyshopbidsdotcom/state_machinable
|
data/lib/base_state_machine.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
|
3
|
-
module BaseStateMachine
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
class_methods do
|
7
|
-
def state_class(state)
|
8
|
-
"#{self}::#{state.classify}".constantize
|
9
|
-
rescue NameError
|
10
|
-
nil
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
included do
|
15
|
-
include Statesman::Machine
|
16
|
-
|
17
|
-
state :initial, :initial => true
|
18
|
-
|
19
|
-
before_transition do |obj, _transition|
|
20
|
-
state_class = obj.state_machine.class.state_class(obj.state_machine.current_state)
|
21
|
-
if state_class.present? && state_class.respond_to?(:exit)
|
22
|
-
state_class.exit(obj)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
after_transition do |obj, transition|
|
27
|
-
obj.update_column(:current_state, transition.to_state)
|
28
|
-
|
29
|
-
state_class = obj.state_machine.class.state_class(transition.to_state)
|
30
|
-
if state_class.present? && state_class.respond_to?(:enter)
|
31
|
-
state_class.enter(obj)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def method_missing(name, *args, &block)
|
36
|
-
begin
|
37
|
-
events = "#{self.class}::EVENTS".constantize
|
38
|
-
rescue NameError
|
39
|
-
events = []
|
40
|
-
end
|
41
|
-
|
42
|
-
events.concat([:ev_before_save, :ev_after_save]).uniq
|
43
|
-
clean_name = name.to_s.chomp('!').to_sym
|
44
|
-
|
45
|
-
if events.include?(clean_name)
|
46
|
-
state_class = self.class.state_class(self.current_state)
|
47
|
-
if state_class.present? && state_class.respond_to?(clean_name)
|
48
|
-
state_class.send(clean_name, self.object, *args)
|
49
|
-
else
|
50
|
-
if name.to_s.last == '!'
|
51
|
-
raise EventNotHandledException.new(:event => clean_name, :state => self.current_state)
|
52
|
-
else
|
53
|
-
nil
|
54
|
-
end
|
55
|
-
end
|
56
|
-
else
|
57
|
-
super
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|