rails_machine 0.0.4 → 0.0.5

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: ab95124a02e8814b75708242f3352ce20b538c8a
4
- data.tar.gz: bbc78c1d36964ff7096897e256d4122fa949d04b
3
+ metadata.gz: 3647b44bf67e5cfffd7975958f2bf14279670074
4
+ data.tar.gz: 824ed3eb69d1cf170a461c03edde88526f362bee
5
5
  SHA512:
6
- metadata.gz: fd2db2e2af3ec3fab12c0c875e7e6c3e28f9993ca6a9ee8974599f703f1eb14333b5c12a1b6360ccfeeab1b781430b59c4257f1cfac2fffd3aba376f0cd430cf
7
- data.tar.gz: a515c1f7775225807f714b1975f4caacc2701b2b2c6350e907be86d68ad3500ab388fa65163b8164528c8c1bcf7fc189a2be67972546d3e279082cc997997592
6
+ metadata.gz: 1980d66914255a7db65693de9dd405039ec4e104e4d03c90e68dfd58e4cf99189acb01cca77d496083d7a23e6af09de3fa11370b91d8e4c143d1513fa26d7b28
7
+ data.tar.gz: 3a9b91124e2339dc506121b81bbbd74a386b94995f6947a57f74597a18b9bca2189c948c0ba75a0247db0eb6b63bb364aa7c110bce763fb99395062bd186f4c3
@@ -1,10 +1,11 @@
1
1
  module RailsMachine
2
2
  class Configuration
3
3
 
4
- attr_reader :states, :transitions
4
+ attr_reader :states, :transitions, :init_states
5
5
 
6
6
  def initialize
7
7
  @states = []
8
+ @init_states = []
8
9
  @transitions = Hash.new{|hash,key| hash[key] = [] }
9
10
  end
10
11
 
@@ -13,6 +14,10 @@ module RailsMachine
13
14
  self.instance_eval(&blk)
14
15
  end
15
16
 
17
+ def init_state(state)
18
+ @init_states << state
19
+ end
20
+
16
21
  def state(name, id: next_id)
17
22
  @states << [name, id]
18
23
  end
@@ -1,3 +1,3 @@
1
1
  module RailsMachine
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/rails_machine.rb CHANGED
@@ -6,10 +6,30 @@ module RailsMachine
6
6
  included do
7
7
  cattr_accessor :transitions
8
8
 
9
- validate :allowed_transition, if: :state_changed?
9
+ validate :allowed_state, if: :state_changed?
10
+ private
11
+ cattr_accessor :init_states
10
12
  end
11
13
 
12
- def allowed_transition
14
+ def allowed_state
15
+ if self.new_record?
16
+ validate_init_state
17
+ else
18
+ validate_transition
19
+ end
20
+ end
21
+
22
+ def validate_init_state
23
+ unless valid_init_state
24
+ errors.add(:state,:invalid_init_state)
25
+ end
26
+ end
27
+
28
+ def valid_init_state
29
+ init_states.empty? || init_states.include?(self.state.to_sym)
30
+ end
31
+
32
+ def validate_transition
13
33
  from = self.state_was.to_sym
14
34
  to = self.state.to_sym
15
35
 
@@ -27,7 +47,7 @@ module RailsMachine
27
47
  configuration.run(&blk)
28
48
 
29
49
  self.transitions = configuration.transitions
30
-
50
+ self.init_states = configuration.init_states
31
51
  enum column => Hash[configuration.states]
32
52
  end
33
53
  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
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Łuszczek
@@ -9,50 +9,50 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-07 00:00:00.000000000 Z
12
+ date: 2014-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '4.1'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '4.1'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: sqlite3
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
34
+ version: '1.3'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '1.3'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec-rails
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '3.0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: '3.0'
56
56
  description: Simple implementation of state machine for Rails using enums.
57
57
  email:
58
58
  - grzegorz@piklus.pl
@@ -93,3 +93,4 @@ signing_key:
93
93
  specification_version: 4
94
94
  summary: State machine for Rails.
95
95
  test_files: []
96
+ has_rdoc: