mutator 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 +24 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +127 -0
- data/Rakefile +6 -0
- data/lib/mutator/helpers.rb +21 -0
- data/lib/mutator/machine.rb +44 -0
- data/lib/mutator/transition.rb +25 -0
- data/lib/mutator/version.rb +3 -0
- data/lib/mutator.rb +4 -0
- data/mutator.gemspec +25 -0
- data/spec/spec_helper.rb +2 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 847fc513438a0e0cc7e5b9843d93a38f99b7366d
|
4
|
+
data.tar.gz: 4da10747a40b0dad72f827f5d70a38b2042aeb11
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 23aee1ed4688d2df18d93766cd72744a860168b1b4a70f9a2b60b11af8214c35de5f718f72b3963a58d2753000c3459d7e063868f370cbac8d03922219d55da3
|
7
|
+
data.tar.gz: 5cf8f1182e50133c991c72b962f6b805c8142f3197483cbc54be8e7a2bed5f801fe9887f65d5ce3f98cdb82b36c129688e04712a25604810c0490eedb1fee991
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.ruby-version
|
24
|
+
.ruby-gemset
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Eric Roberts
|
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,127 @@
|
|
1
|
+
# Yet another state machine
|
2
|
+
|
3
|
+
Ya, this is another state machine gem. Why? Well, among the major state machine gems I found, they didn't quite do what I was looking for. So I wrote my own. Plus, reinventing the wheel is fun!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mutator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mutator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### So how does this thing work?
|
22
|
+
|
23
|
+
Well, I'm going to assume you are adding a state machine to a Rails ActiveRecord class. It can be used for other things but I figure this is the most common example and I don't like writing more documentation than I have to.
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
class Wonder < ActiveRecord::Base
|
27
|
+
include Mutator::Helpers
|
28
|
+
|
29
|
+
def state
|
30
|
+
super || :signed # Set the default state. Can also be done in the database.
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module Mutator
|
35
|
+
class Wonder < Machine
|
36
|
+
def self.transitions
|
37
|
+
[
|
38
|
+
{ to: :sealed, from: [:signed] },
|
39
|
+
{ to: :delivered, from: [:sealed] },
|
40
|
+
{ to: :yours, from: [:delivered] }
|
41
|
+
]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### So how do I use it?
|
48
|
+
|
49
|
+
Transition looks like this:
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
wonder = Wonder.new
|
53
|
+
wonder.machine.transition to: :sealed #=> true
|
54
|
+
|
55
|
+
# Invalid Transition
|
56
|
+
wonder.machine.current_state #=> :sealed
|
57
|
+
wonder.machine.transition to: :yours #=> false
|
58
|
+
```
|
59
|
+
|
60
|
+
### How do I make it do things on success or failure?
|
61
|
+
|
62
|
+
Good question. Most state machines have you define callbacks. I wanted to do this one a bit differently. Whenever you call a transition, you are able to tell it what to do on success and failure. You do this like so:
|
63
|
+
|
64
|
+
``` ruby
|
65
|
+
wonder.machine.current_state #=> :sealed
|
66
|
+
|
67
|
+
success = lambda { |t| puts "delivered!" }
|
68
|
+
failure = lambda { |t| puts "failed :(" }
|
69
|
+
|
70
|
+
wonder.machine.transition to: :delivered,
|
71
|
+
success: success,
|
72
|
+
failure: failure
|
73
|
+
|
74
|
+
#=> "delivered!"
|
75
|
+
#=> true
|
76
|
+
|
77
|
+
# Invalid Transition
|
78
|
+
wonder.machine.transition to: :yours,
|
79
|
+
success: success,
|
80
|
+
failure: failure
|
81
|
+
|
82
|
+
#=> "failed :("
|
83
|
+
#=> false
|
84
|
+
```
|
85
|
+
|
86
|
+
The lambdas will be called and passed the transition object. The transition object contains the state you are transitioning to, the state you are transitioning from, the machine, and the stateholder.
|
87
|
+
|
88
|
+
### OK, but what if I really want to do the same thing on every transition?
|
89
|
+
|
90
|
+
I bet back at the part where I defined `Mutator::Wonder`, you were thinking, "why the hell would you define a whole other class just to define your transitions?". I could have just always included an instance of Machine instead of the subclassed version.
|
91
|
+
|
92
|
+
This is a thing I'm still debating. The idea for keeping them separate is that it gives you a place for more state related behaviour to go. For example, to answer the question at the top of this heading, you could define a method to run the transition:
|
93
|
+
|
94
|
+
``` ruby
|
95
|
+
module Mutator
|
96
|
+
class Wonder < Machine
|
97
|
+
[...]
|
98
|
+
|
99
|
+
# Save the activerecord class on success and raise exception on fail.
|
100
|
+
def deliver!
|
101
|
+
success = lambda { |t| t.stateholder.save! }
|
102
|
+
failure = lambda { |t| fail "Cannot transition to #{t.to} from #{t.from}" }
|
103
|
+
transition to: :delivered, success: success, failure: failure
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
Now you can always use `machine.deliver!` to do the transition to delivered.
|
110
|
+
|
111
|
+
### That's all folks!
|
112
|
+
|
113
|
+
That's more or less it. You may or may not like the way I've done this state machine, but it works for the purposes I need it for. I'm happy to discuss changes or reasoning behind certain things. There isn't a ton of code so feel free to poke around!
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
If your change is not just about fixing bugs, I would suggest opening an issue first. I think there are still improvements to be made, but I don't feel like making this a kitchen sink for behaviours provided by other state machines. If you think something should be added, an issue is the best way to let me know and we can discuss if functionality will be added to or changed.
|
118
|
+
|
119
|
+
That said, I'm pretty open to changing it, so let me know your thoughts!
|
120
|
+
|
121
|
+
Standard contributing instructions: (I have no idea if this is useful... I just always leave it here)
|
122
|
+
|
123
|
+
1. Fork it ( https://github.com/ericroberts/mutator/fork )
|
124
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
125
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
126
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
127
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Mutator
|
2
|
+
module Helpers
|
3
|
+
def machine
|
4
|
+
@machine ||= machine_class.new(self)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
"Machines::#{base.name}".constantize.states.each do |state|
|
9
|
+
base.send(:define_singleton_method, state) do
|
10
|
+
self.where(state: state)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def machine_class
|
18
|
+
"Machines::#{self.class.name}".constantize
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Mutator
|
2
|
+
class Machine
|
3
|
+
attr_reader :stateholder
|
4
|
+
|
5
|
+
def initialize(stateholder)
|
6
|
+
@stateholder = stateholder
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
self.class.states.include? current_state
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_state
|
14
|
+
stateholder.state
|
15
|
+
end
|
16
|
+
|
17
|
+
def transition(to:, success: lambda { |_transition| }, failure: lambda { |_transition| })
|
18
|
+
transition = Transition.new(to: to, from: current_state, machine: self)
|
19
|
+
|
20
|
+
if transition.valid?
|
21
|
+
stateholder.state = to
|
22
|
+
success.call(transition)
|
23
|
+
true
|
24
|
+
else
|
25
|
+
failure.call(transition)
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.states
|
31
|
+
self.transitions.map do |t|
|
32
|
+
[t[:to], t[:from]]
|
33
|
+
end.flatten.uniq
|
34
|
+
end
|
35
|
+
|
36
|
+
def states
|
37
|
+
self.class.states
|
38
|
+
end
|
39
|
+
|
40
|
+
def transitions
|
41
|
+
self.class.transitions
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mutator
|
2
|
+
class Transition
|
3
|
+
attr_reader :to, :from, :machine
|
4
|
+
|
5
|
+
def initialize(to:, from:, machine:)
|
6
|
+
@to, @from, @machine = to, from, machine
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
transitions.present?
|
11
|
+
end
|
12
|
+
|
13
|
+
def stateholder
|
14
|
+
machine.stateholder
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def transitions
|
20
|
+
machine.transitions.select do |t|
|
21
|
+
t[:to] == to && t[:from].include?(from)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/mutator.rb
ADDED
data/mutator.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mutator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mutator"
|
8
|
+
spec.version = Mutator::VERSION
|
9
|
+
spec.authors = ["Eric Roberts"]
|
10
|
+
spec.email = ["ericroberts@gmail.com"]
|
11
|
+
spec.summary = %q{Mutator is just another state machine gem.}
|
12
|
+
spec.description = %q{Yet another state machine gem. I didn't find one I liked, so I made one. I probably didn't look hard enough.}
|
13
|
+
spec.homepage = "https://github.com/ericroberts/mutator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "coveralls"
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mutator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Roberts
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-09 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
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: Yet another state machine gem. I didn't find one I liked, so I made one.
|
70
|
+
I probably didn't look hard enough.
|
71
|
+
email:
|
72
|
+
- ericroberts@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/mutator.rb
|
85
|
+
- lib/mutator/helpers.rb
|
86
|
+
- lib/mutator/machine.rb
|
87
|
+
- lib/mutator/transition.rb
|
88
|
+
- lib/mutator/version.rb
|
89
|
+
- mutator.gemspec
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
homepage: https://github.com/ericroberts/mutator
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.2.2
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Mutator is just another state machine gem.
|
115
|
+
test_files:
|
116
|
+
- spec/spec_helper.rb
|