va 0.4.0 → 0.5.0

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: 71852c4aa71d300422814133cf073f6ddef674f3
4
- data.tar.gz: 98f5f50bf80557f9e76a2f401845c0754700a39f
3
+ metadata.gz: 8d0577caf4e3a47e684b5ed50a7eadd0062dba64
4
+ data.tar.gz: da3184c2e7a499d83578d5a7050c172e39f721a7
5
5
  SHA512:
6
- metadata.gz: dbf0f57ab08834eed1ed195ffc4696f332b5f43bc4290e51466b133e99c69ffc158decc1b14ffebd6f3e86bf1da921ec6e562d07c128df62e25a24fd1071f071
7
- data.tar.gz: 2ff88a638807598c7e219fc02e0e38f96d827d6cb6a5bc44dd5103db29a68144f65671353f5c85c14b568c880084d0f9ef8d12572d7a4991a4c453273613c0e8
6
+ metadata.gz: 1359705669b65f6cf9bffd1141831bda43cceb6321e712496e1918e5ab439caf8fa1eb338fcfa98f6abb6e047d50ea45a93284f0a64502b15a137b142ef1ceb8
7
+ data.tar.gz: 68c1affaa6458dffa190234a90e595b932a2350a287aa2cb8f49df19d84449fc6939b9f685e1e0084b9ba894d28c27b3ea4d160d40b969353cfa8fe71cb37ebe
data/Rakefile CHANGED
@@ -1,12 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
- task :test do
4
- arg_files = ENV["FILES"] && ENV["FILES"].split(/[\s,]+/)
5
- all_files = Rake::FileList["./spec/**/*_spec.rb"]
6
- files = arg_files || all_files
7
- puts "\nRuning tests for: #{ files.join(" ") }\n\n"
8
-
9
- system *["matest"].concat(files)
10
- end
11
-
3
+ require "matest/spec_tasks"
12
4
  task :default => :test
data/lib/va.rb CHANGED
@@ -9,7 +9,11 @@ module Va
9
9
  @attributes ||= self.class.defaults.dup
10
10
  args.each do |k, v|
11
11
  key = k.to_sym
12
- @attributes[key] = v if self.class.keys.include?(key)
12
+ if self.class.keys.include?(key)
13
+ @attributes[key] = v
14
+ elsif ! self.class.ignore_unauthorized_attributes?
15
+ raise UnauthorizedAttribute.new(key, self)
16
+ end
13
17
  end
14
18
  @errors = {}
15
19
  @valid = validate
@@ -33,6 +37,14 @@ module Va
33
37
  @valid
34
38
  end
35
39
 
40
+ def self.ignore_unauthorized_attributes
41
+ @__ignore_unauthorized_attributes__ = true
42
+ end
43
+
44
+ def self.ignore_unauthorized_attributes?
45
+ !! @__ignore_unauthorized_attributes__
46
+ end
47
+
36
48
  private
37
49
  def self.validations
38
50
  @validations ||= []
@@ -62,7 +74,7 @@ module Va
62
74
 
63
75
  def self.validate_not_nil(*attrs)
64
76
  validate_multiple(*attrs) do |attr|
65
- attr != nil
77
+ attr != nil
66
78
  end
67
79
  end
68
80
 
@@ -92,5 +104,12 @@ module Va
92
104
  end
93
105
  end
94
106
  class UnknownAttribute < Exception; end
107
+ class UnauthorizedAttribute < Exception
108
+ def initialize(attr, validator)
109
+ msg = "Unauthorized attribute for '#{validator.class}': '#{attr}'"
110
+ super(msg)
111
+ end
112
+ end
95
113
  class NotSpecified ; end
114
+
96
115
  end
@@ -3,7 +3,13 @@ module StringValidations
3
3
 
4
4
  def validate_string(*attrs)
5
5
  validate_multiple(*attrs) do |attr|
6
- attr.is_a?(String) || attr.nil?
6
+ attr.is_a?(String) || attr.nil?
7
+ end
8
+ end
9
+
10
+ def validate_string_not_empty(*attrs)
11
+ validate_multiple(*attrs) do |attr|
12
+ attr.is_a?(String) && !attr.empty?
7
13
  end
8
14
  end
9
15
 
@@ -1,3 +1,3 @@
1
1
  module Va
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,2 +1,6 @@
1
1
  $:.unshift(File.expand_path("../lib/", __dir__))
2
2
  require "va"
3
+
4
+ Matest.configure do |config|
5
+ config.use_color
6
+ end
@@ -7,7 +7,7 @@ scope "string validations" do
7
7
  class Person < Va::Validator
8
8
  include StringValidations
9
9
  attribute :name
10
-
10
+
11
11
  validate_string(:name)
12
12
  end
13
13
 
@@ -26,4 +26,23 @@ scope "string validations" do
26
26
  @p.valid?
27
27
  end
28
28
  end
29
+
30
+ scope "string is not empty" do
31
+ class Person < Va::Validator
32
+ include StringValidations
33
+ attribute :name
34
+
35
+ validate_string_not_empty(:name)
36
+ end
37
+
38
+ spec "not empty" do
39
+ @p = Person.new(name: "Fede")
40
+ @p.valid?
41
+ end
42
+
43
+ spec "empty" do
44
+ @p = Person.new(name: "")
45
+ ! @p.valid?
46
+ end
47
+ end
29
48
  end
@@ -9,8 +9,8 @@ scope do
9
9
  scope "init" do
10
10
  let(:va) { Login.new({"email" => "fede@example.com", pass: "123456"}) }
11
11
 
12
- it { va.email == "fede@example.com" }
13
- it { va.pass == "123456" }
12
+ spec { va.email == "fede@example.com" }
13
+ spec { va.pass == "123456" }
14
14
  end
15
15
 
16
16
  scope "#attributes" do
@@ -23,11 +23,6 @@ scope do
23
23
  @va = Login.new(email: "fede@example.com")
24
24
  @va.attributes == { email: "fede@example.com" }
25
25
  end
26
-
27
- spec "spureous" do
28
- @attributes = Login.new(email: "fede@example.com", i_dont_belong_here: "HELLO!").attributes
29
- @attributes == { email: "fede@example.com" }
30
- end
31
26
  end
32
27
  end
33
28
 
@@ -77,7 +72,7 @@ scope "custom validations" do
77
72
 
78
73
  scope "can't validate" do
79
74
  spec "invalid arguments" do
80
- begin
75
+ @ex = capture_exception(Va::UnknownAttribute) do
81
76
  class FaceLidator < Va::Validator
82
77
  attribute :face
83
78
 
@@ -85,10 +80,66 @@ scope "custom validations" do
85
80
  true
86
81
  end
87
82
  end
88
- rescue Object => e
89
- e.class == Va::UnknownAttribute
83
+
84
+ presenter.to_whatever
90
85
  end
86
+
87
+ @ex.is_a?(Va::UnknownAttribute)
88
+ end
89
+ end
90
+ end
91
+
92
+ scope "raising UnauthorizedAttribute on initialization" do
93
+ class NonRaiser < Va::Validator
94
+ ignore_unauthorized_attributes
95
+ attribute :yes
96
+ end
97
+
98
+ class Raiser < Va::Validator
99
+ attribute :yes
100
+ end
101
+
102
+ spec "raising when passing one attribute" do
103
+ @ex = capture_exception(Va::UnauthorizedAttribute) do
104
+ @va = Raiser.new(no: :something)
105
+ end
106
+
107
+ @ex.is_a?(Va::UnauthorizedAttribute)
108
+ end
109
+
110
+ spec "raising when passing more than one attribute" do
111
+ @ex = capture_exception(Va::UnauthorizedAttribute) do
112
+ @va = Raiser.new(yes: :something, no: :something_else)
113
+ end
114
+
115
+ @ex.is_a?(Va::UnauthorizedAttribute)
116
+ end
117
+
118
+ spec "names the attribute name on the message" do
119
+ @ex = capture_exception(Va::UnauthorizedAttribute) do
120
+ @va = Raiser.new(attr_name: :something_else)
91
121
  end
122
+
123
+ !! (@ex.message =~ /'attr_name'/)
124
+ end
125
+
126
+ spec "names the validator name on the message" do
127
+ @ex = capture_exception(Va::UnauthorizedAttribute) do
128
+ @va = Raiser.new(attr_name: :something_else)
129
+ end
130
+
131
+ !! (@ex.message =~ /'Raiser'/)
132
+ end
133
+
134
+ spec "not raising when passing one attribute" do
135
+ @va = NonRaiser.new(no: :something)
136
+ @va.valid?
137
+ end
138
+
139
+ spec "raising when passing more than one attribute" do
140
+ @va = NonRaiser.new(yes: :something, no: :something_else)
141
+
142
+ @va.valid?
92
143
  end
93
144
  end
94
145
 
@@ -111,14 +162,13 @@ scope "validate multiple" do
111
162
  @va = Name.new(first: "", last: "")
112
163
  ! @va.valid?
113
164
  end
114
-
165
+
115
166
  spec "one failing" do
116
167
  @va = Name.new(first: "Federico", last: "")
117
168
  ! @va.valid?
118
169
  end
119
170
  end
120
171
 
121
-
122
172
  scope "default values" do
123
173
  class MyDefaults < Va::Validator
124
174
  attribute :name, default: "N/A"
data/va.gemspec CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
 
24
- spec.add_development_dependency "matest", "~> 1.1.3"
24
+ spec.add_development_dependency "matest", "~> 1.6.9"
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: va
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Iachetti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 1.1.3
47
+ version: 1.6.9
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 1.1.3
54
+ version: 1.6.9
55
55
  description: Minimalistic framework agnostic Validator.
56
56
  email:
57
57
  - iachetti.federico@gmail.com
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 2.4.3
98
+ rubygems_version: 2.4.8
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: Minimalistic framework agnostic Validator.