micro_validator 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -0
- data/Rakefile +21 -0
- data/lib/micro_validator.rb +2 -2
- data/lib/micro_validator/errors.rb +9 -1
- data/lib/micro_validator/version.rb +1 -1
- data/micro_validator.gemspec +1 -0
- data/test/micro_validator_test.rb +62 -0
- data/test/test_helper.rb +1 -0
- metadata +23 -3
data/README.md
CHANGED
@@ -1,2 +1,31 @@
|
|
1
1
|
µValidator
|
2
2
|
===============
|
3
|
+
|
4
|
+
Zero dependency, dead simple validation library.
|
5
|
+
|
6
|
+
|
7
|
+
Usage
|
8
|
+
========
|
9
|
+
|
10
|
+
```
|
11
|
+
require "micro_validator"
|
12
|
+
|
13
|
+
|
14
|
+
class Foo
|
15
|
+
validate :bar
|
16
|
+
validate :baz
|
17
|
+
|
18
|
+
private
|
19
|
+
def bar
|
20
|
+
errors.add(:attribute, "Attribute is wrong") if attribute == "Wrong"
|
21
|
+
end
|
22
|
+
|
23
|
+
def baz
|
24
|
+
false # Always fails
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
@foo = Foo.new
|
29
|
+
@foo.valid? # => false
|
30
|
+
|
31
|
+
```
|
data/Rakefile
CHANGED
@@ -1 +1,22 @@
|
|
1
|
+
$LOAD_PATH << "./lib"
|
2
|
+
|
1
3
|
require "bundler/gem_tasks"
|
4
|
+
require "rake"
|
5
|
+
require "rake/testtask"
|
6
|
+
Dir["#{File.dirname(__FILE__)}/lib/**/*.rb"].each {|file| require file }
|
7
|
+
|
8
|
+
begin
|
9
|
+
Bundler.setup(:default, :development)
|
10
|
+
rescue Bundler::BundlerError => e
|
11
|
+
$stderr.puts e.message
|
12
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
13
|
+
exit e.status_code
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::TestTask.new(:test) do |test|
|
17
|
+
test.libs << "lib" << "test"
|
18
|
+
test.pattern = "test/**/*_test.rb"
|
19
|
+
test.verbose = true
|
20
|
+
end
|
21
|
+
|
22
|
+
task default: :test
|
data/lib/micro_validator.rb
CHANGED
data/micro_validator.gemspec
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "micro_validator"
|
3
|
+
|
4
|
+
class MyKlass
|
5
|
+
include MicroValidator
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def testing
|
10
|
+
errors.add(:testing, "Something failed...")
|
11
|
+
end
|
12
|
+
|
13
|
+
def another
|
14
|
+
errors.add(:testing, "Another failure!")
|
15
|
+
end
|
16
|
+
|
17
|
+
def different
|
18
|
+
errors.add(:different, "It's different this time.")
|
19
|
+
end
|
20
|
+
|
21
|
+
def successful
|
22
|
+
(5 > 1) ? true : errors.add(:comparison, "5 is less than 1!")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module MicroValidator
|
27
|
+
class MicroValidatorTest < Minitest::Test
|
28
|
+
def setup
|
29
|
+
@klass = MyKlass.new
|
30
|
+
MyKlass.validation_methods.clear
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_should_not_pass_validation
|
34
|
+
MyKlass.validate :testing, :successful
|
35
|
+
|
36
|
+
assert !@klass.valid?
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_pass_validation
|
40
|
+
MyKlass.validate :successful
|
41
|
+
|
42
|
+
assert @klass.valid?
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_return_all_errors
|
46
|
+
MyKlass.validate :testing, :another, :successful, :different
|
47
|
+
@klass.valid?
|
48
|
+
|
49
|
+
assert_equal ({ testing: ["Something failed...", "Another failure!"],
|
50
|
+
different: ["It's different this time."]} ),
|
51
|
+
@klass.errors.all
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_picking_one_error
|
55
|
+
MyKlass.validate :testing, :different
|
56
|
+
@klass.valid?
|
57
|
+
|
58
|
+
assert_equal ["It's different this time."],
|
59
|
+
@klass.errors.field(:different)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "minitest/autorun"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micro_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
description: µValidator is a zero dependency validation library, which mimics ActiveModel::Error
|
47
63
|
behaviour
|
48
64
|
email:
|
@@ -61,6 +77,8 @@ files:
|
|
61
77
|
- lib/micro_validator/errors.rb
|
62
78
|
- lib/micro_validator/version.rb
|
63
79
|
- micro_validator.gemspec
|
80
|
+
- test/micro_validator_test.rb
|
81
|
+
- test/test_helper.rb
|
64
82
|
homepage: ''
|
65
83
|
licenses:
|
66
84
|
- MIT
|
@@ -86,5 +104,7 @@ rubygems_version: 1.8.25
|
|
86
104
|
signing_key:
|
87
105
|
specification_version: 3
|
88
106
|
summary: Zero–dependency ActiveModel:Error–like validation library.
|
89
|
-
test_files:
|
107
|
+
test_files:
|
108
|
+
- test/micro_validator_test.rb
|
109
|
+
- test/test_helper.rb
|
90
110
|
has_rdoc:
|