menilite 0.4.1 → 0.4.2

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: 520189b4bc3d6aac748653931bd06f829f6f7087
4
- data.tar.gz: fd65b49bbb321e07e8c3addd5e21d7d9c41ee3c2
3
+ metadata.gz: 27f419ef565f994b020d022eea673453e02a6c8a
4
+ data.tar.gz: b9da18be00c27e19fa8e4c214db72adfe145eb02
5
5
  SHA512:
6
- metadata.gz: 52e8524e2ea17b3c76bbdb9e72c4671f6235ad644665f83597d0e065ff721d5b2d042dbe309a1037e5df13d4a4464a074928e1b48ef5fefbcd735f6e19de5a57
7
- data.tar.gz: 645ba9b5abe73a3f6ff0cc009757bda2a26ac4ec2bd51abec0461758d308633b0d130aa5f9da4c6c7f64e2a74231368285ab5151758dff7355edac1cd2ccaa5b
6
+ metadata.gz: 8f5baa982958df003abfa0d738af9238d17e5127a01861f10588b2af58a71f561ba5ccc9a2e983972a963942eabfc4b06bf37e6c2a214b625cc5d334d23c800e
7
+ data.tar.gz: 08f91b60b5eb398a0878af9703be8b1bd3b1f71a1b481ea313d05deca78db52d4030159b09521f87508794493849223a4d901fd9f361dd906a1e7b22c5f28ef3
@@ -13,8 +13,16 @@ class String
13
13
  end
14
14
 
15
15
  module Menilite
16
- class ValidationError < StandardError; end;
17
- class TypeError < StandardError; end;
16
+ class ValidationError < StandardError
17
+ attr_reader :messages
18
+
19
+ def initialize(messages)
20
+ @messages = messages
21
+ super(messages.join(', '))
22
+ end
23
+ end
24
+
25
+ class TypeError < StandardError; end
18
26
 
19
27
  class Model
20
28
  include Menilite::Helper
@@ -246,7 +254,13 @@ module Menilite
246
254
  end
247
255
 
248
256
  on :failure do |res|
249
- callback.call(:failure, res) if callback
257
+ if callback
258
+ if res.json[:result] == 'validation_error'
259
+ callback.call(:validation_error, Menilite::ValidationError.new(res.json[:messages]))
260
+ else
261
+ callback.call(:failure, res)
262
+ end
263
+ end
250
264
  end
251
265
  end
252
266
  end
@@ -263,10 +277,10 @@ module Menilite
263
277
  def validation(field_name, params = {}, &block)
264
278
  params.each do |k, v|
265
279
  if validator = Validators[k, v]
266
- (validators[field_name] ||= []) << validator.new(field_name)
280
+ (validators[field_name] ||= []) << validator.new(self, field_name)
267
281
  end
268
282
  end
269
- (validators[field_name] ||= []) << Validator.new(field_name, &block) if block
283
+ (validators[field_name] ||= []) << Validator.new(self, field_name, &block) if block
270
284
  end
271
285
 
272
286
  def find(id)
@@ -364,18 +378,15 @@ module Menilite
364
378
  end
365
379
 
366
380
  def validate(name, value)
367
- validator = self.class.validators[name]
368
- if validator
369
- messages = validator.map {|validator| validator.validate(value) }.compact
370
- messages.empty? or raise ValidationError.new(messages.join(','))
381
+ validators = self.class.validators[name]
382
+ if validators
383
+ messages = validators.select(&:enabled?).map {|validator| validator.validate(value) }.compact
371
384
  end
372
385
  end
373
386
 
374
387
  def validate_all
375
- self.fields.each do |k, v|
376
- type_validate(k, v)
377
- validate(k, v)
378
- end
388
+ messages = self.class.field_info.flat_map {|k, info| validate(k, self.fields[k]) }.compact
389
+ messages.empty? or raise ValidationError.new(messages)
379
390
  end
380
391
 
381
392
  def to_h
@@ -419,17 +430,49 @@ module Menilite
419
430
  end
420
431
 
421
432
  class Validator
422
- def initialize(name, &block)
433
+ include Menilite::Helper
434
+
435
+ def initialize(klass, name, &block)
436
+ @class = klass
423
437
  @proc = block
424
438
  end
425
439
 
426
440
  def validate(value)
427
441
  @proc.call(value)
428
442
  end
443
+
444
+ def enabled?
445
+ if server?
446
+ self.on_server
447
+ else
448
+ self.on_client
449
+ end
450
+ end
451
+
452
+ def on_server
453
+ true
454
+ end
455
+
456
+ def on_client
457
+ true
458
+ end
429
459
  end
460
+
430
461
  class PresenceValidator < Validator
431
- def initialize(name)
432
- super(name) {|value| "#{name} must not be empty" if value.nil? || value == "" }
462
+ def initialize(klass, name)
463
+ super(klass, name) {|value| "#{name} must not be empty" if value.nil? || value == "" }
464
+ end
465
+ end
466
+
467
+ class UniqueValidator < Validator
468
+ def initialize(klass, name)
469
+ super(klass, name) do |value|
470
+ "#{name}: '#{value}' already exist" unless klass.fetch(filter: { name => value }).empty?
471
+ end
472
+ end
473
+
474
+ def on_client
475
+ false
433
476
  end
434
477
  end
435
478
 
@@ -437,9 +480,9 @@ module Menilite
437
480
  def self.[](key, value)
438
481
  case key
439
482
  when :presence
440
- if value == true
441
- PresenceValidator
442
- end
483
+ PresenceValidator if value == true
484
+ when :unique
485
+ UniqueValidator if value == true
443
486
  end
444
487
  end
445
488
  end
@@ -58,18 +58,15 @@ module Menilite
58
58
  rescue Menilite::ErrorWithStatusCode => e
59
59
  content_type :json
60
60
  status e.code
61
-
62
- {:result => 'error', :message => e.message}.to_json
61
+ { result: 'error', message: e.message }.to_json
63
62
  rescue Menilite::ValidationError => e
64
63
  content_type :json
65
64
  status 403
66
-
67
- {:result => 'error', :message => e.message}.to_json
65
+ { result: 'validation_error', message: e.message, messages: e.messages }.to_json
68
66
  rescue => e
69
67
  content_type :json
70
68
  status 500
71
-
72
- {:result => 'error', :message => e.message}.to_json
69
+ { result: 'error', message: e.message }.to_json
73
70
  end
74
71
 
75
72
  enable :sessions
@@ -119,7 +116,10 @@ module Menilite
119
116
  router.before_action_handlers(klass, action.name).each {|h| self.instance_eval(&h[:proc]) }
120
117
  data = JSON.parse(request.body.read)
121
118
  result = if action.options[:save]
122
- klass.new(data["model"]).send(action.name, *data["args"]).save
119
+ klass.new(data["model"]).tap do |model|
120
+ model.send(action.name, *data["args"])
121
+ model.save
122
+ end
123
123
  elsif action.options[:class]
124
124
  klass.send(action.name, *data["args"])
125
125
  else
@@ -1,3 +1,3 @@
1
1
  module Menilite
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: menilite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-29 00:00:00.000000000 Z
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler