tiny_state_machine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3e73ac446ec51e6ca9fda676416868b29f4250a1
4
+ data.tar.gz: a674b37787627af75f5cd9dac8736d5200db6599
5
+ SHA512:
6
+ metadata.gz: d9ec2246715eb7890ca29417de863c94da29fd022a73a4bb0be3d1d73a40fae3c46f9ab0a46edd0aa6a5bd1fef734230f5ba06f066deac5cfb196730ce2aa8d9
7
+ data.tar.gz: 317ab0c573459371109fef951c6bb3b57ead149fbe474a780d2256658912719a2cb8907b1d7b98e9a08850f974421d5145ec7ec5ff69b911bcbc3e0d313c6542
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tiny_state_machine.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Geoffroy Montel
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,69 @@
1
+ # TinyStateMachine
2
+
3
+ Yet another state machine for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tiny_state_machine'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tiny_state_machine
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ sm = TinyStateMachine::Machine.new :in_store do |sm|
23
+ sm.event :buy, :in_store => :new
24
+ sm.event :use, :new => :used
25
+ sm.event :use, :used => :used
26
+ sm.event :break, :used => :broken
27
+ sm.event :fix, :broken => :used
28
+ sm.on_state_change do |event, from_state, to_state|
29
+ puts "new state = #{to_state}"
30
+ end
31
+ end
32
+
33
+ sm.trigger(:buy)
34
+ # new state = new
35
+ # => :new
36
+
37
+ sm.trigger(:buy)
38
+ # TinyStateMachine::InvalidEvent: Invalid event 'buy' from state 'new'
39
+
40
+ sm.trigger(:use)
41
+ # new state = used
42
+ # => :used
43
+
44
+ sm.state
45
+ # => :used
46
+
47
+ sm.trigger(:use)
48
+ # new state = used
49
+ # => :used
50
+ ```
51
+
52
+ ## Tests
53
+
54
+ ```bash
55
+ bundle install # to install rspec
56
+ bundle exec rspec # to run tests
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
66
+
67
+ ## Author
68
+ Copyright (c) 2014 Geoffroy Montel (@g_montel)
69
+ MIT License
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,3 @@
1
+ module TinyStateMachine
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,73 @@
1
+ require "tiny_state_machine/version"
2
+
3
+ module TinyStateMachine
4
+ # raised when an invalid event is triggered
5
+ class InvalidEvent < StandardError
6
+ end
7
+
8
+ # A tiny state machine
9
+ # @example
10
+ # sm = TinyStateMachine::Machine.new :in_store do |sm|
11
+ # sm.event :buy, :in_store => :new
12
+ # sm.event :use, :new => :used
13
+ # sm.event :use, :used => :used
14
+ # sm.event :break, :used => :broken
15
+ # sm.event :fix, :broken => :used
16
+ # sm.on_state_change do |event, from_state, to_state|
17
+ # puts "new state = #{to_state}"
18
+ # end
19
+ # end
20
+
21
+ class Machine
22
+ # @return [String or Symbol] current state of the state machine.
23
+ attr_reader :state
24
+
25
+ # Construct a tiny state machine with an initial state.
26
+ #
27
+ # @param initial_state [String or Symbol] state the machine is in after initialization
28
+ # @yield [self] yields the machine for easy definition of states
29
+ # @yieldparam [Machine] self
30
+ def initialize(initial_state)
31
+ @state = initial_state
32
+ @events = Array.new
33
+ @callback_block = nil
34
+ if block_given?
35
+ yield self
36
+ end
37
+ end
38
+
39
+ # declare events
40
+ #
41
+ # @param event [String or Symbol] state name
42
+ # @param hash [Hash] a Hash in the form : old_state => new_state
43
+ def event(event, hash)
44
+ hash.each do |from, to|
45
+ @events << { event: event, from: from, to: to }
46
+ end
47
+ end
48
+
49
+ # trigger an event.
50
+ # It will return the new state the machine is in
51
+ # and call the listener's block
52
+ #
53
+ # @see on_state_change
54
+ # @param event [String or Symbol] event to trigger
55
+ # @raise [InvalidEvent] if the event is invalid (incorrect state)
56
+ # @return the new state the machine is in
57
+ def trigger(event)
58
+ e = @events.find { |e| e[:event] == event && e[:from] == @state }
59
+ raise InvalidEvent, "Invalid event '#{event}' from state '#{@state}'" if e.nil?
60
+ old_state = @state
61
+ @state = e[:to]
62
+ @callback_block.call(event, old_state, @state) if @callback_block
63
+ @state
64
+ end
65
+
66
+ # register a listener to a state change.
67
+ #
68
+ # @param block [Proc] block that will be triggered
69
+ def on_state_change(&block)
70
+ @callback_block = block
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'tiny_state_machine'
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ module TinyStateMachine
4
+
5
+ describe Machine do
6
+ describe "#event" do
7
+ it "should register an event" do
8
+ sm = TinyStateMachine::Machine.new :in_store
9
+ expect { sm.event :buy, :in_store => :new }.not_to raise_error
10
+ end
11
+ end
12
+
13
+ describe "#on_state_change" do
14
+ it "should register a callback block" do
15
+ sm = TinyStateMachine::Machine.new :in_store
16
+ expect { sm.on_state_change do |event, from_state, to_state|
17
+ puts "new state = #{to_state}"
18
+ end }.not_to raise_error
19
+ end
20
+ end
21
+
22
+ describe "#trigger" do
23
+ it "should return the new state if the event is valid" do
24
+ sm = TinyStateMachine::Machine.new :in_store do |sm|
25
+ sm.event :buy, :in_store => :new
26
+ end
27
+ expect(sm.trigger(:buy)).to eql :new
28
+ end
29
+
30
+ it "should raise an InvalidEvent error if the event is unknown" do
31
+ sm = TinyStateMachine::Machine.new :in_store do |sm|
32
+ sm.event :buy, :in_store => :new
33
+ end
34
+ expect { sm.trigger(:use) }.to raise_error(InvalidEvent)
35
+ end
36
+
37
+ it "should update the state machine state" do
38
+ sm = TinyStateMachine::Machine.new :in_store do |sm|
39
+ sm.event :buy, :in_store => :new
40
+ end
41
+ sm.trigger(:buy)
42
+ expect(sm.state).to eql :new
43
+ end
44
+
45
+ it "should call the callback block" do
46
+ callback = double('callback', :change => true)
47
+
48
+ sm = TinyStateMachine::Machine.new :in_store do |sm|
49
+ sm.event :buy, :in_store => :new
50
+ sm.on_state_change do |event, from_state, to_state|
51
+ callback.change(event, from_state, to_state)
52
+ end
53
+ end
54
+
55
+ sm.trigger(:buy)
56
+ expect(callback).to have_received(:change).with(:buy, :in_store, :new)
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ 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 'tiny_state_machine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tiny_state_machine"
8
+ spec.version = TinyStateMachine::VERSION
9
+ spec.authors = ["Geoffroy Montel"]
10
+ spec.email = ["g_montel@yahoo.com"]
11
+ spec.description = "Tiny State Machine for Ruby"
12
+ spec.summary = "A tiny (few dozen lines) state machine with callback for Ruby"
13
+ spec.homepage = "https://github.com/geoffroymontel/tiny_state_machine"
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,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_state_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Geoffroy Montel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-26 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: Tiny State Machine for Ruby
56
+ email:
57
+ - g_montel@yahoo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/.DS_Store
69
+ - lib/tiny_state_machine.rb
70
+ - lib/tiny_state_machine/version.rb
71
+ - spec/spec_helper.rb
72
+ - spec/tiny_state_machine_spec.rb
73
+ - tiny_state_machine.gemspec
74
+ homepage: https://github.com/geoffroymontel/tiny_state_machine
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A tiny (few dozen lines) state machine with callback for Ruby
98
+ test_files:
99
+ - spec/spec_helper.rb
100
+ - spec/tiny_state_machine_spec.rb