state_transition 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/lib/state_transition/version.rb +3 -0
- data/lib/state_transition.rb +82 -0
- data/sample/example.rb +24 -0
- data/sample/example2.rb +44 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/state_transition_spec.rb +91 -0
- data/state_transition.gemspec +24 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e196c6420a2f21db25e9e847ebf8dc7d6cf4564
|
4
|
+
data.tar.gz: c3c3b778f89fbec0cdbcd616a48d0833cf973355
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9ba0df6dd2d1d0d6d1749e10233eee2239d78e734cea7b140358683bdebe84d4a1c1a99d59521ce375787a56364f9380c4b382c6b0f4977c3a7b97f209a5859
|
7
|
+
data.tar.gz: bc90f482fbf910cbdffb5f89d4e782ff154aa4c142396a5129e54e8b2f8560e7c552f6b300d5cb4c4a679518e87560cc04d16fe4c40f66d9e97a605e9735ecbf
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
|
19
|
+
**/*.swp
|
20
|
+
*.swp
|
21
|
+
**/*~
|
22
|
+
*.*~
|
23
|
+
*~
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 siman-man
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# StateTransition
|
2
|
+
|
3
|
+
https://github.com/jakesgordon/javascript-state-machine
|
4
|
+
これが便利だったのでRuby verを作ってみた。
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'state_transition'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install state_transition
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
require 'state_transition'
|
23
|
+
|
24
|
+
state = StateTransition::StateMachine.new({
|
25
|
+
initial: :first,
|
26
|
+
|
27
|
+
actions: [
|
28
|
+
{ name: "move_first", from: [ :second, :third ], to: :first },
|
29
|
+
{ name: "move_second", from: [ :first, :third ], to: :second },
|
30
|
+
{ name: "move_third", from: :second, to: :third },
|
31
|
+
],
|
32
|
+
|
33
|
+
callbacks: {
|
34
|
+
before_second: -> { puts "before_second" },
|
35
|
+
after_second: -> { puts "after_second" },
|
36
|
+
}
|
37
|
+
})
|
38
|
+
|
39
|
+
puts state.current #=> :first
|
40
|
+
puts state.can_move?(:second) #=> true
|
41
|
+
puts state.can_move?(:third) #=> false
|
42
|
+
|
43
|
+
state.move_second
|
44
|
+
|
45
|
+
puts state.current #=> :second
|
46
|
+
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require "state_transition/version"
|
2
|
+
|
3
|
+
module StateTransition
|
4
|
+
class StateMachine
|
5
|
+
attr_reader :current
|
6
|
+
attr_accessor :state_list
|
7
|
+
|
8
|
+
def initialize(data = nil)
|
9
|
+
@state_list = []
|
10
|
+
@state_graph = Hash.new{|hash, key| hash[key] = []}
|
11
|
+
@callbacks = {}
|
12
|
+
@sequence = 3
|
13
|
+
|
14
|
+
begin
|
15
|
+
@current = data[:initial]
|
16
|
+
rescue
|
17
|
+
raise StandardError, "StateMachine: Not define first state."
|
18
|
+
end
|
19
|
+
|
20
|
+
set_state(data[:initial])
|
21
|
+
|
22
|
+
create_move_action(data[:actions] || [])
|
23
|
+
create_callbacks(data[:callbacks] || [])
|
24
|
+
end
|
25
|
+
|
26
|
+
def have_state?(state)
|
27
|
+
@state_list.include?(state)
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_move_action(actions)
|
31
|
+
actions.each do |action|
|
32
|
+
name = action[:name]
|
33
|
+
from = action[:from]
|
34
|
+
to = action[:to]
|
35
|
+
|
36
|
+
set_state(from)
|
37
|
+
set_state(to)
|
38
|
+
create_edge(from, to)
|
39
|
+
|
40
|
+
StateMachine.class_eval do
|
41
|
+
define_method name do
|
42
|
+
if can_move?(to)
|
43
|
+
before_func = ("before_" + to.to_s).to_sym
|
44
|
+
after_func = ("after_" + to.to_s).to_sym
|
45
|
+
if @callbacks[before_func]
|
46
|
+
@callbacks[before_func].call
|
47
|
+
end
|
48
|
+
@current = to
|
49
|
+
@callbacks[after_func].call if @callbacks[after_func]
|
50
|
+
else
|
51
|
+
raise StandardError, "Can not move from '#{@current}' to '#{to}'!"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_callbacks(callbacks)
|
59
|
+
callbacks.each do |name, function|
|
60
|
+
@callbacks[name] = function
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def can_move?(state)
|
65
|
+
@state_graph[@current].include?(state)
|
66
|
+
end
|
67
|
+
|
68
|
+
def set_state(state)
|
69
|
+
@state_list.push state unless have_state?(state) || !state.kind_of?(Symbol)
|
70
|
+
end
|
71
|
+
|
72
|
+
def create_edge(from, to)
|
73
|
+
if from.kind_of?(Array)
|
74
|
+
from.each do |state|
|
75
|
+
@state_graph[state].push to unless @state_graph[state].include?(to)
|
76
|
+
end
|
77
|
+
else
|
78
|
+
@state_graph[from].push to
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/sample/example.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'state_transition'
|
2
|
+
|
3
|
+
state = StateTransition::StateMachine.new({
|
4
|
+
initial: :first,
|
5
|
+
|
6
|
+
actions: [
|
7
|
+
{ name: "move_first", from: [ :second, :third ], to: :first },
|
8
|
+
{ name: "move_second", from: [ :first, :third ], to: :second },
|
9
|
+
{ name: "move_third", from: :second, to: :third },
|
10
|
+
],
|
11
|
+
|
12
|
+
callbacks: {
|
13
|
+
before_second: -> { puts "before_second" },
|
14
|
+
after_second: -> { puts "after_second" },
|
15
|
+
}
|
16
|
+
})
|
17
|
+
|
18
|
+
puts state.current #=> :first
|
19
|
+
puts state.can_move?(:second) #=> true
|
20
|
+
puts state.can_move?(:third) #=> false
|
21
|
+
|
22
|
+
state.move_second
|
23
|
+
|
24
|
+
puts state.current #=> :second
|
data/sample/example2.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'state_transition'
|
2
|
+
|
3
|
+
class HelloWorld
|
4
|
+
attr_reader :state
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@hello = "Hello World!"
|
8
|
+
|
9
|
+
@state = StateTransition::StateMachine.new({
|
10
|
+
initial: 'first',
|
11
|
+
|
12
|
+
actions: [
|
13
|
+
{ name: 'one', from: 'first', to: 'second' },
|
14
|
+
{ name: 'two', from: 'second', to: 'third' },
|
15
|
+
{ name: 'three', from: 'third', to: 'first' },
|
16
|
+
{ name: 'reset', from: ['first', 'second', 'thrid'], to: 'first' }
|
17
|
+
],
|
18
|
+
|
19
|
+
callbacks: {
|
20
|
+
before_first: -> { say_world },
|
21
|
+
before_second: -> { say_hello },
|
22
|
+
before_third: -> { say_hello_world },
|
23
|
+
}
|
24
|
+
})
|
25
|
+
end
|
26
|
+
|
27
|
+
def say_hello
|
28
|
+
puts "hello"
|
29
|
+
end
|
30
|
+
|
31
|
+
def say_hello_world
|
32
|
+
puts @hello
|
33
|
+
end
|
34
|
+
|
35
|
+
def say_world
|
36
|
+
puts "world"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
h = HelloWorld.new
|
41
|
+
|
42
|
+
h.state.one
|
43
|
+
h.state.two
|
44
|
+
h.state.three
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StateTransition do
|
4
|
+
|
5
|
+
it 'should have a version number' do
|
6
|
+
StateTransition::VERSION.should_not be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "no initialize" do
|
10
|
+
it "should be error occured." do
|
11
|
+
lambda{ state = StateTransition::StateMachine.new() }.should raise_error(StandardError)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "init state" do
|
16
|
+
before :all do
|
17
|
+
@sequence = []
|
18
|
+
@state = StateTransition::StateMachine.new({
|
19
|
+
initial: :first,
|
20
|
+
|
21
|
+
actions: [
|
22
|
+
{ name: 'two', from: :first, to: :second },
|
23
|
+
{ name: 'three', from: :second, to: :third },
|
24
|
+
{ name: 'reset', from: [:first, :second, :third], to: :first }
|
25
|
+
],
|
26
|
+
|
27
|
+
callbacks: {
|
28
|
+
before_first: -> { @sequence << "before_first" },
|
29
|
+
before_second: -> { @sequence << "before_second" },
|
30
|
+
before_third: -> { @sequence << "before_third" },
|
31
|
+
after_first: -> { @sequence << "after_first" },
|
32
|
+
after_second: -> { @sequence << "after_second"; puts "Hello World!" },
|
33
|
+
after_third: -> { @sequence << "after_third" },
|
34
|
+
}
|
35
|
+
})
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should @state have three(:first, :second, :third) state" do
|
39
|
+
@state.have_state?(:first).should be_true
|
40
|
+
@state.have_state?(:second).should be_true
|
41
|
+
@state.have_state?(:third).should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should first state is :first" do
|
45
|
+
@state.current.should == :first
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "test for callbacks" do
|
49
|
+
before :each do
|
50
|
+
@state.reset
|
51
|
+
end
|
52
|
+
|
53
|
+
it "check callback when move second." do
|
54
|
+
@sequence.clear
|
55
|
+
@state.two
|
56
|
+
|
57
|
+
@sequence[0].should == "before_second"
|
58
|
+
@sequence[1].should == "after_second"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "about move state" do
|
63
|
+
before :each do
|
64
|
+
@state.reset
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be 'two' action after state 'second'" do
|
68
|
+
@state.two
|
69
|
+
@state.current.should == :second
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should be :first state after 'reset' action." do
|
73
|
+
@state.two
|
74
|
+
@state.reset
|
75
|
+
@state.current.should == :first
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should be can move from 'first' to 'second'" do
|
79
|
+
@state.can_move?(:second).should be_true
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should be cannot move from 'first' to 'third'" do
|
83
|
+
@state.can_move?(:third).should be_false
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be raised error invalid action" do
|
87
|
+
lambda{ @state.three }.should raise_error(StandardError)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'state_transition/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "state_transition"
|
8
|
+
spec.version = StateTransition::VERSION
|
9
|
+
spec.authors = ["siman-man"]
|
10
|
+
spec.email = ["k128585@ie.u-ryukyu.ac.jp"]
|
11
|
+
spec.description = %q{This is ruby state_machine.}
|
12
|
+
spec.summary = %q{This is ruby state_machine.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_transition
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- siman-man
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: This is ruby state_machine.
|
56
|
+
email:
|
57
|
+
- k128585@ie.u-ryukyu.ac.jp
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/state_transition.rb
|
70
|
+
- lib/state_transition/version.rb
|
71
|
+
- sample/example.rb
|
72
|
+
- sample/example2.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/state_transition_spec.rb
|
75
|
+
- state_transition.gemspec
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.0.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: This is ruby state_machine.
|
100
|
+
test_files:
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/state_transition_spec.rb
|