toolchain 0.1.0 → 0.1.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
  SHA1:
3
- metadata.gz: 988af450afb20a81dae7333ff7ded62a2fc6ca52
4
- data.tar.gz: f0cecb5a9bfe8ec82cbadab21019f4267f8ae5e5
3
+ metadata.gz: 42b6a53e0441d4960fdefb7aa46d2ab238d90dfd
4
+ data.tar.gz: d5e75f87d85cfcd542bf340f8dd0764586172bf9
5
5
  SHA512:
6
- metadata.gz: 3f707cd58b11de78ce0166874d9712d6197e572dcd6919787355a56cf2821b52127b0748732d2207f238721d45a25bf28c1fc8dc7ffc5d56255682accc1fe6d0
7
- data.tar.gz: 62c64f506a01f32f050f17b4418e1828a13e8c8f9f0dae62b324b3af615e5cc64f469f69af33fd2c7decb651e6fd48c4fd8eab4f579beaf69e5ab4738e6479f6
6
+ metadata.gz: 29dbe5cce70a0fad6e192c5649a0eb4cc343f4a370f58deee48a58033fda006a034926ed514cb738e022aa1234965fdf4d34ad79130c5b5141342ce3b909d379
7
+ data.tar.gz: 13d782b212ec1b7146b02998b6e194432c39b1f3421d10fc308009936960e7b420d6f0cf18794b630e3e9848c4600c0b83aca3d9e3659e1ebcae854610870164
@@ -35,9 +35,18 @@ module Toolchain::Validations
35
35
  def validates(*args)
36
36
  options, key_path = args.pop, args
37
37
 
38
+ _if = options.delete(:if)
39
+ _unless = options.delete(:unless)
40
+
38
41
  options.each do |key, data|
39
42
  if validator = Helpers.find_validator(key)
40
- validations.push(key_path: key_path, validator: validator, data: data)
43
+ validations.push(
44
+ key_path: key_path,
45
+ validator: validator,
46
+ data: data,
47
+ if: _if,
48
+ unless: _unless
49
+ )
41
50
  end
42
51
  end
43
52
  end
@@ -75,7 +84,19 @@ module Toolchain::Validations
75
84
  #
76
85
  def validate
77
86
  Helpers.each_validation(self.class) do |v|
78
- v[:validator].new(self, v[:key_path], v[:data]).validate
87
+ validate = true
88
+
89
+ if v[:if].kind_of?(Proc) && v[:if].call(self) == false
90
+ validate = false
91
+ end
92
+
93
+ if v[:unless].kind_of?(Proc) && v[:unless].call(self) == true
94
+ validate = false
95
+ end
96
+
97
+ if validate
98
+ v[:validator].new(self, v[:key_path], v[:data]).validate
99
+ end
79
100
  end
80
101
 
81
102
  Helpers.each_validation(self.class, :custom_validations) { |v| send(v) }
@@ -23,7 +23,7 @@ module Toolchain::Validations::Validators
23
23
  private
24
24
 
25
25
  def not_included?
26
- !data.include?(value)
26
+ !data[:in].include?(value)
27
27
  end
28
28
  end
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module Toolchain
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,52 @@
1
+ require_relative "base_helper"
2
+
3
+ describe Toolchain::Validations, "Conditional" do
4
+
5
+ class PersonConditionalIfTrue
6
+ include Toolchain::Validations
7
+ validates :name, presence: true, if: ->(i){ i.new }
8
+ attr_accessor :name
9
+ def new; true end
10
+ end
11
+
12
+ it "should validate" do
13
+ person = PersonConditionalIfTrue.new
14
+ person.should_not be_valid
15
+ end
16
+
17
+ class PersonConditionalIfFalse
18
+ include Toolchain::Validations
19
+ validates :name, presence: true, if: ->(i){ i.new }
20
+ attr_accessor :name
21
+ def new; false end
22
+ end
23
+
24
+ it "should not validate" do
25
+ person = PersonConditionalIfFalse.new
26
+ person.should be_valid
27
+ end
28
+
29
+ class PersonConditionalUnlessTrue
30
+ include Toolchain::Validations
31
+ validates :name, presence: true, unless: ->(i){ i.new }
32
+ attr_accessor :name
33
+ def new; true end
34
+ end
35
+
36
+ it "should not validate" do
37
+ person = PersonConditionalUnlessTrue.new
38
+ person.should be_valid
39
+ end
40
+
41
+ class PersonConditionalUnlessFalse
42
+ include Toolchain::Validations
43
+ validates :name, presence: true, unless: ->(i){ i.new }
44
+ attr_accessor :name
45
+ def new; false end
46
+ end
47
+
48
+ it "should validate" do
49
+ person = PersonConditionalUnlessFalse.new
50
+ person.should_not be_valid
51
+ end
52
+ end
@@ -5,7 +5,7 @@ describe Toolchain::Validations::Validators::Inclusion do
5
5
  class UserInclusion
6
6
  include Toolchain::Validations
7
7
  attr_accessor :color
8
- validates :color, inclusion: ["red", "green", "blue"]
8
+ validates :color, inclusion: { in: ["red", "green", "blue"] }
9
9
  end
10
10
 
11
11
  it "should be valid if 'red'" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toolchain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael van Rooijen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-14 00:00:00.000000000 Z
12
+ date: 2014-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - spec/toolchain/attributes/include_attributes_spec.rb
91
91
  - spec/toolchain/attributes/initializer_spec.rb
92
92
  - spec/toolchain/validations/base_helper.rb
93
+ - spec/toolchain/validations/conditional_spec.rb
93
94
  - spec/toolchain/validations/custom_validations_spec.rb
94
95
  - spec/toolchain/validations/delegation_spec.rb
95
96
  - spec/toolchain/validations/include_validations_spec.rb
@@ -143,6 +144,7 @@ test_files:
143
144
  - spec/toolchain/attributes/include_attributes_spec.rb
144
145
  - spec/toolchain/attributes/initializer_spec.rb
145
146
  - spec/toolchain/validations/base_helper.rb
147
+ - spec/toolchain/validations/conditional_spec.rb
146
148
  - spec/toolchain/validations/custom_validations_spec.rb
147
149
  - spec/toolchain/validations/delegation_spec.rb
148
150
  - spec/toolchain/validations/include_validations_spec.rb