state_transition 0.0.5 → 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 78d0702f02df6aa1b0e40037ef02821232e14b79
4
+ data.tar.gz: fb5c7bf4d43388b6b2b4b56694abf890018c8d46
5
+ SHA512:
6
+ metadata.gz: 94520dc57416158cdb898aecb0c547f78b8867d537ab50bbaf27518bef873453c2e3c9c64b507a1c2f05dfac3d3b2326a3c1e44337882b78b8a798208d3170ee
7
+ data.tar.gz: 1a797f74173b31d1908ba09622ef2d8423c23c3c421b5f13186cb6019f90647509187a33c1c7143cfd5092d9fffdd104c39e7e0d590af2e6830dbdb5d7e3f95f
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # StateTransition
2
2
 
3
3
  https://github.com/jakesgordon/javascript-state-machine
4
- これが便利だったのでRuby verを作ってみた。
4
+ This program is very useful. so I created ruby ver.
5
+
6
+ version 0.1.0 is active only upper 2.1.0.
5
7
 
6
8
  ## Installation
7
9
 
@@ -1,13 +1,14 @@
1
1
  require "state_transition/version"
2
+ require "state_transition/deep_copy"
2
3
 
3
4
  module StateTransition
4
5
  class StateMachine
6
+ using DeepCopy
7
+
5
8
  attr_reader :current, :define_actions
6
- attr_accessor :state_list
7
9
 
8
10
  def initialize(data = nil)
9
- @state_list = []
10
- @state_graph = Hash.new{|hash, key| hash[key] = []}
11
+ @state_list = Hash.new{|hash, key| hash[key] = {}}
11
12
  @callbacks = {}
12
13
  @define_actions = []
13
14
 
@@ -17,50 +18,49 @@ module StateTransition
17
18
  raise StandardError, "StateMachine: Not define first state."
18
19
  end
19
20
 
20
- set_state(data[:initial])
21
+ @current = data[:initial].to_sym
21
22
 
22
23
  create_move_action(data[:actions] || [])
23
24
  create_callbacks(data[:callbacks] || [])
24
25
  end
25
26
 
26
27
  def initialize_copy(org)
27
- @callbacks = @callbacks.dup
28
- @state_list = @state_list.dup
29
- @state_graph = @state_graph.dup
30
- @define_actions = @define_actions.dup
28
+ @callbacks = @callbacks.deep_copy
29
+ @state_list = @state_list.deep_copy
30
+ @define_actions = @define_actions.deep_copy
31
31
  end
32
32
 
33
33
  def have_state?(state)
34
- @state_list.include?(state.to_sym)
34
+ @state_list.keys.include?(state.to_sym)
35
35
  end
36
36
 
37
- def add_action(action)
37
+ def add_move_action(action)
38
38
  create_move_action([action])
39
39
  end
40
40
 
41
41
  def create_move_action(actions)
42
42
  actions.each do |action|
43
- name = action[:name].to_sym
44
- from = action[:from]
45
- to = action[:to].to_sym
43
+ key = action[:name].to_sym
44
+ froms = ( action[:from].is_a?(Array) )? action[:from] : [action[:from]]
45
+ to = action[:to].to_sym
46
46
 
47
- @define_actions << name.to_sym
47
+ @define_actions << key.to_sym
48
48
 
49
- set_state(from)
50
- set_state(to)
51
- create_edge(from, to)
49
+ set_state(froms: froms, key: key, to: to)
52
50
 
53
51
  StateMachine.class_eval do
54
- define_method name do
55
- if can_move?(to)
56
- before_func = ("before_" + to.to_s).to_sym
57
- after_func = ("after_" + to.to_s).to_sym
58
-
59
- @callbacks[before_func].call if @callbacks[before_func]
60
- @current = to
61
- @callbacks[after_func].call if @callbacks[after_func]
62
- else
63
- raise StandardError, "Can not move from '#{@current}' to '#{to}'!"
52
+ froms.each do |from|
53
+ define_method key do
54
+ if can_move?(to)
55
+ before_func = ("before_" + to.to_s).to_sym
56
+ after_func = ("after_" + to.to_s).to_sym
57
+
58
+ @callbacks[before_func].call if @callbacks[before_func]
59
+ @current = to
60
+ @callbacks[after_func].call if @callbacks[after_func]
61
+ else
62
+ raise StandardError, "Can not move from '#{@current}' to '#{to}'!"
63
+ end
64
64
  end
65
65
  end
66
66
  end
@@ -73,22 +73,16 @@ module StateTransition
73
73
  end
74
74
  end
75
75
 
76
- def can_move?(state)
77
- @state_graph[@current].include?(state)
76
+ def can_move?(point, from: @current, to: nil)
77
+ @state_list[from].values.include?(to||point)
78
78
  end
79
79
 
80
- def set_state(state)
81
- @state_list.push state.to_sym unless !state.respond_to?(:to_sym) || have_state?(state)
82
- end
80
+ def set_state(froms:, key:, to:)
81
+ froms = [froms] unless froms.is_a?(Array)
83
82
 
84
- def create_edge(from, to)
85
- if from.kind_of?(Array)
86
- from.each do |state|
87
- state = state.to_sym
88
- @state_graph[state].push to unless @state_graph[state].include?(to)
89
- end
90
- else
91
- @state_graph[from].push to
83
+ froms.each do |from|
84
+ @state_list[to] = {} unless @state_list.has_key?(to)
85
+ @state_list[from.to_sym][key] = to
92
86
  end
93
87
  end
94
88
  end
@@ -0,0 +1,19 @@
1
+ module StateTransition
2
+ module DeepCopy
3
+ refine Array do
4
+ def deep_copy
5
+ each_with_object([]) do |obj, arr|
6
+ arr.push(( obj.is_a?(Array) )? obj.deep_copy : obj)
7
+ end
8
+ end
9
+ end
10
+
11
+ refine Hash do
12
+ def deep_copy
13
+ each_with_object({}) do |(key,value), hash|
14
+ hash[key] = ( value.is_a?(Hash) )? value.deep_copy : value
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module StateTransition
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
data/sample/example2.rb CHANGED
@@ -39,6 +39,7 @@ end
39
39
 
40
40
  h = HelloWorld.new
41
41
 
42
+ p h.state.current
42
43
  h.state.one
43
44
  h.state.two
44
45
  h.state.three
data/sample/example3.rb CHANGED
@@ -22,5 +22,5 @@ p hoge.can_move?(:piyo)
22
22
  p state.define_actions
23
23
  p hoge.define_actions
24
24
 
25
- state.add_action(name: :world, from: :world, to: :hello)
25
+ state.add_move_action(name: :world, from: :world, to: :hello)
26
26
  p state.define_actions
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe StateTransition::DeepCopy do
4
+ using StateTransition::DeepCopy
5
+
6
+ describe "Array#deep_copy" do
7
+ before(:each) do
8
+ @orig_arr = [1,2,3, [4,5,6, [7,8,9]]]
9
+ end
10
+
11
+ it "double stair array" do
12
+ copy = @orig_arr.deep_copy
13
+ copy[-1][0] = 100
14
+ expect(@orig_arr[-1][0]).not_to eq(copy[-1][0])
15
+ end
16
+
17
+ it "triple stair array" do
18
+ copy = @orig_arr.deep_copy
19
+ copy[-1][-1][-1] = 100
20
+ expect(@orig_arr[-1][-1][-1]).not_to eq(copy[-1][-1][-1])
21
+ end
22
+ end
23
+
24
+ describe "Hash#deep_copy" do
25
+ before(:each) do
26
+ @orig_hash = { hoge: "hoge", piyo: { fuga: "fuga" }}
27
+ end
28
+
29
+ it "single stair hash" do
30
+ copy = @orig_hash.deep_copy
31
+ copy[:hoge] = "Ruby"
32
+ expect(@orig_hash[:hoge]).not_to eq(copy[:hoge])
33
+ end
34
+
35
+ it "double stair hash" do
36
+ copy = @orig_hash.deep_copy
37
+ copy[:piyo][:fuga] = "Ruby"
38
+ expect(@orig_hash[:piyo][:fuga]).not_to eq(copy[:piyo][:fuga])
39
+ end
40
+ end
41
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'state_transition'
2
+ require 'state_transition'
@@ -29,7 +29,7 @@ describe StateTransition do
29
29
  before_second: -> { @sequence << "before_second" },
30
30
  before_third: -> { @sequence << "before_third" },
31
31
  after_first: -> { @sequence << "after_first" },
32
- after_second: -> { @sequence << "after_second"; puts "Hello World!" },
32
+ after_second: -> { @sequence << "after_second" },
33
33
  after_third: -> { @sequence << "after_third" },
34
34
  }
35
35
  })
@@ -58,7 +58,7 @@ describe StateTransition do
58
58
  end
59
59
 
60
60
  it "add action test" do
61
- @state.add_action(name: 'test', from: 'hoge', to: 'piyo')
61
+ @state.add_move_action(name: 'test', from: 'hoge', to: 'piyo')
62
62
  @state.have_state?(:hoge).should be_true
63
63
  @state.have_state?(:piyo).should be_true
64
64
  @state.respond_to?(:test)
@@ -114,7 +114,7 @@ describe StateTransition do
114
114
  describe "about duplicated" do
115
115
  it "should not influence state change" do
116
116
  @copy = @state.dup
117
- @state.add_action(name: 'test', from: 'hoge', to: 'piyo')
117
+ @state.add_move_action(name: 'test', from: 'hoge', to: 'piyo')
118
118
 
119
119
  @copy.have_state?(:hoge).should be_false
120
120
  @copy.have_state?(:piyo).should be_false
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_transition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - siman-man
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-19 00:00:00.000000000 Z
11
+ date: 2014-04-27 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: This is ruby state_machine.
@@ -66,46 +59,49 @@ executables: []
66
59
  extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
69
- - .gitignore
70
- - .rspec
71
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
72
65
  - Gemfile
73
66
  - LICENSE.txt
74
67
  - README.md
75
68
  - Rakefile
76
69
  - lib/state_transition.rb
70
+ - lib/state_transition/deep_copy.rb
77
71
  - lib/state_transition/version.rb
78
72
  - sample/example.rb
79
73
  - sample/example2.rb
80
74
  - sample/example3.rb
75
+ - spec/deep_copy_spec.rb
81
76
  - spec/spec_helper.rb
82
77
  - spec/state_transition_spec.rb
83
78
  - state_transition.gemspec
84
79
  homepage: https://github.com/siman-man/state_transition
85
80
  licenses:
86
81
  - MIT
82
+ metadata: {}
87
83
  post_install_message:
88
84
  rdoc_options: []
89
85
  require_paths:
90
86
  - lib
91
87
  required_ruby_version: !ruby/object:Gem::Requirement
92
- none: false
93
88
  requirements:
94
- - - ! '>='
89
+ - - ">="
95
90
  - !ruby/object:Gem::Version
96
91
  version: '0'
97
92
  required_rubygems_version: !ruby/object:Gem::Requirement
98
- none: false
99
93
  requirements:
100
- - - ! '>='
94
+ - - ">="
101
95
  - !ruby/object:Gem::Version
102
96
  version: '0'
103
97
  requirements: []
104
98
  rubyforge_project:
105
- rubygems_version: 1.8.23
99
+ rubygems_version: 2.2.2
106
100
  signing_key:
107
- specification_version: 3
101
+ specification_version: 4
108
102
  summary: This is ruby state_machine.
109
103
  test_files:
104
+ - spec/deep_copy_spec.rb
110
105
  - spec/spec_helper.rb
111
106
  - spec/state_transition_spec.rb
107
+ has_rdoc: