rails_machine 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rails_machine/configuration.rb +3 -3
- data/lib/rails_machine/version.rb +1 -1
- data/lib/rails_machine.rb +25 -25
- metadata +16 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab95124a02e8814b75708242f3352ce20b538c8a
|
4
|
+
data.tar.gz: bbc78c1d36964ff7096897e256d4122fa949d04b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd2db2e2af3ec3fab12c0c875e7e6c3e28f9993ca6a9ee8974599f703f1eb14333b5c12a1b6360ccfeeab1b781430b59c4257f1cfac2fffd3aba376f0cd430cf
|
7
|
+
data.tar.gz: a515c1f7775225807f714b1975f4caacc2701b2b2c6350e907be86d68ad3500ab388fa65163b8164528c8c1bcf7fc189a2be67972546d3e279082cc997997592
|
@@ -4,8 +4,8 @@ module RailsMachine
|
|
4
4
|
attr_reader :states, :transitions
|
5
5
|
|
6
6
|
def initialize
|
7
|
-
@states
|
8
|
-
@transitions
|
7
|
+
@states = []
|
8
|
+
@transitions = Hash.new{|hash,key| hash[key] = [] }
|
9
9
|
end
|
10
10
|
|
11
11
|
# Just runs code
|
@@ -18,7 +18,7 @@ module RailsMachine
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def transition(from: :any, to: :any, guards: [])
|
21
|
-
|
21
|
+
@transitions[from] << { to: to, guards: guards }
|
22
22
|
end
|
23
23
|
|
24
24
|
def next_id
|
data/lib/rails_machine.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
require 'rails_machine/configuration'
|
2
2
|
|
3
3
|
module RailsMachine
|
4
|
-
|
4
|
+
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
included do
|
7
|
+
cattr_accessor :transitions
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
validate :allowed_transition, if: :state_changed?
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def allowed_transition
|
13
|
+
from = self.state_was.to_sym
|
14
|
+
to = self.state.to_sym
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
transitions = (transitions_for(from) + transitions_for(:any)).select { |t| t[:to] == to || t[:to] == :any }
|
17
|
+
return errors.add(:state, :transition_not_found) if transitions.empty?
|
18
18
|
|
19
|
-
|
20
|
-
|
19
|
+
errors.add(:state, :guard_failed) if transitions.none? { |t| t[:guards].all? { |guard| guard.call(self) } }
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
module ClassMethods
|
23
|
+
def rails_machine(column: :state, &blk)
|
24
|
+
raise ArgumentError unless block_given?
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
configuration = Configuration.new
|
27
|
+
configuration.run(&blk)
|
28
28
|
|
29
|
-
|
29
|
+
self.transitions = configuration.transitions
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
enum column => Hash[configuration.states]
|
32
|
+
end
|
33
|
+
end
|
34
34
|
|
35
|
-
|
35
|
+
protected
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
def transitions_for(action)
|
38
|
+
self.class.transitions[action] || []
|
39
|
+
end
|
40
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grzegorz Łuszczek
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-07-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -39,6 +39,20 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec-rails
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
42
56
|
description: Simple implementation of state machine for Rails using enums.
|
43
57
|
email:
|
44
58
|
- grzegorz@piklus.pl
|
@@ -79,4 +93,3 @@ signing_key:
|
|
79
93
|
specification_version: 4
|
80
94
|
summary: State machine for Rails.
|
81
95
|
test_files: []
|
82
|
-
has_rdoc:
|