state_transition 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eefda24616ce72d905ecacb4cd7f49a055c0fb8a
4
- data.tar.gz: 9882ba655beb0b15c57ca018115272f620dcbdd8
3
+ metadata.gz: 8b22515a8243f2b3c2b2a3add22b3ffc39aac00a
4
+ data.tar.gz: cec1e3e10169e953134e8efaab57314b9a9b539c
5
5
  SHA512:
6
- metadata.gz: 405f0a030b838d2fb7eed6846956fb000fb51d457997ee5e704605432323f404abe5406a4ed1c6c9f373c352ac12b2ca39bce14d0a956dabad722d4c69b87093
7
- data.tar.gz: 36ff82c5681d26313b91d124399fa44cc040ba552bdc8fd76d50ed7b55f6ad0170f568e4ed9c10565b0c19587740e52ae3ff4ad4c41d8d110ce27a5f4f5e1320
6
+ metadata.gz: 47957db32525246bfd51584d58b1a9204d17e2a8836f965d9ae45bf38603dfe7010a4b7cc168f129f6dd1ef517f3bb878c04da5f97f23a330d1f66c027f2ad29
7
+ data.tar.gz: 29cdc113a112f6ce1b42480294f4dbd0e95efba38dde074b5ba403e0b0b17b1c4e4cfac199814f1a228312bb49b3e01f7c8c19056b546f1708c8c1b0c677d919
@@ -2,13 +2,14 @@ require "state_transition/version"
2
2
 
3
3
  module StateTransition
4
4
  class StateMachine
5
- attr_reader :current
5
+ attr_reader :current, :define_actions
6
6
  attr_accessor :state_list
7
7
 
8
8
  def initialize(data = nil)
9
9
  @state_list = []
10
10
  @state_graph = Hash.new{|hash, key| hash[key] = []}
11
11
  @callbacks = {}
12
+ @define_actions = []
12
13
 
13
14
  begin
14
15
  @current = data[:initial]
@@ -23,14 +24,20 @@ module StateTransition
23
24
  end
24
25
 
25
26
  def have_state?(state)
26
- @state_list.include?(state)
27
+ @state_list.include?(state.to_sym)
28
+ end
29
+
30
+ def add_action(action)
31
+ create_move_action([action])
27
32
  end
28
33
 
29
34
  def create_move_action(actions)
30
35
  actions.each do |action|
31
- name = action[:name]
36
+ name = action[:name].to_sym
32
37
  from = action[:from]
33
- to = action[:to]
38
+ to = action[:to].to_sym
39
+
40
+ @define_actions << name.to_sym
34
41
 
35
42
  set_state(from)
36
43
  set_state(to)
@@ -41,9 +48,8 @@ module StateTransition
41
48
  if can_move?(to)
42
49
  before_func = ("before_" + to.to_s).to_sym
43
50
  after_func = ("after_" + to.to_s).to_sym
44
- if @callbacks[before_func]
45
- @callbacks[before_func].call
46
- end
51
+
52
+ @callbacks[before_func].call if @callbacks[before_func]
47
53
  @current = to
48
54
  @callbacks[after_func].call if @callbacks[after_func]
49
55
  else
@@ -65,12 +71,13 @@ module StateTransition
65
71
  end
66
72
 
67
73
  def set_state(state)
68
- @state_list.push state unless have_state?(state) || !state.kind_of?(Symbol)
74
+ @state_list.push state.to_sym unless !state.respond_to?(:to_sym) || have_state?(state)
69
75
  end
70
76
 
71
77
  def create_edge(from, to)
72
78
  if from.kind_of?(Array)
73
79
  from.each do |state|
80
+ state = state.to_sym
74
81
  @state_graph[state].push to unless @state_graph[state].include?(to)
75
82
  end
76
83
  else
@@ -1,3 +1,3 @@
1
1
  module StateTransition
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'state_transition'
2
+
3
+ state = StateTransition::StateMachine.new(
4
+ initial: :hello,
5
+ actions: [
6
+ { name: 'test', from: :hello, to: :world }
7
+ ]
8
+ )
9
+
10
+ hoge = StateTransition::StateMachine.new(
11
+ initial: :hello,
12
+ actions: [
13
+ { name: 'hoge', from: :piyo, to: :hello },
14
+ { name: 'piyo', from: :piyo, to: :hello },
15
+ { name: 'test', from: :hello, to: :piyo }
16
+ ]
17
+ )
18
+
19
+ p state.current
20
+ p state.can_move?(:piyo)
21
+ p hoge.can_move?(:piyo)
22
+ p state.define_actions
23
+ p hoge.define_actions
24
+
25
+ state.add_action(name: :world, from: :world, to: :hello)
26
+ p state.define_actions
@@ -41,6 +41,29 @@ describe StateTransition do
41
41
  @state.have_state?(:third).should be_true
42
42
  end
43
43
 
44
+ it "should @state have three('first', 'second', 'third') state" do
45
+ @state.have_state?('first').should be_true
46
+ @state.have_state?('second').should be_true
47
+ @state.have_state?('third').should be_true
48
+ end
49
+
50
+ it "should @state have three method" do
51
+ @state.respond_to?(:two)
52
+ @state.respond_to?(:three)
53
+ @state.respond_to?(:reset)
54
+ end
55
+
56
+ it "should show defined actions" do
57
+ @state.define_actions.should == [:two, :three, :reset]
58
+ end
59
+
60
+ it "add action test" do
61
+ @state.add_action(name: 'test', from: 'hoge', to: 'piyo')
62
+ @state.have_state?(:hoge).should be_true
63
+ @state.have_state?(:piyo).should be_true
64
+ @state.respond_to?(:test)
65
+ end
66
+
44
67
  it "should first state is :first" do
45
68
  @state.current.should == :first
46
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_transition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - siman-man
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-20 00:00:00.000000000 Z
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - lib/state_transition/version.rb
71
71
  - sample/example.rb
72
72
  - sample/example2.rb
73
+ - sample/example3.rb
73
74
  - spec/spec_helper.rb
74
75
  - spec/state_transition_spec.rb
75
76
  - state_transition.gemspec
@@ -93,10 +94,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
94
  version: '0'
94
95
  requirements: []
95
96
  rubyforge_project:
96
- rubygems_version: 2.0.2
97
+ rubygems_version: 2.0.3
97
98
  signing_key:
98
99
  specification_version: 4
99
100
  summary: This is ruby state_machine.
100
101
  test_files:
101
102
  - spec/spec_helper.rb
102
103
  - spec/state_transition_spec.rb
104
+ has_rdoc: