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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7e1659eebf7238e7fbb2e3167d8b7c127a32cac
4
- data.tar.gz: d958c76251e9b1a808c0fe2fef285b4fb73ac611
3
+ metadata.gz: ab95124a02e8814b75708242f3352ce20b538c8a
4
+ data.tar.gz: bbc78c1d36964ff7096897e256d4122fa949d04b
5
5
  SHA512:
6
- metadata.gz: 8a93fbde64eca07e5584b5b3fa1ab67f32e9fc9186085e4c47d19e99e9c34a63b7575a76fdbac1b43b0ef192b069bd8c274e07cfbb3227564a2529ceb7782246
7
- data.tar.gz: 2ecd0a3bc38a722cec81539f6ad600874008c7b51af4993ab40522640fe49b6fa63df59526cbd3a9870da83db3c478a803d0c5e135d7b55124d2882250c08e8c
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
- (@transitions[from] ||= [])<< { to: to, guards: guards }
21
+ @transitions[from] << { to: to, guards: guards }
22
22
  end
23
23
 
24
24
  def next_id
@@ -1,3 +1,3 @@
1
1
  module RailsMachine
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/rails_machine.rb CHANGED
@@ -1,40 +1,40 @@
1
1
  require 'rails_machine/configuration'
2
2
 
3
3
  module RailsMachine
4
- extend ActiveSupport::Concern
4
+ extend ActiveSupport::Concern
5
5
 
6
- included do
7
- cattr_accessor :transitions
6
+ included do
7
+ cattr_accessor :transitions
8
8
 
9
- validate :allowed_transition, if: :state_changed?
10
- end
9
+ validate :allowed_transition, if: :state_changed?
10
+ end
11
11
 
12
- def allowed_transition
13
- from = self.state_was.to_sym
14
- to = self.state.to_sym
12
+ def allowed_transition
13
+ from = self.state_was.to_sym
14
+ to = self.state.to_sym
15
15
 
16
- transitions = (transitions_for(from) + transitions_for(:any)).select { |t| t[:to] == to || t[:to] == :any }
17
- errors.add(:state, :transition_not_found) if transitions.empty?
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
- errors.add(:state, :guard_failed) if transitions.none? { |t| t[:guards].all? { |guard| guard.call(self) } }
20
- end
19
+ errors.add(:state, :guard_failed) if transitions.none? { |t| t[:guards].all? { |guard| guard.call(self) } }
20
+ end
21
21
 
22
- module ClassMethods
23
- def rails_machine(&blk)
24
- raise ArgumentError unless block_given?
22
+ module ClassMethods
23
+ def rails_machine(column: :state, &blk)
24
+ raise ArgumentError unless block_given?
25
25
 
26
- configuration = Configuration.new
27
- configuration.run(&blk)
26
+ configuration = Configuration.new
27
+ configuration.run(&blk)
28
28
 
29
- self.transitions = configuration.transitions
29
+ self.transitions = configuration.transitions
30
30
 
31
- enum state: Hash[configuration.states]
32
- end
33
- end
31
+ enum column => Hash[configuration.states]
32
+ end
33
+ end
34
34
 
35
- protected
35
+ protected
36
36
 
37
- def transitions_for(action)
38
- self.class.transitions[action] || []
39
- end
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.2
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-06-27 00:00:00.000000000 Z
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: