switcher 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/LICENSE +8 -0
- data/README.md +14 -1
- data/lib/switcher/adapters/active_record.rb +2 -49
- data/lib/switcher/adapters/object.rb +5 -44
- data/lib/switcher/facade.rb +2 -1
- data/lib/switcher/spec.rb +3 -3
- data/lib/switcher/statement.rb +23 -20
- data/lib/switcher/version.rb +1 -1
- data/lib/switcher.rb +35 -1
- metadata +7 -6
data/Gemfile
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright (c) 2012, Andrey Savchenko
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
* Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.
|
7
|
+
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -116,4 +116,17 @@ user.save
|
|
116
116
|
|
117
117
|
* ActiveModel
|
118
118
|
* Virtus
|
119
|
-
* Mongoid
|
119
|
+
* Mongoid
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
The modified MIT license
|
124
|
+
|
125
|
+
Copyright (c) 2012 Andrey Savchenko
|
126
|
+
|
127
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
128
|
+
|
129
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
130
|
+
* Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.
|
131
|
+
|
132
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,53 +1,11 @@
|
|
1
1
|
module Switcher
|
2
2
|
module ActiveRecord
|
3
|
-
|
4
|
-
module Initializer
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def switcher_pre_initialize(inst, spec)
|
9
|
-
inst.class_eval do
|
10
|
-
spec_name = spec.name
|
11
|
-
|
12
|
-
define_method(:"#{spec_name}_spec") { spec }
|
13
|
-
define_method(:"#{spec_name}_prev") { self.instance_variable_get(:"@#{spec_name}_statement").state_prev }
|
14
|
-
|
15
|
-
define_method(:"#{spec_name}") { self.instance_variable_get(:"@#{spec_name}_statement").state_current }
|
16
|
-
define_method(:"#{spec_name}=") { nil } # FIXME - raise exception
|
17
|
-
|
18
|
-
define_method(:"#{spec_name}_force") { |state| self.instance_variable_get(:"@#{spec_name}_statement").force_state(state.to_sym) }
|
19
|
-
|
20
|
-
events = []
|
21
|
-
|
22
|
-
spec.states.each_pair do |state_name, state|
|
23
|
-
define_method(:"#{spec_name}_#{state_name}?") do
|
24
|
-
state_name == self.send(:"#{spec_name}")
|
25
|
-
end
|
26
|
-
|
27
|
-
events << state.event_names
|
28
|
-
end
|
29
|
-
|
30
|
-
events.flatten.each do |event_name|
|
31
|
-
define_method(:"can_#{event_name}?") do
|
32
|
-
self.class.class_variable_get(:@@__specs__).map { |spc|
|
33
|
-
spc.states[self.send(:"#{spc.name}")].event_names.include?(event_name)
|
34
|
-
}.include?(true)
|
35
|
-
end
|
36
|
-
|
37
|
-
define_method(:"#{event_name}!") do |*args|
|
38
|
-
switch_from(nil, event_name, *args)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
3
|
def self.included(base)
|
47
4
|
base.extend Switcher::ClassMethods
|
48
|
-
base.extend Switcher::ActiveRecord::ClassMethods
|
49
5
|
|
50
6
|
base.class_eval do
|
7
|
+
include Switcher
|
8
|
+
|
51
9
|
after_initialize do
|
52
10
|
self.class.class_variable_get(:@@__specs__).each do |spec|
|
53
11
|
spec_name = spec.name
|
@@ -60,10 +18,6 @@ module Switcher
|
|
60
18
|
end
|
61
19
|
end
|
62
20
|
|
63
|
-
def switch(event, *args)
|
64
|
-
switch_from(self.class, event, *args)
|
65
|
-
end
|
66
|
-
|
67
21
|
def switch_from(orig, event, *args)
|
68
22
|
return false unless (self.respond_to?(:"can_#{event}?") and self.send(:"can_#{event}?"))
|
69
23
|
|
@@ -80,6 +34,5 @@ module Switcher
|
|
80
34
|
|
81
35
|
true
|
82
36
|
end
|
83
|
-
|
84
37
|
end
|
85
38
|
end
|
@@ -11,52 +11,13 @@ module Switcher
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
module ClassMethods
|
15
|
-
def switcher_pre_initialize(inst, spec)
|
16
|
-
inst.class_eval do
|
17
|
-
spec_name = spec.name
|
18
|
-
|
19
|
-
define_method(:"#{spec_name}_spec") { spec }
|
20
|
-
define_method(:"#{spec_name}_prev") { self.instance_variable_get(:"@#{spec_name}_statement").state_prev }
|
21
|
-
|
22
|
-
define_method(:"#{spec_name}") { self.instance_variable_get(:"@#{spec_name}_statement").state_current }
|
23
|
-
define_method(:"#{spec_name}=") { nil }
|
24
|
-
|
25
|
-
define_method(:"#{spec_name}_force") { |state| self.instance_variable_get(:"@#{spec_name}_statement").force_state(state.to_sym) }
|
26
|
-
|
27
|
-
events = []
|
28
|
-
|
29
|
-
spec.states.each_pair do |state_name, state|
|
30
|
-
define_method(:"#{spec_name}_#{state_name}?") do
|
31
|
-
state_name == self.send(:"#{spec_name}")
|
32
|
-
end
|
33
|
-
|
34
|
-
events << state.event_names
|
35
|
-
end
|
36
|
-
|
37
|
-
events.flatten.each do |event_name|
|
38
|
-
define_method(:"can_#{event_name}?") do
|
39
|
-
self.class.class_variable_get(:@@__specs__).map { |spc|
|
40
|
-
spc.states[self.send(:"#{spc.name}")].event_names.include?(event_name)
|
41
|
-
}.include?(true)
|
42
|
-
end
|
43
|
-
|
44
|
-
define_method(:"#{event_name}!") do |*args|
|
45
|
-
switch(event_name, *args)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
14
|
def self.included(base)
|
53
|
-
base.extend
|
54
|
-
base.extend
|
55
|
-
base.extend Switcher::Object::ClassMethods
|
56
|
-
end
|
15
|
+
base.extend Switcher::ClassMethods
|
16
|
+
base.extend Switcher::Object::Initializer
|
57
17
|
|
58
|
-
|
59
|
-
|
18
|
+
base.class_eval do
|
19
|
+
include Switcher
|
20
|
+
end
|
60
21
|
end
|
61
22
|
|
62
23
|
def switch_from(orig, event, *args)
|
data/lib/switcher/facade.rb
CHANGED
data/lib/switcher/spec.rb
CHANGED
@@ -7,10 +7,10 @@ module Switcher
|
|
7
7
|
@name = name.to_sym
|
8
8
|
@states = {}
|
9
9
|
@states_list = []
|
10
|
-
@
|
10
|
+
@subscribers = []
|
11
11
|
end
|
12
12
|
|
13
|
-
attr_reader :name, :states, :states_list, :
|
13
|
+
attr_reader :name, :states, :states_list, :subscribers
|
14
14
|
|
15
15
|
def state(name, &block)
|
16
16
|
listener = Listener.new(name)
|
@@ -21,7 +21,7 @@ module Switcher
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def notify(*list)
|
24
|
-
@
|
24
|
+
@subscribers = list
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
data/lib/switcher/statement.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
module Switcher
|
2
2
|
class Statement
|
3
3
|
def initialize(instance, spec, state_current=nil, state_prev=nil)
|
4
|
-
@
|
5
|
-
|
6
|
-
@spec = spec
|
7
|
-
|
4
|
+
@spec = spec
|
5
|
+
@instance = instance
|
8
6
|
@state_prev = state_prev
|
9
7
|
@state_current = state_current
|
10
8
|
end
|
@@ -19,40 +17,41 @@ module Switcher
|
|
19
17
|
@state_prev ? @state_prev.to_sym : nil
|
20
18
|
end
|
21
19
|
|
22
|
-
def publish(event,
|
20
|
+
def publish(event, target, args)
|
23
21
|
states = @spec.states
|
24
22
|
state = state_current
|
25
23
|
|
26
24
|
return unless states.has_key?(state)
|
27
25
|
|
28
|
-
|
26
|
+
listener = states[state]
|
27
|
+
facade = Facade.new(target, args)
|
29
28
|
|
30
29
|
["before_#{event}", event.to_s].each do |ev|
|
31
|
-
|
30
|
+
listener.trigger(ev, facade, @instance, args)
|
32
31
|
end
|
33
32
|
|
34
33
|
set_state(facade)
|
35
34
|
|
36
|
-
|
35
|
+
listener.trigger("after_#{event}", facade, @instance, args)
|
37
36
|
|
38
37
|
return if facade.bubble_cancelled
|
39
38
|
|
40
|
-
|
41
|
-
@spec.
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
39
|
+
unless @spec.subscribers.empty?
|
40
|
+
@spec.subscribers.each do |sub|
|
41
|
+
sub_instance = @instance.send(sub.to_sym)
|
42
|
+
|
43
|
+
if sub_instance.respond_to?(:each)
|
44
|
+
sub_instance.each { |ti| sub_switch(ti, target, event, args) }
|
47
45
|
else
|
48
|
-
|
46
|
+
sub_switch(sub_instance, target, event, args)
|
49
47
|
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
48
|
+
end # each
|
49
|
+
end # unless
|
50
|
+
end # def publish
|
53
51
|
|
54
52
|
def force_state(state)
|
55
53
|
return unless @spec.states_list.include?(state.to_sym) # FIXME - raise exception
|
54
|
+
|
56
55
|
@state_prev = state_current
|
57
56
|
@state_current = state.to_sym
|
58
57
|
end
|
@@ -66,5 +65,9 @@ module Switcher
|
|
66
65
|
end
|
67
66
|
end
|
68
67
|
|
69
|
-
|
68
|
+
def sub_switch(sub, target, event, args)
|
69
|
+
sub.switch_from(target, event, args) if sub.respond_to?(:switch_from)
|
70
|
+
end
|
71
|
+
|
72
|
+
end # class Statement
|
70
73
|
end
|
data/lib/switcher/version.rb
CHANGED
data/lib/switcher.rb
CHANGED
@@ -14,7 +14,41 @@ module Switcher
|
|
14
14
|
|
15
15
|
self.class_variable_get(:@@__specs__) << spec
|
16
16
|
|
17
|
-
|
17
|
+
spec_name = spec.name
|
18
|
+
|
19
|
+
define_method(:"#{spec_name}_spec") { spec }
|
20
|
+
define_method(:"#{spec_name}_prev") { self.instance_variable_get(:"@#{spec_name}_statement").state_prev }
|
21
|
+
|
22
|
+
define_method(:"#{spec_name}") { self.instance_variable_get(:"@#{spec_name}_statement").state_current }
|
23
|
+
define_method(:"#{spec_name}=") { nil }
|
24
|
+
|
25
|
+
define_method(:"#{spec_name}_force") { |state| self.instance_variable_get(:"@#{spec_name}_statement").force_state(state.to_sym) }
|
26
|
+
|
27
|
+
events = []
|
28
|
+
|
29
|
+
spec.states.each_pair do |state_name, state|
|
30
|
+
define_method(:"#{spec_name}_#{state_name}?") do
|
31
|
+
state_name == self.send(:"#{spec_name}")
|
32
|
+
end
|
33
|
+
|
34
|
+
events << state.event_names
|
35
|
+
end
|
36
|
+
|
37
|
+
events.flatten.each do |event_name|
|
38
|
+
define_method(:"can_#{event_name}?") do
|
39
|
+
self.class.class_variable_get(:@@__specs__).map { |spc|
|
40
|
+
spc.states[self.send(:"#{spc.name}")].event_names.include?(event_name)
|
41
|
+
}.include?(true)
|
42
|
+
end
|
43
|
+
|
44
|
+
define_method(:"#{event_name}!") do |*args|
|
45
|
+
switch(event_name, *args)
|
46
|
+
end
|
47
|
+
end
|
18
48
|
end
|
19
49
|
end
|
50
|
+
|
51
|
+
def switch(event, *args)
|
52
|
+
switch_from(self.class, event, *args)
|
53
|
+
end
|
20
54
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-08 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70130949632800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70130949632800
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70130949632380 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70130949632380
|
36
36
|
description: Switcher is simple, event-driven state machine
|
37
37
|
email:
|
38
38
|
- andrey@aejis.eu
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- .travis.yml
|
45
45
|
- Gemfile
|
46
46
|
- Guardfile
|
47
|
+
- LICENSE
|
47
48
|
- README.md
|
48
49
|
- Rakefile
|
49
50
|
- lib/switcher.rb
|