evnt 3.0.2 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -381
- data/lib/evnt/command.rb +4 -4
- data/lib/evnt/validator.rb +162 -209
- data/lib/evnt/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d27e279061c1fe5534475dff8473ebd99be75598bc1d107388cba8a6c5acf8f
|
4
|
+
data.tar.gz: 794c7058bf25bdde40ade891d4fd8535ddddfbcf9b1c8baf5393bcfa9cf1c605
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 680ad444595d388a62fb4de5b8426487c5b7224775c249491ca5c9b15625ff148979ca7ae21391485524d58c617b7c2395072085ff9dc54c4b43bd61fb260ff6
|
7
|
+
data.tar.gz: 89cf7a8bca1840914aa2338c9cc4e9b9e31d19e1e0327169f0de227161e57d6eecd2d380316d5665db2ccdbdf7fe3034ce3531f528eb30c2cb579f8316014075
|
data/README.md
CHANGED
@@ -4,18 +4,13 @@ CQRS and Event Driven Development architecture for Ruby projects.
|
|
4
4
|
|
5
5
|
- [Installation](#installation)
|
6
6
|
- [Structure](#structure)
|
7
|
-
- [Command](
|
8
|
-
- [Event](
|
9
|
-
- [Handler](
|
10
|
-
- [Rails integration](
|
11
|
-
- [Generators integration](#generators-integration)
|
12
|
-
- [Command generators](#command-generators)
|
13
|
-
- [Event generators](#event-generators)
|
14
|
-
- [Handler generators](#handler-generators)
|
15
|
-
- [Manual integration](#manual-integration)
|
7
|
+
- [Command](https://github.com/ideonetwork/evnt/blob/master/doc/Command.md)
|
8
|
+
- [Event](https://github.com/ideonetwork/evnt/blob/master/doc/Event.md)
|
9
|
+
- [Handler](https://github.com/ideonetwork/evnt/blob/master/doc/Handler.md)
|
10
|
+
- [Rails integration](https://github.com/ideonetwork/evnt/blob/master/doc/RailsIntegration.md)
|
16
11
|
- [Development](#development)
|
17
12
|
|
18
|
-
Full documentation here: https://ideonetwork.github.io/evnt/
|
13
|
+
Full class documentation here: https://ideonetwork.github.io/evnt/
|
19
14
|
|
20
15
|
## Installation
|
21
16
|
|
@@ -35,377 +30,9 @@ gem 'evnt'
|
|
35
30
|
|
36
31
|
Evnt is developed to be used over all kinds of projects and frameworks (like Ruby on Rails or Sinatra), so it contains only three types of entities:
|
37
32
|
|
38
|
-
- Command
|
39
|
-
- Event
|
40
|
-
- Handler
|
41
|
-
|
42
|
-
### Command
|
43
|
-
|
44
|
-
Commands are used to run single tasks on the system. It's like a controller on an MVC architecture.
|
45
|
-
|
46
|
-
Every command has three steps to execute:
|
47
|
-
|
48
|
-
- The params normalization which normalize the parameters used to run the command.
|
49
|
-
- The params validation which validates the parameters used to run the command.
|
50
|
-
- The logic validation which checks the command can be executed in compliance with the system rules.
|
51
|
-
- The event intialization which initializes an event object used to save the command.
|
52
|
-
|
53
|
-
An example of command should be:
|
54
|
-
|
55
|
-
```ruby
|
56
|
-
class CreateOrderCommand < Evnt::Command
|
57
|
-
|
58
|
-
validates :user_id, type: :integer, presence: true
|
59
|
-
validates :product_id, type: :integer, presence: true
|
60
|
-
validates :quantity, type: :integer, presence: true
|
61
|
-
|
62
|
-
to_normalize_params do
|
63
|
-
params[:quantity] = params[:quantity].to_i
|
64
|
-
end
|
65
|
-
|
66
|
-
to_validate_params do
|
67
|
-
err 'Quantity should be positive' unless params[:quantity].positive?
|
68
|
-
end
|
69
|
-
|
70
|
-
to_validate_logic do
|
71
|
-
@user = User.find_by(id: params[:user_id])
|
72
|
-
@product = Product.find_by(id: params[:product_id])
|
73
|
-
|
74
|
-
# check user exist
|
75
|
-
unless @user
|
76
|
-
err 'The user does not exist'
|
77
|
-
break
|
78
|
-
end
|
79
|
-
|
80
|
-
# check product exist
|
81
|
-
unless @product
|
82
|
-
err 'The product does not exist'
|
83
|
-
break
|
84
|
-
end
|
85
|
-
|
86
|
-
# check quantity exist for the product
|
87
|
-
err 'The requested quantity is not available' if @product.quantity < params[:quantity]
|
88
|
-
|
89
|
-
# check user has money to buy the product
|
90
|
-
err 'You do not have enought money' if @user.money < @product.price * params[:quantity]
|
91
|
-
end
|
92
|
-
|
93
|
-
to_initialize_events do
|
94
|
-
# generate order id
|
95
|
-
order_id = SecureRandom.uuid
|
96
|
-
|
97
|
-
# initialize event
|
98
|
-
begin
|
99
|
-
CreateOrderEvent.new(
|
100
|
-
order_id: order_id,
|
101
|
-
user_id: @user.id,
|
102
|
-
product_id: @product.id,
|
103
|
-
quantity: params[:quantity],
|
104
|
-
_user: @user,
|
105
|
-
_product: @product
|
106
|
-
)
|
107
|
-
rescue
|
108
|
-
err 'Sorry, there was an error', code: 500
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
```
|
114
|
-
|
115
|
-
An example of command usage should be:
|
116
|
-
|
117
|
-
```ruby
|
118
|
-
command = CreateOrderCommand.new(
|
119
|
-
user_id: 128,
|
120
|
-
product_id: 534,
|
121
|
-
quantity: 10
|
122
|
-
)
|
123
|
-
|
124
|
-
if command.completed?
|
125
|
-
puts 'Command completed'
|
126
|
-
puts command.params # -> { user_id: 128, product_id: 534, quantity: 10 }
|
127
|
-
else
|
128
|
-
puts command.errors # array of hashes with a { message, code } structure
|
129
|
-
puts command.error_messages # array of error messages
|
130
|
-
puts command.error_codes # array of error codes
|
131
|
-
end
|
132
|
-
```
|
133
|
-
|
134
|
-
It's also possible to use err method inside the command to raise an exception with the option **exception: true**. An example of usage should be:
|
135
|
-
|
136
|
-
```ruby
|
137
|
-
begin
|
138
|
-
command = CreateOrderCommand.new(
|
139
|
-
user_id: 128,
|
140
|
-
product_id: 534,
|
141
|
-
quantity: 10,
|
142
|
-
_options: {
|
143
|
-
excption: true
|
144
|
-
}
|
145
|
-
)
|
146
|
-
rescue => e
|
147
|
-
puts e
|
148
|
-
end
|
149
|
-
```
|
150
|
-
|
151
|
-
### Event
|
152
|
-
|
153
|
-
Events are used to save on a persistent data structure what happends on the system.
|
154
|
-
|
155
|
-
Every event has three informations:
|
156
|
-
|
157
|
-
- The **name** is an unique identifier of the event.
|
158
|
-
- The **attributes** are the list of attributes required from the event to be saved.
|
159
|
-
- The **handlers** are a list of handler objects which will be notified when the event is completed.
|
160
|
-
|
161
|
-
Every event has also a single function used to write the event information on the data structure.
|
162
|
-
|
163
|
-
An example of event should be:
|
164
|
-
|
165
|
-
```ruby
|
166
|
-
class CreateOrderEvent < Evnt::Event
|
167
|
-
|
168
|
-
name_is :create_order
|
169
|
-
|
170
|
-
attributes_are :order_id, :user_id, :product_id, :quantity
|
171
|
-
|
172
|
-
handlers_are [
|
173
|
-
ProductHandler.new,
|
174
|
-
UserNotifierHandler.new
|
175
|
-
]
|
176
|
-
|
177
|
-
to_write_event do
|
178
|
-
# save event on database
|
179
|
-
Event.create(
|
180
|
-
name: name,
|
181
|
-
payload: payload
|
182
|
-
)
|
183
|
-
end
|
184
|
-
|
185
|
-
end
|
186
|
-
```
|
187
|
-
|
188
|
-
An example of event usage should be:
|
189
|
-
|
190
|
-
```ruby
|
191
|
-
event = CreateOrderEvent.new(
|
192
|
-
order_id: order_id,
|
193
|
-
user_id: @user.id,
|
194
|
-
product_id: @product.id,
|
195
|
-
quantity: params[:quantity]
|
196
|
-
)
|
197
|
-
|
198
|
-
puts event.name # -> :create_order
|
199
|
-
puts event.attributes # -> [:order_id, :user_id, :product_id, :quantity]
|
200
|
-
puts event.payload # -> { order_id: 1, user_id: 128, product_id: 534, quantity: 10, evnt: { timestamp: 2017010101, name: 'create_order' } }
|
201
|
-
```
|
202
|
-
|
203
|
-
The event payload should contain all event attributes and a reserved attributes "evnt" used to store the event timestamp and the event name.
|
204
|
-
|
205
|
-
It's also possible to give datas to the event without save them on the event payload, to do this you shuld only use a key with "_" as first character. An example should be:
|
206
|
-
|
207
|
-
```ruby
|
208
|
-
event = CreateOrderEvent.new(
|
209
|
-
order_id: order_id,
|
210
|
-
user_id: @user.id,
|
211
|
-
product_id: @product.id,
|
212
|
-
quantity: params[:quantity],
|
213
|
-
_total_price: params[:quantity] * @product.price
|
214
|
-
)
|
215
|
-
|
216
|
-
puts event.payload # -> { order_id: 1, user_id: 128, product_id: 534, quantity: 10, evnt: { timestamp: 2017010101, name: 'create_order' } }
|
217
|
-
puts event.extras # -> { total_price: 50 }
|
218
|
-
```
|
219
|
-
|
220
|
-
After the execution of the **to_write_event** block the event object should notify all its handlers.
|
221
|
-
|
222
|
-
Sometimes you need to reload an old event to notify handlers to re-build queries from events. To initialize a new event object with the payload of an old event you can pass the old event payload to the event constructor:
|
223
|
-
|
224
|
-
```ruby
|
225
|
-
events = Event.where(name: 'create_order')
|
226
|
-
reloaded_event = CreateOrderEvent.new(events.sample.payload)
|
227
|
-
|
228
|
-
puts reloaded_event.reloaded? # -> true
|
229
|
-
```
|
230
|
-
|
231
|
-
### Handler
|
232
|
-
|
233
|
-
Handlers are used to listen one or more events and run tasks after their execution.
|
234
|
-
|
235
|
-
Every handler event management has two steps to execute:
|
236
|
-
|
237
|
-
- The queries update which updates temporary data structures used to read datas.
|
238
|
-
- The manage event code which run other tasks like mailers, parallel executions ecc.
|
239
|
-
|
240
|
-
An example of handler shuould be:
|
241
|
-
|
242
|
-
```ruby
|
243
|
-
class ProductHandler < Evnt::Handler
|
244
|
-
|
245
|
-
on :create_order do
|
246
|
-
|
247
|
-
to_update_queries do
|
248
|
-
# update product quantity
|
249
|
-
product = event.extras[:product]
|
250
|
-
product.update(quantity: product.quantity - event.payload[:quantity])
|
251
|
-
end
|
252
|
-
|
253
|
-
to_manage_event do
|
254
|
-
# this block is called only for not reloaded events
|
255
|
-
end
|
256
|
-
|
257
|
-
end
|
258
|
-
|
259
|
-
end
|
260
|
-
```
|
261
|
-
|
262
|
-
The execution of **to_update_queries** block runs after every events initialization.
|
263
|
-
The execution of **to_manage_event** block runs only for not reloaded events initialization.
|
264
|
-
|
265
|
-
Sometimes you need to run some code to manage only reloaded events. To run code only for reloaded events you can use the **to_manage_reloaded_event** block:
|
266
|
-
|
267
|
-
```ruby
|
268
|
-
class ProductHandler < Evnt::Handler
|
269
|
-
|
270
|
-
on :create_order do
|
271
|
-
|
272
|
-
to_manage_reloaded_event do
|
273
|
-
# this block is called only for reloaded events
|
274
|
-
end
|
275
|
-
|
276
|
-
end
|
277
|
-
|
278
|
-
end
|
279
|
-
```
|
280
|
-
|
281
|
-
## Rails integration
|
282
|
-
|
283
|
-
Evnt can be used with Ruby on Rails to extends the MVC pattern.
|
284
|
-
|
285
|
-
### Generators integration
|
286
|
-
|
287
|
-
There's a simple generator that can be used to inizialize a Rails application with Evnt.
|
288
|
-
|
289
|
-
```shell
|
290
|
-
|
291
|
-
rails generate evnt:initializer
|
292
|
-
|
293
|
-
```
|
294
|
-
|
295
|
-
This command should:
|
296
|
-
|
297
|
-
- Create an **application_command.rb** on app/commands directory.
|
298
|
-
- Create an **application_event.rb** on app/events directory.
|
299
|
-
- Create an **application_handler.rb** on app/handlers directory.
|
300
|
-
- Create tests for the three new classes added on the project.
|
301
|
-
- Added app/commands, app/events and app/handlers on config.autoload_paths setting of the application.
|
302
|
-
|
303
|
-
#### Command generators
|
304
|
-
|
305
|
-
Usage:
|
306
|
-
|
307
|
-
```shell
|
308
|
-
|
309
|
-
rails generate evnt:command Authentication::LoginCommand email:string password:string ip_address:string
|
310
|
-
|
311
|
-
```
|
312
|
-
|
313
|
-
Output:
|
314
|
-
|
315
|
-
```ruby
|
316
|
-
# ./app/commands/authentication/login_command.rb
|
317
|
-
module Authentication
|
318
|
-
class LoginCommand < ApplicationCommand
|
319
|
-
|
320
|
-
validates :email, type: :string
|
321
|
-
|
322
|
-
validates :password, type: :string
|
323
|
-
|
324
|
-
validates :ip_address, type: :string
|
325
|
-
|
326
|
-
end
|
327
|
-
end
|
328
|
-
```
|
329
|
-
|
330
|
-
#### Event generators
|
331
|
-
|
332
|
-
Usage:
|
333
|
-
|
334
|
-
```shell
|
335
|
-
|
336
|
-
rails generate evnt:event Authentication::LoginEvent user_uuid ip_address
|
337
|
-
|
338
|
-
```
|
339
|
-
|
340
|
-
Output:
|
341
|
-
|
342
|
-
```ruby
|
343
|
-
# ./app/events/authentication/login_event.rb
|
344
|
-
module Authentication
|
345
|
-
class LoginEvent < ApplicationEvent
|
346
|
-
|
347
|
-
name_is :authentication_login_event
|
348
|
-
|
349
|
-
attributes_are :user_uuid, :ip_address
|
350
|
-
|
351
|
-
end
|
352
|
-
end
|
353
|
-
```
|
354
|
-
|
355
|
-
#### Handler generators
|
356
|
-
|
357
|
-
Usage:
|
358
|
-
|
359
|
-
```shell
|
360
|
-
|
361
|
-
rails generate evnt:handler AuthenticationHandler authentication_login_event authentication_signup_event
|
362
|
-
|
363
|
-
```
|
364
|
-
|
365
|
-
Output:
|
366
|
-
|
367
|
-
```ruby
|
368
|
-
# ./app/handlers/authentication_handler.rb
|
369
|
-
class AuthenticationHandler < ApplicationHandler
|
370
|
-
|
371
|
-
on :authentication_login_event do; end
|
372
|
-
|
373
|
-
on :authentication_signup_event do; end
|
374
|
-
|
375
|
-
end
|
376
|
-
```
|
377
|
-
|
378
|
-
### Manual integration
|
379
|
-
|
380
|
-
To use the gem with Rails you need to create three folders inside the ./app project's path:
|
381
|
-
|
382
|
-
- **./app/commands**
|
383
|
-
- **./app/events**
|
384
|
-
- **./app/handlers**
|
385
|
-
|
386
|
-
You also need to require all files from these folders. To do this you need to edit the ./config/application.rb file like this example:
|
387
|
-
|
388
|
-
```ruby
|
389
|
-
|
390
|
-
require_relative 'boot'
|
391
|
-
|
392
|
-
require 'rails/all'
|
393
|
-
|
394
|
-
# Require the gems listed in Gemfile, including any gems
|
395
|
-
# you've limited to :test, :development, or :production.
|
396
|
-
Bundler.require(*Rails.groups)
|
397
|
-
|
398
|
-
module MyApplicationName
|
399
|
-
class Application < Rails::Application
|
400
|
-
|
401
|
-
config.autoload_paths << Rails.root.join('app/commands')
|
402
|
-
config.autoload_paths << Rails.root.join('app/events')
|
403
|
-
config.autoload_paths << Rails.root.join('app/handlers')
|
404
|
-
|
405
|
-
end
|
406
|
-
end
|
407
|
-
|
408
|
-
```
|
33
|
+
- [Command](https://github.com/ideonetwork/evnt/blob/master/doc/Command.md)
|
34
|
+
- [Event](https://github.com/ideonetwork/evnt/blob/master/doc/Event.md)
|
35
|
+
- [Handler](https://github.com/ideonetwork/evnt/blob/master/doc/Handler.md)
|
409
36
|
|
410
37
|
## Development
|
411
38
|
|
data/lib/evnt/command.rb
CHANGED
@@ -136,13 +136,13 @@ module Evnt
|
|
136
136
|
return if self.class._validations.nil? || self.class._validations.empty?
|
137
137
|
|
138
138
|
self.class._validations.each do |val|
|
139
|
-
|
139
|
+
validator = Evnt::Validator.new(params[val[:param]], val[:options])
|
140
140
|
|
141
|
-
if
|
141
|
+
if validator.passed?
|
142
|
+
@params[val[:param]] = validator.value
|
143
|
+
else
|
142
144
|
err "#{val[:param].capitalize} value not accepted"
|
143
145
|
break
|
144
|
-
else
|
145
|
-
@params[val[:param]] = result
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
data/lib/evnt/validator.rb
CHANGED
@@ -7,233 +7,186 @@ module Evnt
|
|
7
7
|
##
|
8
8
|
class Validator
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
:ERROR
|
40
|
-
end
|
10
|
+
attr_reader :value
|
11
|
+
|
12
|
+
TYPES = %i[boolean string integer symbol float hash array
|
13
|
+
date datetime time].freeze
|
14
|
+
|
15
|
+
##
|
16
|
+
# The constructor is used to initialize a new validator.
|
17
|
+
#
|
18
|
+
# ==== Attributes
|
19
|
+
#
|
20
|
+
# * +value+ - The value that should be validated.
|
21
|
+
# * +options+ - The list of options for the validation.
|
22
|
+
#
|
23
|
+
# ==== Options
|
24
|
+
#
|
25
|
+
# * +presence+ - Boolean value used to not accept nil values.
|
26
|
+
# * +type+ - Symbol or String value used to set the type of the value.
|
27
|
+
# * +custom_options+ - Other options that can change for every type.
|
28
|
+
##
|
29
|
+
def initialize(value, options)
|
30
|
+
@value = value
|
31
|
+
|
32
|
+
@_options = options
|
33
|
+
@_result = true
|
34
|
+
|
35
|
+
_validates_presence if @_result
|
36
|
+
_validates_type if @_result
|
37
|
+
_validates_custom if @_result
|
38
|
+
end
|
41
39
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
# * +option_key+ - The key of the option that should be used.
|
49
|
-
# * +option_value+ - The value of the option that should be used.
|
50
|
-
##
|
51
|
-
def validate_option(param, option_key, option_value)
|
52
|
-
case option_key
|
53
|
-
when :type
|
54
|
-
validate_type(param, option_value)
|
55
|
-
when :presence
|
56
|
-
validate_presence(param, option_value)
|
57
|
-
when :blank
|
58
|
-
validate_blank(param, option_value)
|
59
|
-
else
|
60
|
-
raise 'Validator option not accepted'
|
61
|
-
end
|
62
|
-
rescue StandardError => e
|
63
|
-
puts e
|
64
|
-
:ERROR
|
65
|
-
end
|
40
|
+
##
|
41
|
+
# This function tells if the validation is passed or not.
|
42
|
+
##
|
43
|
+
def passed?
|
44
|
+
@_result
|
45
|
+
end
|
66
46
|
|
67
|
-
|
68
|
-
# This function validates the type of the parameter.
|
69
|
-
#
|
70
|
-
# ==== Attributes
|
71
|
-
#
|
72
|
-
# * +param+ - The parameter to be validated.
|
73
|
-
# * +value+ - The value of the type option that should be used.
|
74
|
-
##
|
75
|
-
def validate_type(param, value)
|
76
|
-
return param if param.nil?
|
77
|
-
|
78
|
-
if value.instance_of?(Symbol)
|
79
|
-
validate_type_general(param, value)
|
80
|
-
elsif value.instance_of?(String)
|
81
|
-
validate_type_custom(param, value)
|
82
|
-
else
|
83
|
-
raise 'Validator type option not accepted'
|
84
|
-
end
|
85
|
-
rescue StandardError => e
|
86
|
-
puts e
|
87
|
-
:ERROR
|
88
|
-
end
|
47
|
+
private
|
89
48
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
# ==== Attributes
|
95
|
-
#
|
96
|
-
# * +param+ - The parameter to be validated.
|
97
|
-
# * +value+ - The value of the presence option that should be used.
|
98
|
-
##
|
99
|
-
def validate_presence(param, value)
|
100
|
-
is_nil = param.nil?
|
101
|
-
result = value ? !is_nil : is_nil
|
102
|
-
return param if result
|
103
|
-
|
104
|
-
:ERROR
|
105
|
-
rescue StandardError => e
|
106
|
-
puts e
|
107
|
-
:ERROR
|
108
|
-
end
|
49
|
+
def _validates_presence
|
50
|
+
return if @_options[:presence].nil?
|
51
|
+
@_result = @_options[:presence] ? !@value.nil? : @value.nil?
|
52
|
+
end
|
109
53
|
|
110
|
-
|
111
|
-
|
112
|
-
# A parameter is blank when its value is nil, false, or empty.
|
113
|
-
#
|
114
|
-
# ==== Attributes
|
115
|
-
#
|
116
|
-
# * +param+ - The parameter to be validated.
|
117
|
-
# * +value+ - The value of the presence option that should be used.
|
118
|
-
##
|
119
|
-
def validate_blank(param, value)
|
120
|
-
blank = (!param || param.empty?)
|
121
|
-
result = value ? blank : !blank
|
122
|
-
return param if result
|
123
|
-
|
124
|
-
:ERROR
|
125
|
-
rescue StandardError => e
|
126
|
-
puts e
|
127
|
-
:ERROR
|
128
|
-
end
|
54
|
+
def _validates_type
|
55
|
+
return if @value.nil? || @_options[:type].nil?
|
129
56
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
##########################################################################
|
137
|
-
|
138
|
-
# This function validates a param type for general types.
|
139
|
-
def validate_type_general(param, value)
|
140
|
-
case value
|
141
|
-
when :boolean
|
142
|
-
validate_type_boolean(param)
|
143
|
-
when :string
|
144
|
-
validate_type_string(param)
|
145
|
-
when :integer
|
146
|
-
validate_type_integer(param)
|
147
|
-
when :symbol
|
148
|
-
validate_type_symbol(param)
|
149
|
-
when :float
|
150
|
-
validates_type_float(param)
|
151
|
-
when :hash
|
152
|
-
validates_type_hash(param)
|
153
|
-
when :array
|
154
|
-
validates_type_array(param)
|
155
|
-
when :date
|
156
|
-
validates_type_date(param)
|
157
|
-
when :datetime
|
158
|
-
validates_type_datetime(param)
|
159
|
-
else
|
160
|
-
raise 'Validator type option not accepted'
|
161
|
-
end
|
57
|
+
if @_options[:type].instance_of?(Symbol) && TYPES.include?(@_options[:type])
|
58
|
+
send("_validates_type_#{@_options[:type]}")
|
59
|
+
elsif @_options[:type].instance_of?(String)
|
60
|
+
_validates_type_custom
|
61
|
+
else
|
62
|
+
raise 'Validator type option not accepted'
|
162
63
|
end
|
64
|
+
end
|
163
65
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
66
|
+
def _validates_custom
|
67
|
+
_validates_global_in if @_result
|
68
|
+
_validates_global_out if @_result
|
69
|
+
|
70
|
+
case @_options[:type]
|
71
|
+
when :string
|
72
|
+
_validates_string_blank if @_result
|
73
|
+
_validates_string_length if @_result
|
74
|
+
when :integer
|
75
|
+
_validates_number_min if @_result
|
76
|
+
_validates_number_max if @_result
|
77
|
+
when :float
|
78
|
+
_validates_number_min if @_result
|
79
|
+
_validates_number_max if @_result
|
168
80
|
end
|
81
|
+
end
|
169
82
|
|
170
|
-
|
171
|
-
|
172
|
-
:ERROR
|
173
|
-
end
|
83
|
+
# Types validations:
|
84
|
+
##########################################################################
|
174
85
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
return false if [0, 'false'].include? param
|
179
|
-
:ERROR
|
180
|
-
end
|
86
|
+
def _validates_type_custom
|
87
|
+
@_result = @value.instance_of?(Object.const_get(@_options[:type]))
|
88
|
+
end
|
181
89
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
return param.to_i
|
186
|
-
rescue StandardError
|
187
|
-
return :ERROR
|
188
|
-
end
|
189
|
-
end
|
90
|
+
def _validates_type_symbol
|
91
|
+
@_result = @value.instance_of?(Symbol)
|
92
|
+
end
|
190
93
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
return param.to_f
|
195
|
-
rescue StandardError
|
196
|
-
return :ERROR
|
197
|
-
end
|
198
|
-
end
|
94
|
+
def _validates_type_hash
|
95
|
+
@_result = @value.instance_of?(Hash)
|
96
|
+
end
|
199
97
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
return param.to_s
|
204
|
-
rescue StandardError
|
205
|
-
return :ERROR
|
206
|
-
end
|
207
|
-
end
|
98
|
+
def _validates_type_array
|
99
|
+
@_result = @value.instance_of?(Array)
|
100
|
+
end
|
208
101
|
|
209
|
-
|
210
|
-
|
211
|
-
begin
|
212
|
-
return Date.parse(param)
|
213
|
-
rescue StandardError
|
214
|
-
return :ERROR
|
215
|
-
end
|
216
|
-
end
|
102
|
+
def _validates_type_boolean
|
103
|
+
return if @value.instance_of?(TrueClass) || @value.instance_of?(FalseClass)
|
217
104
|
|
218
|
-
|
219
|
-
|
220
|
-
begin
|
221
|
-
return DateTime.parse(param)
|
222
|
-
rescue StandardError
|
223
|
-
return :ERROR
|
224
|
-
end
|
225
|
-
end
|
105
|
+
@value = true && return if [1, 'true'].include?(@value)
|
106
|
+
@value = false && return if [0, 'false'].include?(@value)
|
226
107
|
|
227
|
-
|
228
|
-
|
229
|
-
:ERROR
|
230
|
-
end
|
108
|
+
@_result = false
|
109
|
+
end
|
231
110
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
111
|
+
def _validates_type_integer
|
112
|
+
return if @value.instance_of?(Integer)
|
113
|
+
@value = @value.to_i
|
114
|
+
rescue StandardError
|
115
|
+
@_result = false
|
116
|
+
end
|
117
|
+
|
118
|
+
def _validates_type_float
|
119
|
+
return if @value.instance_of?(Float)
|
120
|
+
@value = @value.to_f
|
121
|
+
rescue StandardError
|
122
|
+
@_result = false
|
123
|
+
end
|
124
|
+
|
125
|
+
def _validates_type_string
|
126
|
+
return if @value.instance_of?(String)
|
127
|
+
@value = @value.to_s
|
128
|
+
rescue StandardError
|
129
|
+
@_result = false
|
130
|
+
end
|
131
|
+
|
132
|
+
def _validates_type_date
|
133
|
+
return if @value.instance_of?(Date)
|
134
|
+
@value = Date.parse(@value)
|
135
|
+
rescue StandardError
|
136
|
+
@_result = false
|
137
|
+
end
|
138
|
+
|
139
|
+
def _validates_type_datetime
|
140
|
+
return if @value.instance_of?(DateTime)
|
141
|
+
@value = DateTime.parse(@value)
|
142
|
+
rescue StandardError
|
143
|
+
@_result = false
|
144
|
+
end
|
145
|
+
|
146
|
+
def _validates_type_time
|
147
|
+
return if @value.instance_of?(Time)
|
148
|
+
@value = Time.parse(@value)
|
149
|
+
rescue StandardError
|
150
|
+
@_result = false
|
151
|
+
end
|
152
|
+
|
153
|
+
# Global validations:
|
154
|
+
##########################################################################
|
155
|
+
|
156
|
+
def _validates_global_in
|
157
|
+
return if @value.nil? || @_options[:in].nil?
|
158
|
+
@_result = @_options[:in].include?(@value)
|
159
|
+
end
|
160
|
+
|
161
|
+
def _validates_global_out
|
162
|
+
return if @value.nil? || @_options[:out].nil?
|
163
|
+
@_result = !@_options[:out].include?(@value)
|
164
|
+
end
|
165
|
+
|
166
|
+
# String validations:
|
167
|
+
##########################################################################
|
168
|
+
|
169
|
+
def _validates_string_blank
|
170
|
+
return if @value.nil? || @_options[:blank].nil?
|
171
|
+
@_result = @_options[:blank] ? @value.empty? : !@value.empty?
|
172
|
+
end
|
173
|
+
|
174
|
+
def _validates_string_length
|
175
|
+
return if @value.nil? || @_options[:length].nil?
|
176
|
+
@_result = @value.length == @_options[:length]
|
177
|
+
end
|
178
|
+
|
179
|
+
# Number validations:
|
180
|
+
##########################################################################
|
181
|
+
|
182
|
+
def _validates_number_min
|
183
|
+
return if @value.nil? || @_options[:min].nil?
|
184
|
+
@_result = @value >= @_options[:min]
|
185
|
+
end
|
236
186
|
|
187
|
+
def _validates_number_max
|
188
|
+
return if @value.nil? || @_options[:max].nil?
|
189
|
+
@_result = @value <= @_options[:max]
|
237
190
|
end
|
238
191
|
|
239
192
|
end
|
data/lib/evnt/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: evnt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ideonetwork
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|