cequel_stateful_enum 1.0.0 → 1.1.0

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: e3a756dda6366a683accf33e3c848cfe9593b689
4
- data.tar.gz: 443654533689d282e506c30085f0d65e442b87d8
3
+ metadata.gz: 6c8d9928e15b1a39df8e5543bbfc523646dc053e
4
+ data.tar.gz: ca4affebdadb8ee1f33097065bbb8c91c137b63b
5
5
  SHA512:
6
- metadata.gz: 5a5fb6eedcbf1eef21bdcc3dea6dcab39f2eb3f51bde0452fcfe3ff5926b3fb0d2dbc65d17ad81cb95521682130efc7bb05607829c73f03966f722ec1ab4258d
7
- data.tar.gz: cda07ca03c09476e88aba710d33a291a42091d60595e89870e6f289513510052f48e66e35720f37bf9407885e96dd49d27943ea1183188ad67fe7a16bca7327e
6
+ metadata.gz: bcda6c95928be784f60b052049d4ab782f0d27b4076a6904a78761d3a7a46f1897b904264555afe4ea2fb02d7ae4a6a65fbd5da38a269fdbc8a5c387af2f6171
7
+ data.tar.gz: 8b6f6c5608fd130b0fb25b01446564d6289787ed9b19f55c9c1b7716c1ad1814e29eb74c11d0169f0d7ddc02eee10872956aff285c179379e8661cd80a7dc9cf
data/README.md CHANGED
@@ -100,6 +100,30 @@ event :assign do
100
100
  end
101
101
  ```
102
102
 
103
+ ### Saving the model
104
+
105
+ When firing the event model will be saved by default. To prevent saving use `save: false` attribute:
106
+ ```ruby
107
+ @bug.assign(save: false) # => true
108
+ ```
109
+ If model can't be saved (for example, it is in invalid state) and there was no `save: false` parameter passed, event will return `false`, but field will be changed and before callbacks will be fired. Can method does not check is the model valid:
110
+ ```ruby
111
+ @bug.subject = '' # Invalid subject
112
+ @bug.can_resolve? # => true
113
+ @bug.resolve # => false
114
+ @bug.updated_at # Not saved
115
+ @bug.status # => :resolved
116
+ @bug.resolved_at # Setted by after callback
117
+ ```
118
+
119
+ ### Danger mode
120
+
121
+ If you should control the bang and non-bang behavour with variable, you can use `danger: value` parameter, where `value` is `true` or `false`. Parameter is accepted on both event and can methods:
122
+ ```ruby
123
+ @bug.assign(danger: true) # Same as @bug.assign!
124
+ @bug.can_assign?(danger: true) # Will raise error if can't
125
+ ```
126
+
103
127
  ### Event Hooks
104
128
 
105
129
  You can define `before` and `after` event hooks inside of an `event` block as shown in the example above. Symbols and Proc objects are supported.
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'cequel_stateful_enum'
6
- spec.version = '1.0.0'
6
+ spec.version = '1.1.0'
7
7
  spec.authors = ['Xanders', 'Akira Matsuda']
8
8
  spec.email = ['necropolis@inbox.ru', 'ronnie@dio.jp']
9
9
 
@@ -31,20 +31,27 @@ module CequelStatefulEnum
31
31
 
32
32
  # defining event methods
33
33
  model.class_eval do
34
- define_method name do |danger = false|
35
- next false unless send(can, danger)
34
+ define_method name do |save: true, danger: false|
35
+ next false unless send(can, danger: danger)
36
36
  to = transitions[send(column).to_sym].first
37
37
  before.each { |callback| instance_eval(&callback) }
38
38
  send("#{column}=", to)
39
- after.each { |callback| instance_eval(&callback) }
40
- true
39
+ if save
40
+ if danger
41
+ save!
42
+ else
43
+ result = self.save
44
+ end
45
+ end
46
+ after.each { |callback| instance_eval(&callback) } unless result == false
47
+ result.nil? ? true : result
41
48
  end
42
49
 
43
- define_method "#{name}!" do
44
- send(name, true)
50
+ define_method "#{name}!" do |save: true|
51
+ send(name, save: save, danger: true)
45
52
  end
46
53
 
47
- define_method can do |danger = false|
54
+ define_method can do |danger: false|
48
55
  from = send(column).to_sym
49
56
  to, condition = transitions[from]
50
57
  if !to
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cequel_stateful_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xanders
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-03-20 00:00:00.000000000 Z
12
+ date: 2017-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cequel