mutator 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +19 -0
- data/README.md +12 -3
- data/Rakefile +1 -1
- data/lib/mutator/helpers.rb +3 -3
- data/lib/mutator/machine.rb +12 -17
- data/lib/mutator/transition.rb +8 -13
- data/lib/mutator/version.rb +1 -1
- data/mutator.gemspec +2 -2
- data/spec/lib/mutator/machine_spec.rb +10 -2
- data/spec/lib/mutator/transition_spec.rb +3 -1
- data/spec/support/test_classes.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0df3406ff9b500ebbec5794cc2c69271567cbb87
|
4
|
+
data.tar.gz: da50d33014377da65646a4b2af574361be31a575
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d7f981148e536e969f4e51ba1d5406c61e3949c678303ca08def21a7382891c49de38dd09c9880eeb466826169bcea7c12e0594235453e667e4c240a3c356d2
|
7
|
+
data.tar.gz: 3dac9b17bbc154dac16f4a7185b5762f6e2a68c0f5962e7e78817d95c6b71b1d59b47600ac2e6a2a3662b1710d4eed3cde49c39bd93f3ae66de8f6dfbf18165a
|
data/.hound.yml
CHANGED
@@ -3,3 +3,22 @@ StringLiterals:
|
|
3
3
|
|
4
4
|
AccessModifierIndentation:
|
5
5
|
EnforcedStyle: outdent
|
6
|
+
|
7
|
+
MethodDefParentheses:
|
8
|
+
EnforcedStyle: require_no_parentheses
|
9
|
+
|
10
|
+
Documentation:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Lambda:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
DotPosition:
|
17
|
+
EnforcedStyle: trailing
|
18
|
+
|
19
|
+
Blocks:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
AllCops:
|
23
|
+
Exclude:
|
24
|
+
- 'mutator.gemspec'
|
data/README.md
CHANGED
@@ -40,15 +40,24 @@ module Mutator
|
|
40
40
|
class Wonder < Machine
|
41
41
|
def self.transitions
|
42
42
|
[
|
43
|
-
{ to: :sealed, from:
|
44
|
-
{ to: :delivered, from:
|
45
|
-
{ to: :yours, from:
|
43
|
+
{ to: :sealed, from: :signed },
|
44
|
+
{ to: :delivered, from: :sealed },
|
45
|
+
{ to: :yours, from: :delivered }
|
46
46
|
]
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
50
50
|
```
|
51
51
|
|
52
|
+
You can add multiple states to transition from like so:
|
53
|
+
|
54
|
+
``` ruby
|
55
|
+
def self.transitions
|
56
|
+
[
|
57
|
+
{ to: :yours, from: [:signed, :sealed, :delivered] }
|
58
|
+
]
|
59
|
+
```
|
60
|
+
|
52
61
|
### So how do I use it?
|
53
62
|
|
54
63
|
Transitions look like this:
|
data/Rakefile
CHANGED
data/lib/mutator/helpers.rb
CHANGED
@@ -4,10 +4,10 @@ module Mutator
|
|
4
4
|
@machine ||= machine_class.new(self)
|
5
5
|
end
|
6
6
|
|
7
|
-
def self.included
|
7
|
+
def self.included base
|
8
8
|
Mutator.const_get(base.name, false).states.each do |state|
|
9
9
|
base.send(:define_singleton_method, state) do
|
10
|
-
|
10
|
+
where state: state
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -15,7 +15,7 @@ module Mutator
|
|
15
15
|
protected
|
16
16
|
|
17
17
|
def machine_class
|
18
|
-
Mutator.const_get
|
18
|
+
Mutator.const_get self.class.name, false
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/mutator/machine.rb
CHANGED
@@ -2,7 +2,7 @@ module Mutator
|
|
2
2
|
class Machine
|
3
3
|
attr_reader :stateholder
|
4
4
|
|
5
|
-
def initialize
|
5
|
+
def initialize stateholder
|
6
6
|
@stateholder = stateholder
|
7
7
|
end
|
8
8
|
|
@@ -14,21 +14,21 @@ module Mutator
|
|
14
14
|
stateholder.state
|
15
15
|
end
|
16
16
|
|
17
|
-
def transition
|
18
|
-
options = extract
|
19
|
-
success, failure
|
17
|
+
def transition options
|
18
|
+
options = extract options
|
19
|
+
transition, success, failure = options.values
|
20
20
|
|
21
21
|
if transition.call
|
22
|
-
success.call
|
22
|
+
success.call transition
|
23
23
|
true
|
24
24
|
else
|
25
|
-
failure.call
|
25
|
+
failure.call transition
|
26
26
|
false
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
def self.states
|
31
|
-
|
31
|
+
transitions.map do |transition|
|
32
32
|
to, from = transition[:to], transition[:from]
|
33
33
|
[to, from]
|
34
34
|
end.flatten.uniq
|
@@ -44,19 +44,14 @@ module Mutator
|
|
44
44
|
|
45
45
|
protected
|
46
46
|
|
47
|
-
def extract
|
48
|
-
to = options
|
49
|
-
fail ArgumentError, 'must provide state to transition to' unless to
|
47
|
+
def extract options
|
48
|
+
to = options.fetch(:to)
|
50
49
|
|
51
50
|
{
|
51
|
+
transition: Transition.new(to: to, from: current_state, machine: self),
|
52
52
|
success: lambda { |_| },
|
53
|
-
failure: lambda { |_| }
|
54
|
-
|
55
|
-
to: to,
|
56
|
-
from: current_state,
|
57
|
-
machine: self
|
58
|
-
)
|
59
|
-
}.merge(options)
|
53
|
+
failure: lambda { |_| }
|
54
|
+
}.merge options
|
60
55
|
end
|
61
56
|
end
|
62
57
|
end
|
data/lib/mutator/transition.rb
CHANGED
@@ -2,9 +2,10 @@ module Mutator
|
|
2
2
|
class Transition
|
3
3
|
attr_reader :to, :from, :machine
|
4
4
|
|
5
|
-
def initialize
|
6
|
-
|
7
|
-
@
|
5
|
+
def initialize opts
|
6
|
+
@to = opts.fetch(:to)
|
7
|
+
@from = opts.fetch(:from)
|
8
|
+
@machine = opts.fetch(:machine)
|
8
9
|
end
|
9
10
|
|
10
11
|
def call
|
@@ -19,25 +20,19 @@ module Mutator
|
|
19
20
|
machine.stateholder
|
20
21
|
end
|
21
22
|
|
22
|
-
def ==
|
23
|
+
def == other
|
23
24
|
to == other.to && from == other.from && machine == other.machine
|
24
25
|
end
|
25
26
|
|
26
|
-
def eql?
|
27
|
-
public_send
|
27
|
+
def eql? other
|
28
|
+
public_send :==, other
|
28
29
|
end
|
29
30
|
|
30
31
|
protected
|
31
32
|
|
32
33
|
def transitions
|
33
34
|
machine.transitions.select do |transition|
|
34
|
-
transition[:to] == to && transition[:from].include?(from)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def require_parameters!(opts)
|
39
|
-
[:to, :from, :machine].each do |attr|
|
40
|
-
fail ArgumentError, "must provide #{attr}" unless opts[attr]
|
35
|
+
transition[:to] == to && Array(transition[:from]).include?(from)
|
41
36
|
end
|
42
37
|
end
|
43
38
|
end
|
data/lib/mutator/version.rb
CHANGED
data/mutator.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Mutator::VERSION
|
9
9
|
spec.authors = ["Eric Roberts"]
|
10
10
|
spec.email = ["ericroberts@gmail.com"]
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
11
|
+
spec.summary = "Mutator is just another state machine gem."
|
12
|
+
spec.description = "Yet another state machine gem. I didn't find one I liked, so I made one. I probably didn't look hard enough."
|
13
13
|
spec.homepage = "https://github.com/ericroberts/mutator"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -41,10 +41,16 @@ describe Mutator::Stateholder do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
context 'arguments' do
|
44
|
-
it 'should raise an exception if you do not provide
|
44
|
+
it 'should raise an exception if you do not provide any arguments' do
|
45
45
|
expect { subject.transition }.to raise_error ArgumentError
|
46
46
|
end
|
47
47
|
|
48
|
+
it 'should raise a specific key error if you provide an arg but not to' do
|
49
|
+
expect {
|
50
|
+
subject.transition success: 'something'
|
51
|
+
}.to raise_error KeyError
|
52
|
+
end
|
53
|
+
|
48
54
|
it 'should not raise if to is provided' do
|
49
55
|
expect { subject.transition to: :any_state }.to_not raise_error
|
50
56
|
end
|
@@ -138,7 +144,9 @@ describe Mutator::Stateholder do
|
|
138
144
|
before { expect(failure).to receive(:call).and_raise(Exception) }
|
139
145
|
|
140
146
|
it 'should raise an exception' do
|
141
|
-
expect {
|
147
|
+
expect {
|
148
|
+
subject.transition(to: to, failure: failure)
|
149
|
+
}.to raise_error Exception
|
142
150
|
end
|
143
151
|
end
|
144
152
|
end
|
@@ -18,7 +18,9 @@ describe Mutator::Transition do
|
|
18
18
|
it "should require you to pass #{attr}" do
|
19
19
|
args = { to: :something, from: :something, machine: :something }
|
20
20
|
args.delete attr
|
21
|
-
expect {
|
21
|
+
expect {
|
22
|
+
subject.class.new(args)
|
23
|
+
}.to raise_error KeyError
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
@@ -2,7 +2,7 @@ module Mutator
|
|
2
2
|
class Stateholder < Machine
|
3
3
|
def self.transitions
|
4
4
|
[
|
5
|
-
{ from:
|
5
|
+
{ from: :initial_state, to: :second_state },
|
6
6
|
{ from: [:second_state], to: :third_state }
|
7
7
|
]
|
8
8
|
end
|
@@ -18,6 +18,6 @@ class Stateholder
|
|
18
18
|
@state ||= :initial_state
|
19
19
|
end
|
20
20
|
|
21
|
-
def self.where
|
21
|
+
def self.where *_
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mutator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|