mongoid_enumerable 0.2.2 → 0.3.1

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
  SHA256:
3
- metadata.gz: 2297bea78c60f9cab28d878252f29a80752d7a29b708ba9e11b3ac6d5a3cefca
4
- data.tar.gz: 619b26c22a62e2acbd6f84ad2623d62a7494048958c57ef9c01853e5526af274
3
+ metadata.gz: cc95ca01789205d6d562246b481decd47a4a9cc24f7737591f0a2ee6d0fed00b
4
+ data.tar.gz: 83d3e8f06edde0e37d9e212cf3a7727a6168130711ec8a4f52ba48344a81b883
5
5
  SHA512:
6
- metadata.gz: da92a24350c59ee86e9c6f5035b45e640b0e8207e64c4fe93f0491edf3383c3ff68bde374cea0881567caacfa3ba724290357c35f756c6e20975368db52f62f2
7
- data.tar.gz: 98a2761a3feaf2d62d8143327b935991e576457f764202daec1c907b6f35d622bb5411f181ffa4198a99f0b171ef748132a7adfb9b221e9bed6364fbf24d9170
6
+ metadata.gz: d7645e234a2157285ed33e4f8c06d04b3f956bc0d62305a0d6253a0fc88e3cd6d7c460702a007862348f69133eb3208d2204a5ffe1a074ae70042199bd0a13b2
7
+ data.tar.gz: 4b57b9eb7fe390848cdbb76dbbbf2848c247b597e223c94a8e3b77fb3db9cd9932ce56663be9fdcb555ca5cf014f77fef578b0b8dd81a0da619e40c849cf2320
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid_enumerable (0.2.2)
4
+ mongoid_enumerable (0.3.1)
5
5
  mongoid (>= 4.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -70,6 +70,57 @@ task.build_failed!
70
70
  task.deploy_running?
71
71
  task.deploy_failed!
72
72
  ```
73
+ #### Callbacks
74
+ ##### Before Change
75
+ You can define a `before_change` callback that runs before each change. If the method returns a falsey value (`nil` or `false`) then the change is be aborted.
76
+
77
+ The method must receive two parameters: the old and the new value, respectively.
78
+
79
+ Example:
80
+ ```ruby
81
+ class Task
82
+ include Mongoid::Document
83
+ include MongoidEnumerable
84
+
85
+ enumerable :status, %w(completed running failed waiting), default: "waiting", before_change: :can_status_change?
86
+
87
+ def can_status_change?(old_value, new_value)
88
+ new_value != "waiting"
89
+ end
90
+ end
91
+
92
+ task = Task.new
93
+ task.status # "waiting"
94
+
95
+ task.running!
96
+ task.status # "running"
97
+
98
+ task.waiting!
99
+ task.status # "running"
100
+ ```
101
+
102
+ ##### After Change
103
+ You can define an `after_change` callback that runs after each change. The method must receive two parameters: the old and the new value, respectively.
104
+
105
+ Example:
106
+ ```ruby
107
+ class Task
108
+ include Mongoid::Document
109
+ include MongoidEnumerable
110
+
111
+ enumerable :status, %w(completed running failed waiting),
112
+ default: "waiting",
113
+ after_change: :status_changed
114
+
115
+ def status_changed(old_value, new_value)
116
+ puts "Status changed from #{old_value} to #{new_value}."
117
+ end
118
+ end
119
+
120
+ task = Task.new
121
+ task.running!
122
+ # Console output: "Status changed from waiting to running."
123
+ ```
73
124
 
74
125
 
75
126
  ### Scopes/Criterias
@@ -85,7 +136,6 @@ Task.build_waiting # Returns all tasks with build waiting status
85
136
  Task.deploy_running # Returns all tasks with deploy running status
86
137
  ```
87
138
 
88
-
89
139
  ## Development
90
140
 
91
141
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,33 +2,83 @@ require "mongoid_enumerable/version"
2
2
  require "mongoid"
3
3
 
4
4
  module MongoidEnumerable
5
- def self.included(base)
6
- base.class.redefine_method :enumerable do |field_name, values, options = {}|
7
- field_name = String(field_name)
8
- values.collect!(&:to_s)
5
+ def self.included(model)
6
+ define_enumerable_methods(model)
7
+ end
8
+
9
+ private
10
+
11
+ def self.define_enumerable_methods(model)
12
+ model.define_singleton_method :enumerable do |field_name, values, options = {}|
13
+ MongoidEnumerable.define_enumerable_method(
14
+ model: model,
15
+ field_name: String(field_name),
16
+ values: values.collect(&:to_s),
17
+ default: options.fetch(:default, values.first),
18
+ prefix: options.fetch(:prefix, nil),
19
+ before_change: options[:before_change],
20
+ after_change: options[:after_change]
21
+ )
22
+ end
23
+ end
9
24
 
10
- prefix = options.fetch(:prefix, "")
11
- default = options.fetch(:default, values.first)
25
+ def self.define_enumerable_method(model:, field_name:, values:, default:, prefix:, before_change:, after_change:)
26
+ model.field(field_name, type: String, default: default)
12
27
 
13
- field(field_name, type: String, default: default)
28
+ values.each do |value|
29
+ define_value_methods(
30
+ model: model,
31
+ value: value,
32
+ field_name: field_name,
33
+ prefix: prefix,
34
+ before_change: before_change,
35
+ after_change: after_change
36
+ )
37
+ end
14
38
 
15
- values.each do |value|
16
- method_name = "#{prefix}#{value}"
39
+ model.define_singleton_method("all_#{field_name}") do
40
+ values
41
+ end
42
+ end
17
43
 
18
- define_method("#{method_name}!") do
19
- update!(field_name => value)
20
- end
44
+ def self.define_value_methods(model:, value:, field_name:, prefix:, before_change:, after_change:)
45
+ method_name = "#{prefix}#{value}"
21
46
 
22
- define_method("#{method_name}?") do
23
- send(field_name) == value
24
- end
47
+ define_method("#{method_name}!") do
48
+ value_before = send(field_name)
49
+ value_after = value
25
50
 
26
- scope value, -> { where(field_name => value) }
27
- end
51
+ callback_result = run_callback(model: model, callback_method_name: before_change,
52
+ value_before: value_before, value_after: value_after)
28
53
 
29
- base.define_singleton_method("all_#{field_name}") do
30
- values
54
+ if callback_result
55
+ update!(field_name => value)
56
+ run_callback(model: model, callback_method_name: after_change,
57
+ value_before: value_before, value_after: value_after)
31
58
  end
32
59
  end
60
+
61
+ define_method("#{method_name}?") do
62
+ send(field_name) == value
63
+ end
64
+
65
+ model.scope value, -> { where(field_name => value) }
66
+ end
67
+
68
+ def run_callback(model:, callback_method_name:, value_before:, value_after:)
69
+ if callback_method_name
70
+ callback_method = method(callback_method_name)
71
+ validate_callback_method(method: callback_method)
72
+
73
+ callback_method.call(value_before, value_after)
74
+ else
75
+ true
76
+ end
77
+ end
78
+
79
+ def validate_callback_method(method:)
80
+ if method.arity != 2 && method.arity >= 0
81
+ raise "Method #{method.name} must receive two parameters: old_value and new_value"
82
+ end
33
83
  end
34
84
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidEnumerable
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_enumerable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Lise
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-28 00:00:00.000000000 Z
11
+ date: 2018-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler