compel 0.4.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/lib/compel/validation/conditions/if.rb +31 -4
- data/lib/compel/version.rb +1 -1
- data/spec/compel/builder_spec.rb +50 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 760b8bea600ef35e2feaf4f884ce9993e60308f2
|
4
|
+
data.tar.gz: 2d6c7964a591146d16920535d751c5e4bc786d57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac24d0904ce924b9c34a44faaaa5fc138840412ad57db195e743d7ac0e40251adc0e2dc1cf37bee934acb9a9c777006abbd920743f4a6d881cc360c4bc2b81c6
|
7
|
+
data.tar.gz: 727af6ca9af8da14f937aacf58f3a4b52224c38a7db63cabd4ad8d1cef1d9be8609b8fae29a5e6995d3e00e36cfc8c875465c7732fd6b001a4bc480a1ebb07f9
|
data/.travis.yml
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
module Compel
|
2
4
|
module Validation
|
3
5
|
|
@@ -5,17 +7,42 @@ module Compel
|
|
5
7
|
|
6
8
|
def validate_value
|
7
9
|
unless valid?
|
8
|
-
"
|
10
|
+
"is invalid"
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
14
|
private
|
13
15
|
|
14
16
|
def valid?
|
15
|
-
|
16
|
-
|
17
|
+
CustomValidator.new(option_value).valid?(value)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
class CustomValidator
|
23
|
+
|
24
|
+
attr_reader :caller,
|
25
|
+
:method
|
26
|
+
|
27
|
+
def initialize(caller)
|
28
|
+
❨╯°□°❩╯︵┻━┻? caller
|
29
|
+
end
|
30
|
+
|
31
|
+
def valid?(value)
|
32
|
+
caller.send(method, value)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def ❨╯°□°❩╯︵┻━┻? caller
|
38
|
+
fail unless caller.is_a?(Proc) || caller.arity > 1
|
39
|
+
|
40
|
+
if caller.arity == 1
|
41
|
+
@caller = caller
|
42
|
+
@method = 'call'
|
17
43
|
else
|
18
|
-
|
44
|
+
@caller = caller.binding.receiver
|
45
|
+
@method = caller.call
|
19
46
|
end
|
20
47
|
end
|
21
48
|
|
data/lib/compel/version.rb
CHANGED
data/spec/compel/builder_spec.rb
CHANGED
@@ -401,6 +401,11 @@ describe Compel::Builder do
|
|
401
401
|
raise_error Compel::TypeError, 'invalid proc for if'
|
402
402
|
end
|
403
403
|
|
404
|
+
it 'should raise_error for invalid proc 1' do
|
405
|
+
expect{ Compel.any.if(1) }.to \
|
406
|
+
raise_error Compel::TypeError, 'invalid proc for if'
|
407
|
+
end
|
408
|
+
|
404
409
|
end
|
405
410
|
|
406
411
|
end
|
@@ -671,21 +676,21 @@ describe Compel::Builder do
|
|
671
676
|
result = Compel.any.if{:is_valid_one}.validate(2)
|
672
677
|
|
673
678
|
expect(result.valid?).to eq(false)
|
674
|
-
expect(result.errors).to include(
|
679
|
+
expect(result.errors).to include('is invalid')
|
675
680
|
end
|
676
681
|
|
677
682
|
it 'should validate with lambda' do
|
678
683
|
result = Compel.any.if(Proc.new {|value| value == 2 }).validate(1)
|
679
684
|
|
680
685
|
expect(result.valid?).to eq(false)
|
681
|
-
expect(result.errors).to include(
|
686
|
+
expect(result.errors).to include('is invalid')
|
682
687
|
end
|
683
688
|
|
684
689
|
it 'should validate within an instance method' do
|
685
690
|
result = CustomValidationsKlass.new(1).validate
|
686
691
|
|
687
692
|
expect(result.valid?).to eq(false)
|
688
|
-
expect(result.errors).to include(
|
693
|
+
expect(result.errors).to include('is invalid')
|
689
694
|
end
|
690
695
|
|
691
696
|
it 'should validate within an instance method 1' do
|
@@ -694,7 +699,7 @@ describe Compel::Builder do
|
|
694
699
|
.validate_with_lambda(Proc.new {|value| value == [1, 2, 3] })
|
695
700
|
|
696
701
|
expect(result.valid?).to eq(false)
|
697
|
-
expect(result.errors).to include(
|
702
|
+
expect(result.errors).to include('is invalid')
|
698
703
|
end
|
699
704
|
|
700
705
|
it 'should use custom message with parsed value' do
|
@@ -707,6 +712,35 @@ describe Compel::Builder do
|
|
707
712
|
expect(result.errors).to include('give me a 1!')
|
708
713
|
end
|
709
714
|
|
715
|
+
it 'should validate a date value' do
|
716
|
+
to_validate = '1969-01-01T00:00:00'
|
717
|
+
|
718
|
+
def validate_time(value)
|
719
|
+
value > Time.at(0)
|
720
|
+
end
|
721
|
+
|
722
|
+
result = Compel.time.if{:validate_time}.validate(to_validate)
|
723
|
+
|
724
|
+
expect(result.valid?).to eq(false)
|
725
|
+
expect(result.errors).to include('is invalid')
|
726
|
+
end
|
727
|
+
|
728
|
+
it 'should raise_error for invalid custom method arity' do
|
729
|
+
def custom_method_arity_two(value, extra_arg)
|
730
|
+
false
|
731
|
+
end
|
732
|
+
|
733
|
+
def custom_method_arity_zero
|
734
|
+
false
|
735
|
+
end
|
736
|
+
|
737
|
+
expect{ Compel.integer.if{:custom_method_arity_two}.validate(1) }.to \
|
738
|
+
raise_error ArgumentError
|
739
|
+
|
740
|
+
expect{ Compel.integer.if{:custom_method_arity_zero}.validate(1) }.to \
|
741
|
+
raise_error ArgumentError
|
742
|
+
end
|
743
|
+
|
710
744
|
end
|
711
745
|
|
712
746
|
context 'valid' do
|
@@ -756,6 +790,18 @@ describe Compel::Builder do
|
|
756
790
|
expect(result.valid?).to eq(true)
|
757
791
|
end
|
758
792
|
|
793
|
+
it 'should validate a date value' do
|
794
|
+
to_validate = '1969-01-01T00:00:00'
|
795
|
+
|
796
|
+
def validate_time(value)
|
797
|
+
value < Time.at(0)
|
798
|
+
end
|
799
|
+
|
800
|
+
result = Compel.time.if{:validate_time}.validate(to_validate)
|
801
|
+
|
802
|
+
expect(result.valid?).to eq(true)
|
803
|
+
end
|
804
|
+
|
759
805
|
end
|
760
806
|
|
761
807
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: compel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joaquim Adráz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
149
|
version: '0'
|
150
150
|
requirements: []
|
151
151
|
rubyforge_project:
|
152
|
-
rubygems_version: 2.
|
152
|
+
rubygems_version: 2.5.1
|
153
153
|
signing_key:
|
154
154
|
specification_version: 4
|
155
155
|
summary: Ruby Object Coercion and Validation
|