bstard 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTJmMDJmODEzNDNjOWNjODJlMDRmOWUyZWQ3N2I3NmY5NTA2ODk3Mw==
5
+ data.tar.gz: !binary |-
6
+ NjI3YmVkYzhkYWE5YTA1MGY3MGYxN2ZlMjliNDY1YzNiODlkZTg2Mw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ODM1MGQxYzkzNTQzYzkyNTEzYTE1MGY0YmI2MmY2OTUxMDUyOTg0YmU3MjM3
10
+ NWVjM2E4YjVlMTZlMjMyZmRhOTMxZmI4MTQ3Y2Q5M2IzMTRlMDcxNzU0YmVi
11
+ ZmM1ZTg4ZjQ3MzIzYjI5NTUzYWM3Zjc0ZGM3MjkxMTZiOTY1NTk=
12
+ data.tar.gz: !binary |-
13
+ ZGNlZDk4Njk5Zjg4OTkwMTg4Y2Q5ODI5Njc2YjZjNjgwNGQ4MTM2YjE2Mjkw
14
+ MTI2YmMxZTA4Y2NkNGE2NzkyZjg1MjI4MDdhNzgwNjMyOTViZmNiODg2OWQy
15
+ OWQzYzgxZTI4OWI1YjczMTI1OWIyYTZhZjE4NTg1NTI2ZGQ2NTc=
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bstard.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tony Headford
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # Bstard - A New State(sman) Machine
2
+
3
+ A small state machine library with a simple interface. Designed around the concept of aggregation rather than inheritance enabling its use wherever you need it without the need to derive from it or mix it in.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ``` ruby
10
+ gem 'bstard'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bstard
20
+
21
+ ## Usage
22
+
23
+ Use `Bstard.define` to describe your state machine
24
+
25
+ ``` ruby
26
+ machine = Bstard.define do |fsm|
27
+ fsm.initial :new
28
+ fsm.event :submit, :new => :submitted
29
+ fsm.event :delete, :submitted => :deleted
30
+ end
31
+ ```
32
+
33
+ This defines a state machine that:
34
+
35
+ - has __new__ __submitted__ and __deleted__ states
36
+ - responds to _submit_ and _delete_ events
37
+ - has an initial state of __new__
38
+ - transitions from __new__ to __submitted__ when triggered by the _submit_ event
39
+ - transitions from __submitted__ to __deleted__ when triggered by the _delete_ event
40
+
41
+ Pretty simple huh?
42
+
43
+ ### Initial State
44
+ Set the initial state of the machine with `initial`. If no initial state is set a default state `:uninitialized` is used.
45
+
46
+ ### Querying the State
47
+
48
+ The current state of the machine is accessed via the `current_state` method
49
+
50
+ ``` ruby
51
+ machine.current_state
52
+ #=> :new
53
+ ```
54
+
55
+ Helper methods are automatically generated for each state consisting of the state name appended with a question mark. This method returns true is the current state matches the queried state or false otherwise.
56
+
57
+ ``` ruby
58
+ machine.new?
59
+ #=> true
60
+ machine.submitted?
61
+ #=> false
62
+ machine.deleted?
63
+ #=> false
64
+ ```
65
+
66
+ ### Querying Transitions
67
+
68
+ Helper methods are dynamically generated to enable querying whether an event is valid for the current state. These methods are named using the event name prefixed with 'can\_' and suffixed with a question mark.
69
+
70
+ ```ruby
71
+ machine.current_state
72
+ #=> :new
73
+ machine.can_submit?
74
+ #=> true
75
+ machine.can_delete?
76
+ #=> false
77
+ ```
78
+
79
+ ### Triggering Events
80
+
81
+ Trigger events by sending a message to the state machine using the event name suffixed with an exclamation mark.
82
+
83
+ ```ruby
84
+ machine.submit!
85
+ #=> :submitted
86
+ machine.delete!
87
+ #=> :deleted
88
+ ```
89
+
90
+ ### Callbacks
91
+
92
+ Callbacks can be configured for particular events and state changes, or for _any_ event or state change.
93
+ Event callbacks are configured with `on`
94
+
95
+ ``` ruby
96
+ machine = Bstard.define do |fsm|
97
+ # ...
98
+ fsm.on :submit do |previous_state|
99
+ # code that needs to be run when :submit is triggered
100
+ end
101
+ end
102
+ ```
103
+
104
+ State change callbacks are configured with `when`
105
+
106
+ ``` ruby
107
+ machine = Bstard.define do |fsm|
108
+ # ...
109
+ fsm.when :submitted do |previous_state|
110
+ # code that needs to run when state changes to :submitted
111
+ end
112
+ end
113
+ ```
114
+
115
+ Use the symbol `:any` for event or state change callbacks that should by run when any event is triggered or after any state change.
116
+
117
+ ``` ruby
118
+ machine = Bstard.define do |fsm|
119
+ # ...
120
+ fsm.on :any do |previous_state|
121
+ # code that will run when any event is triggered
122
+ end
123
+ fsm.when :any do |previous_state|
124
+ # code that will run when after any state change
125
+ end
126
+ end
127
+ ```
128
+
129
+ e.g. Use `initial` and `when :any` to persist state
130
+
131
+ ``` ruby
132
+ class MyModel < ActiveRecord::Base
133
+ def make_draft
134
+ # do stuff
135
+ state.save!
136
+ end
137
+
138
+ private
139
+ def state
140
+ @state ||= Bstard.define do |fsm|
141
+ fsm.initial status
142
+ fsm.event :save, :new => :draft
143
+ fsm.event :approve, :draft => :approved
144
+ fsm.when :any do |prev_state|
145
+ status = fsm.current_state
146
+ end
147
+ end
148
+ end
149
+ end
150
+ ```
151
+
152
+ ### Exceptions
153
+
154
+ An `InvalidTransition` error will be raised if an event is triggered on a state that cannot transition on that event.
155
+
156
+
157
+ ## Development
158
+
159
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
160
+ To install this gem onto your local machine, run `bundle exec rake install`.
161
+ To run the tests, run `bundle exec rake test`.
162
+
163
+ ## Contributing
164
+
165
+ 1. Fork it ( https://github.com/[my-github-username]/bstard/fork )
166
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
167
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
168
+ 4. Push to the branch (`git push origin my-new-feature`)
169
+ 5. Create a new Pull Request
170
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = ["lib", "test"]
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bstard"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bstard.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bstard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bstard"
8
+ spec.version = Bstard::VERSION
9
+ spec.authors = ["Tony Headford"]
10
+ spec.email = ["tony@binarycircus.com"]
11
+
12
+ spec.summary = %q{A Lightweight State Machine}
13
+ spec.description = %q{Bstard is a New State(sman) Machine}
14
+ spec.homepage = "https://github.com/tonyheadford/bstard"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "minitest-reporters"
27
+ end
data/lib/bstard.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'bstard/version'
2
+ require 'bstard/fsm'
3
+ require 'bstard/errors'
4
+
5
+ module Bstard
6
+ def self.define
7
+ fsm = Fsm.new
8
+ yield fsm if block_given?
9
+ fsm
10
+ end
11
+ end
12
+
@@ -0,0 +1,4 @@
1
+ module Bstard
2
+ class InvalidEvent < ::ArgumentError; end
3
+ class InvalidTransition < ::ArgumentError; end
4
+ end
data/lib/bstard/fsm.rb ADDED
@@ -0,0 +1,86 @@
1
+ class Bstard::Fsm
2
+ attr_reader :current_state
3
+
4
+ def initialize()
5
+ @current_state = :uninitialized
6
+ @events = {}
7
+ @states = []
8
+ @event_callbacks = {}
9
+ @state_callbacks = {}
10
+ end
11
+
12
+ def initial(state)
13
+ @current_state = state
14
+ add_state(state)
15
+ end
16
+
17
+ def event(event_name, transitions)
18
+ evt = event_name.to_sym
19
+ t = @events.fetch(evt, {})
20
+ @events[evt] = t.merge(transitions)
21
+ transitions.each { |k,v| add_state(k); add_state(v) }
22
+ add_event_method(evt)
23
+ add_can_event_method(evt)
24
+ end
25
+
26
+ def on(event, &block)
27
+ callbacks = @event_callbacks.fetch(event.to_sym, [])
28
+ @event_callbacks[event.to_sym] = callbacks << block
29
+ end
30
+
31
+ def when(state, &block)
32
+ callbacks = @state_callbacks.fetch(state.to_sym, [])
33
+ @state_callbacks[state.to_sym] = callbacks << block
34
+ end
35
+
36
+ def events
37
+ @events.keys.sort
38
+ end
39
+
40
+ def states
41
+ @states.sort
42
+ end
43
+
44
+ private
45
+
46
+ def add_state(state)
47
+ s = state.to_sym
48
+ unless @states.include? s
49
+ @states << s
50
+ metaclass.send :define_method, "#{s.to_s}?" do
51
+ current_state == s
52
+ end
53
+ end
54
+ end
55
+
56
+ def add_event_method(evt)
57
+ unless self.respond_to? evt
58
+ metaclass.send :define_method, "#{evt.to_s}!" do
59
+ transitions = @events[evt]
60
+ raise Bstard::InvalidEvent.new("Unknown event <#{evt.to_s}>") if transitions.nil?
61
+ new_state = transitions[current_state]
62
+ raise Bstard::InvalidTransition.new("Cannot <#{evt.to_s}> from [#{current_state.to_s}]") if new_state.nil?
63
+ @event_callbacks.fetch(evt.to_sym, []).concat(@event_callbacks.fetch(:any, [])).each { |c| c.call(current_state) }
64
+ old_state = current_state
65
+ @current_state = new_state
66
+ @state_callbacks.fetch(current_state, []).concat(@state_callbacks.fetch(:any, [])).each { |c| c.call(old_state) }
67
+ current_state
68
+ end
69
+ end
70
+ end
71
+
72
+ def add_can_event_method(evt)
73
+ m = "can_#{evt.to_s}?"
74
+ unless self.respond_to? m
75
+ metaclass.send :define_method, m do
76
+ transitions = @events[evt]
77
+ transitions && transitions[current_state]
78
+ end
79
+ end
80
+ end
81
+
82
+ def metaclass
83
+ class << self; self; end
84
+ end
85
+ end
86
+
@@ -0,0 +1,4 @@
1
+ module Bstard
2
+ VERSION = '0.1.0'
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bstard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Headford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-08 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Bstard is a New State(sman) Machine
70
+ email:
71
+ - tony@binarycircus.com
72
+ executables:
73
+ - console
74
+ - setup
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .travis.yml
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - bstard.gemspec
88
+ - lib/bstard.rb
89
+ - lib/bstard/errors.rb
90
+ - lib/bstard/fsm.rb
91
+ - lib/bstard/version.rb
92
+ homepage: https://github.com/tonyheadford/bstard
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A Lightweight State Machine
116
+ test_files: []