mutator 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d69e47c148240cf6a7f6000f259980b0ef39caa6
4
- data.tar.gz: b1aa0ed144d14bd76ab91a1884f7d005519afbb3
3
+ metadata.gz: 0df3406ff9b500ebbec5794cc2c69271567cbb87
4
+ data.tar.gz: da50d33014377da65646a4b2af574361be31a575
5
5
  SHA512:
6
- metadata.gz: ae7fdbe701040f8116984b8ed5222231f97333dfa1b62068eec392b3aa9ea5f05cf40ed60d3a33b5ab3616f1175ef910e5b9d6ddeeb268b22806f4d7f2865fc5
7
- data.tar.gz: 94afab94f0b66e4b76d1c6f5086fb5080db2040eab6215dc55254ee0b365932a133f0fcdc7597497ba659eead9d185c19d6591e6c39ca117498a2028cc3572c9
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: [:signed] },
44
- { to: :delivered, from: [:sealed] },
45
- { to: :yours, from: [:delivered] }
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
@@ -3,4 +3,4 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -4,10 +4,10 @@ module Mutator
4
4
  @machine ||= machine_class.new(self)
5
5
  end
6
6
 
7
- def self.included(base)
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
- self.where(state: state)
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(self.class.name, false)
18
+ Mutator.const_get self.class.name, false
19
19
  end
20
20
  end
21
21
  end
@@ -2,7 +2,7 @@ module Mutator
2
2
  class Machine
3
3
  attr_reader :stateholder
4
4
 
5
- def initialize(stateholder)
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(options)
18
- options = extract(options)
19
- success, failure, transition = options.values
17
+ def transition options
18
+ options = extract options
19
+ transition, success, failure = options.values
20
20
 
21
21
  if transition.call
22
- success.call(transition)
22
+ success.call transition
23
23
  true
24
24
  else
25
- failure.call(transition)
25
+ failure.call transition
26
26
  false
27
27
  end
28
28
  end
29
29
 
30
30
  def self.states
31
- self.transitions.map do |transition|
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(options)
48
- to = options[:to]
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
- transition: Transition.new(
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
@@ -2,9 +2,10 @@ module Mutator
2
2
  class Transition
3
3
  attr_reader :to, :from, :machine
4
4
 
5
- def initialize(opts)
6
- require_parameters!(opts)
7
- @to, @from, @machine = opts[:to], opts[:from], opts[:machine]
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 ==(other)
23
+ def == other
23
24
  to == other.to && from == other.from && machine == other.machine
24
25
  end
25
26
 
26
- def eql?(other)
27
- public_send(:==, other)
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
@@ -1,3 +1,3 @@
1
1
  module Mutator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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 = %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.}
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 to' do
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 { subject.transition(to: to, failure: failure) }.to raise_error Exception
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 { subject.class.new(args) }.to raise_error ArgumentError, "must provide #{attr}"
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: [:initial_state], to: :second_state },
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.1.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-12 00:00:00.000000000 Z
11
+ date: 2014-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler