r_machine 0.1.0
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 +7 -0
- data/.gitignore +38 -0
- data/.rspec +4 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +67 -0
- data/Guardfile +11 -0
- data/LICENSE +22 -0
- data/README.md +63 -0
- data/Rakefile +8 -0
- data/lib/r_machine/event.rb +15 -0
- data/lib/r_machine/notifiers/call_event_method_notifier.rb +58 -0
- data/lib/r_machine/notifiers/change_state_value_notifier.rb +14 -0
- data/lib/r_machine/notifiers/retrieve_state_value_notifier.rb +13 -0
- data/lib/r_machine/rule.rb +29 -0
- data/lib/r_machine/state_notifier_factory.rb +64 -0
- data/lib/r_machine/state_template.rb +36 -0
- data/lib/r_machine/state_transaction_not_permitted_error.rb +4 -0
- data/lib/r_machine.rb +44 -0
- data/spec/integrations/r_machine_spec.rb +143 -0
- data/spec/integrations/shared_contexts/entity.rb +34 -0
- data/spec/spec_helper.rb +96 -0
- data/travis.yml +3 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 547d21093e60db263e1f66f40a50a7c332fb1dd7
|
4
|
+
data.tar.gz: def9937507ea971c2c598d44846889e40a13be45
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61ee8e6ed1aebb0e8dd387c01cec65322e0a4ef44c68d661d24dff0a470f56ea21a90b1c62a723eefcf0785cf96c5bbad759390e0ad2ae36000566ce4180c067
|
7
|
+
data.tar.gz: a0294c696a0cb3cd9e23fc279b1ed692219b2c1d3b00538658d3d96603003ae92e8d903e56c1310abc8bb064ff3658d103f6a1b3e8935787979cc2bb027d893f
|
data/.gitignore
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
36
|
+
|
37
|
+
*.swp
|
38
|
+
*.swo
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
statemachine
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
byebug (6.0.2)
|
5
|
+
coderay (1.1.0)
|
6
|
+
diff-lcs (1.2.5)
|
7
|
+
ffi (1.9.10)
|
8
|
+
formatador (0.2.5)
|
9
|
+
guard (2.13.0)
|
10
|
+
formatador (>= 0.2.4)
|
11
|
+
listen (>= 2.7, <= 4.0)
|
12
|
+
lumberjack (~> 1.0)
|
13
|
+
nenv (~> 0.1)
|
14
|
+
notiffany (~> 0.0)
|
15
|
+
pry (>= 0.9.12)
|
16
|
+
shellany (~> 0.0)
|
17
|
+
thor (>= 0.18.1)
|
18
|
+
guard-compat (1.2.1)
|
19
|
+
guard-rspec (4.6.4)
|
20
|
+
guard (~> 2.1)
|
21
|
+
guard-compat (~> 1.1)
|
22
|
+
rspec (>= 2.99.0, < 4.0)
|
23
|
+
listen (3.0.3)
|
24
|
+
rb-fsevent (>= 0.9.3)
|
25
|
+
rb-inotify (>= 0.9)
|
26
|
+
lumberjack (1.0.9)
|
27
|
+
method_source (0.8.2)
|
28
|
+
nenv (0.2.0)
|
29
|
+
notiffany (0.0.8)
|
30
|
+
nenv (~> 0.1)
|
31
|
+
shellany (~> 0.0)
|
32
|
+
pry (0.10.3)
|
33
|
+
coderay (~> 1.1.0)
|
34
|
+
method_source (~> 0.8.1)
|
35
|
+
slop (~> 3.4)
|
36
|
+
rake (10.4.2)
|
37
|
+
rb-fsevent (0.9.6)
|
38
|
+
rb-inotify (0.9.5)
|
39
|
+
ffi (>= 0.5.0)
|
40
|
+
rspec (3.3.0)
|
41
|
+
rspec-core (~> 3.3.0)
|
42
|
+
rspec-expectations (~> 3.3.0)
|
43
|
+
rspec-mocks (~> 3.3.0)
|
44
|
+
rspec-core (3.3.2)
|
45
|
+
rspec-support (~> 3.3.0)
|
46
|
+
rspec-expectations (3.3.1)
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
48
|
+
rspec-support (~> 3.3.0)
|
49
|
+
rspec-mocks (3.3.2)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.3.0)
|
52
|
+
rspec-support (3.3.0)
|
53
|
+
shellany (0.0.1)
|
54
|
+
slop (3.6.0)
|
55
|
+
thor (0.19.1)
|
56
|
+
|
57
|
+
PLATFORMS
|
58
|
+
ruby
|
59
|
+
|
60
|
+
DEPENDENCIES
|
61
|
+
byebug
|
62
|
+
guard-rspec
|
63
|
+
rake
|
64
|
+
rspec
|
65
|
+
|
66
|
+
BUNDLED WITH
|
67
|
+
1.10.6
|
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
2
|
+
# rspec may be run, below are examples of the most common uses.
|
3
|
+
# * bundler: 'bundle exec rspec'
|
4
|
+
# * bundler binstubs: 'bin/rspec'
|
5
|
+
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
6
|
+
# installed the spring binstubs per the docs)
|
7
|
+
# * zeus: 'zeus rspec' (requires the server to be started separetly)
|
8
|
+
# * 'just' rspec: 'rspec'
|
9
|
+
guard :rspec, cmd:"bundle exec rspec" do
|
10
|
+
watch(%r{^spec/.+_spec\.rb$})
|
11
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Rafael Soares dos Santos
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# RMachine
|
2
|
+
|
3
|
+
[](https://travis-ci.org/rafaels88/rmachine)
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
class Entity
|
7
|
+
include RMachine
|
8
|
+
|
9
|
+
r_machine do |state|
|
10
|
+
state.field = :status
|
11
|
+
state.initial = :created
|
12
|
+
|
13
|
+
state.on :event_without_param do |event|
|
14
|
+
event.change_to :second_state, from: :created
|
15
|
+
event.change_to :third_state, from: :second_state
|
16
|
+
event.change_to :fourth_state, from: :third_state, if: :is_this_true?
|
17
|
+
end
|
18
|
+
|
19
|
+
state.on :event_with_param do |event|
|
20
|
+
event.change_to :second_state, from: :created
|
21
|
+
event.change_to :third_state, from: :second_state
|
22
|
+
event.change_to :fourth_state, from: :third_state, if: :is_param_correct?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def event_without_param
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
def event_with_param(param_to_verify)
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
def is_this_true?
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
def is_param_correct?(param_to_verify)
|
39
|
+
if param_to_verify == :expected
|
40
|
+
true
|
41
|
+
else
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
entity = Entity.new
|
48
|
+
puts entity.status # :created
|
49
|
+
|
50
|
+
entity.event_without_param
|
51
|
+
puts entity.status # :second_state
|
52
|
+
|
53
|
+
entity.event_with_param(:any)
|
54
|
+
puts entity.status # :third_state
|
55
|
+
|
56
|
+
entity.event_with_param(:any) # raises StateTransactionNotPermittedError
|
57
|
+
|
58
|
+
entity.event_with_param(:expected)
|
59
|
+
puts entity.status # :fourth_state
|
60
|
+
|
61
|
+
entity.status = :created
|
62
|
+
puts entity.status # :created
|
63
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module RMachine
|
2
|
+
class Event
|
3
|
+
attr_reader :event_name, :rules
|
4
|
+
|
5
|
+
def initialize(event_name)
|
6
|
+
@event_name = event_name
|
7
|
+
@rules = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def change_to(state, rule_options={})
|
11
|
+
rule = Rule.new({new_state: state}.merge(rule_options))
|
12
|
+
rules.push(rule)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module RMachine
|
2
|
+
class CallEventMethodNotifier
|
3
|
+
attr_reader :method_to_call, :message_args, :change_state_value_notifier, :state_template,
|
4
|
+
:entity_with_state
|
5
|
+
|
6
|
+
def initialize(params)
|
7
|
+
@entity_with_state = params[:entity]
|
8
|
+
@state_template = params[:state_template]
|
9
|
+
@method_to_call = params[:method_to_call]
|
10
|
+
@message_args = params[:message_args]
|
11
|
+
@change_state_value_notifier = params[:change_state_value_notifier]
|
12
|
+
end
|
13
|
+
|
14
|
+
def notify!
|
15
|
+
event = state_template.find_event(method_to_call)
|
16
|
+
|
17
|
+
success = false
|
18
|
+
event.rules.each do |rule|
|
19
|
+
if rule.must_have_specific_state?
|
20
|
+
if is_in_state?(rule.state_should_be)
|
21
|
+
if rule.has_conditional?
|
22
|
+
if execute_conditional_method(rule.conditional_method, message_args) == true
|
23
|
+
change_current_state_value_to(rule.change_to)
|
24
|
+
success = true
|
25
|
+
end
|
26
|
+
else
|
27
|
+
change_current_state_value_to(rule.change_to)
|
28
|
+
success = true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
else
|
32
|
+
change_current_state_value_to(rule.change_to)
|
33
|
+
success = true
|
34
|
+
end
|
35
|
+
break if success == true
|
36
|
+
end
|
37
|
+
|
38
|
+
raise StateTransactionNotPermittedError if success == false
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def is_in_state?(state)
|
44
|
+
state_template.state == state
|
45
|
+
end
|
46
|
+
|
47
|
+
def execute_conditional_method(method, args_to_send)
|
48
|
+
entity_with_state.send(method, *args_to_send)
|
49
|
+
end
|
50
|
+
|
51
|
+
def change_current_state_value_to(value)
|
52
|
+
ChangeStateValueNotifier.new({
|
53
|
+
state_template: state_template,
|
54
|
+
new_value: value
|
55
|
+
}).notify!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module RMachine
|
2
|
+
class ChangeStateValueNotifier
|
3
|
+
attr_reader :state_template, :new_value
|
4
|
+
|
5
|
+
def initialize(params)
|
6
|
+
@state_template = params[:state_template]
|
7
|
+
@new_value = params[:new_value]
|
8
|
+
end
|
9
|
+
|
10
|
+
def notify!
|
11
|
+
state_template.state = new_value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RMachine
|
2
|
+
class Rule
|
3
|
+
attr_reader :rule_options
|
4
|
+
|
5
|
+
def initialize(params={})
|
6
|
+
@rule_options = params
|
7
|
+
end
|
8
|
+
|
9
|
+
def has_conditional?
|
10
|
+
!@rule_options[:if].nil?
|
11
|
+
end
|
12
|
+
|
13
|
+
def must_have_specific_state?
|
14
|
+
!@rule_options[:from].nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def conditional_method
|
18
|
+
@rule_options[:if]
|
19
|
+
end
|
20
|
+
|
21
|
+
def change_to
|
22
|
+
@rule_options[:new_state]
|
23
|
+
end
|
24
|
+
|
25
|
+
def state_should_be
|
26
|
+
@rule_options[:from]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module RMachine
|
2
|
+
class StateNotifierFactory
|
3
|
+
attr_reader :object_called, :state_template,
|
4
|
+
:message_name, :message_args
|
5
|
+
|
6
|
+
def initialize(params={})
|
7
|
+
@object_called = params[:object_called]
|
8
|
+
@state_template = params[:state_template]
|
9
|
+
@message_name = params[:message_name]
|
10
|
+
@message_args = params[:message_args]
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_notifying_state_machine?
|
14
|
+
is_calling_state_attribute? ||
|
15
|
+
is_changing_state_attribute? ||
|
16
|
+
is_calling_an_event?
|
17
|
+
end
|
18
|
+
|
19
|
+
def notifier
|
20
|
+
if is_changing_state_attribute?
|
21
|
+
build_change_state_value_notifier!
|
22
|
+
elsif is_calling_state_attribute?
|
23
|
+
build_retrieve_state_value_notifier!
|
24
|
+
elsif is_calling_an_event?
|
25
|
+
build_call_event_method_notifier!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def build_call_event_method_notifier!
|
32
|
+
@call_event_method_notifier ||= CallEventMethodNotifier.new({
|
33
|
+
entity: object_called,
|
34
|
+
state_template: state_template,
|
35
|
+
method_to_call: message_name,
|
36
|
+
message_args: message_args,
|
37
|
+
change_state_value_notifier: build_change_state_value_notifier!
|
38
|
+
})
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_change_state_value_notifier!
|
42
|
+
@change_state_value_notifier ||= ChangeStateValueNotifier.new({
|
43
|
+
state_template: state_template,
|
44
|
+
new_value: message_args.first
|
45
|
+
})
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_retrieve_state_value_notifier!
|
49
|
+
@retrieve_state_value_notifier ||= RetrieveStateValueNotifier.new({ state_template: state_template })
|
50
|
+
end
|
51
|
+
|
52
|
+
def is_calling_state_attribute?
|
53
|
+
message_name.to_s == state_template.state_field_name.to_s
|
54
|
+
end
|
55
|
+
|
56
|
+
def is_changing_state_attribute?
|
57
|
+
message_name.to_s == "#{state_template.state_field_name}="
|
58
|
+
end
|
59
|
+
|
60
|
+
def is_calling_an_event?
|
61
|
+
!state_template.find_event(message_name).nil?
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RMachine
|
2
|
+
class StateTemplate
|
3
|
+
attr_reader :state_field_name, :events_by_name
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@state_value = nil
|
7
|
+
@events_by_name = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def state
|
11
|
+
@state_value
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_event(name)
|
15
|
+
events_by_name[name]
|
16
|
+
end
|
17
|
+
|
18
|
+
def state=(v)
|
19
|
+
@state_value = v
|
20
|
+
end
|
21
|
+
|
22
|
+
def field=(field_name)
|
23
|
+
@state_field_name = field_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def initial=(initial_status)
|
27
|
+
@state_value = initial_status
|
28
|
+
end
|
29
|
+
|
30
|
+
def on(event_name)
|
31
|
+
event = Event.new(event_name)
|
32
|
+
events_by_name[event_name] = event
|
33
|
+
yield event
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/r_machine.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'r_machine/event'
|
2
|
+
require 'r_machine/rule'
|
3
|
+
require 'r_machine/state_template'
|
4
|
+
require 'r_machine/state_notifier_factory'
|
5
|
+
require 'r_machine/notifiers/call_event_method_notifier'
|
6
|
+
require 'r_machine/notifiers/change_state_value_notifier'
|
7
|
+
require 'r_machine/notifiers/retrieve_state_value_notifier'
|
8
|
+
require 'r_machine/state_transaction_not_permitted_error'
|
9
|
+
|
10
|
+
module RMachine
|
11
|
+
def self.included(base)
|
12
|
+
base.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(called_method, *args)
|
16
|
+
factory_params = {
|
17
|
+
object_called: self,
|
18
|
+
state_template: ClassMethods.state_template,
|
19
|
+
message_name: called_method,
|
20
|
+
message_args: args
|
21
|
+
}
|
22
|
+
|
23
|
+
factory = StateNotifierFactory.new factory_params
|
24
|
+
|
25
|
+
if factory.is_notifying_state_machine?
|
26
|
+
factory.notifier.notify!
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
def r_machine
|
36
|
+
@@state_template = StateTemplate.new
|
37
|
+
yield @@state_template
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.state_template
|
41
|
+
@@state_template
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative '../../lib/r_machine'
|
3
|
+
require_relative '../../lib/r_machine/event'
|
4
|
+
require_relative '../../lib/r_machine/rule'
|
5
|
+
require_relative '../../lib/r_machine/state_template'
|
6
|
+
require_relative '../../lib/r_machine/state_notifier_factory'
|
7
|
+
require_relative '../../lib/r_machine/notifiers/call_event_method_notifier'
|
8
|
+
require_relative '../../lib/r_machine/notifiers/change_state_value_notifier'
|
9
|
+
require_relative '../../lib/r_machine/notifiers/retrieve_state_value_notifier'
|
10
|
+
require_relative '../../lib/r_machine/state_transaction_not_permitted_error'
|
11
|
+
require_relative 'shared_contexts/entity'
|
12
|
+
|
13
|
+
describe Entity do
|
14
|
+
context "when defined state_field is #status" do
|
15
|
+
describe ".new" do
|
16
|
+
context "when initial state value is defined as :created" do
|
17
|
+
it "changes #status value to :created" do
|
18
|
+
expect(subject.status).to be == :created
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#event_without_param" do
|
24
|
+
context "with #status == :created and change_to :second_state, :from => :created defined" do
|
25
|
+
before do
|
26
|
+
subject.status = :created
|
27
|
+
end
|
28
|
+
|
29
|
+
it "changes #status to :second_state" do
|
30
|
+
subject.event_without_param
|
31
|
+
expect(subject.status).to be == :second_state
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "with #status == :second_state and change_to :third_state, :from => :second_state defined" do
|
36
|
+
before do
|
37
|
+
subject.status = :second_state
|
38
|
+
end
|
39
|
+
|
40
|
+
it "changes #status to :third_state" do
|
41
|
+
subject.event_without_param
|
42
|
+
expect(subject.status).to be == :third_state
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
context "with #status == :third_state and change_to :fourth_state, :from => :third_state defined, :if => :is_this_true?" do
|
48
|
+
before do
|
49
|
+
subject.status = :third_state
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when #is_this_true? == true" do
|
53
|
+
before do
|
54
|
+
allow(subject).to receive(:is_this_true?).and_return true
|
55
|
+
end
|
56
|
+
|
57
|
+
it "changes #status to :fourth_state" do
|
58
|
+
subject.event_without_param
|
59
|
+
expect(subject.status).to be == :fourth_state
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when #is_this_true? == false" do
|
64
|
+
before do
|
65
|
+
allow(subject).to receive(:is_this_true?).and_return false
|
66
|
+
end
|
67
|
+
|
68
|
+
it "raises StateTransactionNotPermittedError" do
|
69
|
+
expect { subject.event_without_param }.to raise_error RMachine::StateTransactionNotPermittedError
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "with #status == :another and change_to :third_state, :from => :second_state defined" do
|
75
|
+
before do
|
76
|
+
subject.status = :another
|
77
|
+
end
|
78
|
+
|
79
|
+
it "raises StateTransactionNotPermittedError" do
|
80
|
+
expect { subject.event_without_param }.to raise_error RMachine::StateTransactionNotPermittedError
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#event_with_param" do
|
86
|
+
context "with #status == :created and change_to :second_state, :from => :created defined" do
|
87
|
+
before do
|
88
|
+
subject.status = :created
|
89
|
+
end
|
90
|
+
|
91
|
+
it "changes #status to :second_state" do
|
92
|
+
subject.event_with_param(:any)
|
93
|
+
expect(subject.status).to be == :second_state
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "with #status == :second_state and change_to :third_state, :from => :second_state defined" do
|
98
|
+
before do
|
99
|
+
subject.status = :second_state
|
100
|
+
end
|
101
|
+
|
102
|
+
it "changes #status to :third_state" do
|
103
|
+
subject.event_with_param(:any)
|
104
|
+
expect(subject.status).to be == :third_state
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
context "with #status == :third_state and change_to :fourth_state, :from => :third_state defined, :if => :is_param_correct?" do
|
110
|
+
let(:correct_param) { :correct }
|
111
|
+
|
112
|
+
before do
|
113
|
+
allow(subject).to receive(:is_param_correct?).and_return false
|
114
|
+
allow(subject).to receive(:is_param_correct?).with(correct_param).and_return true
|
115
|
+
subject.status = :third_state
|
116
|
+
end
|
117
|
+
|
118
|
+
context "when #event_with_param is called with correct_param" do
|
119
|
+
it "changes #status to :fourth_state" do
|
120
|
+
subject.event_with_param(correct_param)
|
121
|
+
expect(subject.status).to be == :fourth_state
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context "when #event_with_param is NOT called with correct_param" do
|
126
|
+
it "raises StateTransactionNotPermittedError" do
|
127
|
+
expect { subject.event_with_param(:any) }.to raise_error RMachine::StateTransactionNotPermittedError
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "with #status == :another and change_to :third_state, :from => :second_state defined" do
|
133
|
+
before do
|
134
|
+
subject.status = :another
|
135
|
+
end
|
136
|
+
|
137
|
+
it "raises StateTransactionNotPermittedError" do
|
138
|
+
expect { subject.event_with_param(:any) }.to raise_error RMachine::StateTransactionNotPermittedError
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Entity
|
2
|
+
include RMachine
|
3
|
+
|
4
|
+
r_machine do |state|
|
5
|
+
state.field = :status
|
6
|
+
state.initial = :created
|
7
|
+
|
8
|
+
state.on :event_without_param do |event|
|
9
|
+
event.change_to :second_state, from: :created
|
10
|
+
event.change_to :third_state, from: :second_state
|
11
|
+
event.change_to :fourth_state, from: :third_state, if: :is_this_true?
|
12
|
+
end
|
13
|
+
|
14
|
+
state.on :event_with_param do |event|
|
15
|
+
event.change_to :second_state, from: :created
|
16
|
+
event.change_to :third_state, from: :second_state
|
17
|
+
event.change_to :fourth_state, from: :third_state, if: :is_param_correct?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def event_without_param
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def event_with_param(param)
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def is_this_true?
|
30
|
+
end
|
31
|
+
|
32
|
+
def is_param_correct?(param)
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Allows RSpec to persist some state between runs in order to support
|
54
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
55
|
+
# you configure your source control system to ignore this file.
|
56
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
57
|
+
|
58
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
59
|
+
# recommended. For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
62
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
63
|
+
config.disable_monkey_patching!
|
64
|
+
|
65
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
66
|
+
# be too noisy due to issues in dependencies.
|
67
|
+
config.warnings = true
|
68
|
+
|
69
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
70
|
+
# file, and it's useful to allow more verbose output when running an
|
71
|
+
# individual spec file.
|
72
|
+
if config.files_to_run.one?
|
73
|
+
# Use the documentation formatter for detailed output,
|
74
|
+
# unless a formatter has already been configured
|
75
|
+
# (e.g. via a command-line flag).
|
76
|
+
config.default_formatter = 'doc'
|
77
|
+
end
|
78
|
+
|
79
|
+
# Print the 10 slowest examples and example groups at the
|
80
|
+
# end of the spec run, to help surface which specs are running
|
81
|
+
# particularly slow.
|
82
|
+
config.profile_examples = 10
|
83
|
+
|
84
|
+
# Run specs in random order to surface order dependencies. If you find an
|
85
|
+
# order dependency and want to debug it, you can fix the order by providing
|
86
|
+
# the seed, which is printed after each run.
|
87
|
+
# --seed 1234
|
88
|
+
config.order = :random
|
89
|
+
|
90
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
91
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
92
|
+
# test failures related to randomization by passing the same `--seed` value
|
93
|
+
# as the one that triggered the failure.
|
94
|
+
Kernel.srand config.seed
|
95
|
+
=end
|
96
|
+
end
|
data/travis.yml
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: r_machine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Soares
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple state machine library
|
14
|
+
email: rafaeltravel88@gmail.common
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- ".rspec"
|
21
|
+
- ".ruby-gemset"
|
22
|
+
- ".ruby-version"
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- Guardfile
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/r_machine.rb
|
30
|
+
- lib/r_machine/event.rb
|
31
|
+
- lib/r_machine/notifiers/call_event_method_notifier.rb
|
32
|
+
- lib/r_machine/notifiers/change_state_value_notifier.rb
|
33
|
+
- lib/r_machine/notifiers/retrieve_state_value_notifier.rb
|
34
|
+
- lib/r_machine/rule.rb
|
35
|
+
- lib/r_machine/state_notifier_factory.rb
|
36
|
+
- lib/r_machine/state_template.rb
|
37
|
+
- lib/r_machine/state_transaction_not_permitted_error.rb
|
38
|
+
- spec/integrations/r_machine_spec.rb
|
39
|
+
- spec/integrations/shared_contexts/entity.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
- travis.yml
|
42
|
+
homepage: http://rubygems.org/gems/r_machine
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.4.8
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: RMachine
|
66
|
+
test_files:
|
67
|
+
- spec/integrations/r_machine_spec.rb
|
68
|
+
- spec/integrations/shared_contexts/entity.rb
|
69
|
+
- spec/spec_helper.rb
|