valcro 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +6 -0
- data/LICENSE +19 -0
- data/README.md +82 -0
- data/Rakefile +19 -0
- data/lib/valcro.rb +1 -0
- data/lib/valcro/error_list.rb +4 -0
- data/lib/valcro/version.rb +1 -1
- data/spec/error_list_spec.rb +11 -0
- data/spec/runner_spec.rb +2 -2
- data/spec/valcro_spec.rb +13 -1
- data/valcro.gemspec +3 -2
- metadata +26 -6
data/.travis.yml
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2012 Harold Giménez
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Valcro
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/hgimenez/valcro.png?branch=master)](http://travis-ci.org/hgimenez/valcro)
|
4
|
+
|
5
|
+
Valcro is the simple validation library for Ruby. It provides
|
6
|
+
|
7
|
+
* A declarative way to describe what makes an object valid
|
8
|
+
* A way to declare validator classes encapsulating validations to be shared
|
9
|
+
across objects
|
10
|
+
* A method for running validations against an instance of your class
|
11
|
+
* A method for checking whether an object is valid
|
12
|
+
* Visibility into what validations failed
|
13
|
+
|
14
|
+
It is similar in spirit to ActiveModel's or Sequel's validations, but far
|
15
|
+
simpler and with a slightly different API. It does not provide validation
|
16
|
+
helpers such as `validates_presence_of` or similar.
|
17
|
+
|
18
|
+
## Example
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'valcro'
|
22
|
+
|
23
|
+
class Dog
|
24
|
+
include Valcro
|
25
|
+
|
26
|
+
attr_accessor :name
|
27
|
+
|
28
|
+
def validate
|
29
|
+
super
|
30
|
+
errors.add(:name, 'must be great') unless name =~ /super|great|cool/
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
dog = Dog.new
|
35
|
+
dog.name = 'spike'
|
36
|
+
dog.validate
|
37
|
+
dog.valid?
|
38
|
+
=> false
|
39
|
+
dog.errors[:name]
|
40
|
+
=> ["must be great"]
|
41
|
+
dog.error_messages
|
42
|
+
=> "name must be great"
|
43
|
+
```
|
44
|
+
|
45
|
+
Unlike other ruby validation libraries, you must call validate explicitely to
|
46
|
+
check for validity of objects.
|
47
|
+
|
48
|
+
### Sharing validators
|
49
|
+
|
50
|
+
You may also have Cats in your system, which share the same validation. Valcro lets you encapsulate validations in an object with the following contract:
|
51
|
+
|
52
|
+
* It is constructed with the object being validated
|
53
|
+
* It responds to `call`, which receives a `Valcro::ErrorList`. Add validation errors to this object.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class GreatNameValidator
|
57
|
+
def initialize(thing_with_name)
|
58
|
+
@thing_with_name = thing_with_name
|
59
|
+
end
|
60
|
+
|
61
|
+
def call(errors)
|
62
|
+
errors.add(:name, 'must be great') unless @thing_with_name.name =~ /super|great|cool/
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Dog
|
67
|
+
include Valcro
|
68
|
+
attr_accessor :name
|
69
|
+
validates_with GreatNameValidator
|
70
|
+
end
|
71
|
+
|
72
|
+
class Cat
|
73
|
+
include Valcro
|
74
|
+
attr_accessor :name
|
75
|
+
validates_with GreatNameValidator
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
Valcro is copyright (c) Harold Giménez and is released under the terms of the
|
82
|
+
MIT License found in the LICENSE file.
|
data/Rakefile
CHANGED
@@ -1 +1,20 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
task default: :spec
|
6
|
+
|
7
|
+
namespace :gem do
|
8
|
+
desc "Release to rubygems"
|
9
|
+
task :release do
|
10
|
+
require File.expand_path('lib/valcro/version', File.dirname(__FILE__))
|
11
|
+
version = Valcro::VERSION
|
12
|
+
message = "Bump to version #{version}"
|
13
|
+
system "git tag -a -m '#{message}' v#{version}"
|
14
|
+
system "git push origin master"
|
15
|
+
system "git push origin $(git tag | tail -1)"
|
16
|
+
system "gem build valcro.gemspec"
|
17
|
+
system "gem push valcro-#{version}.gem"
|
18
|
+
system "rm valcro-#{version}.gem"
|
19
|
+
end
|
20
|
+
end
|
data/lib/valcro.rb
CHANGED
data/lib/valcro/error_list.rb
CHANGED
data/lib/valcro/version.rb
CHANGED
data/spec/error_list_spec.rb
CHANGED
@@ -57,3 +57,14 @@ describe Valcro::ErrorList, '#any?' do
|
|
57
57
|
list.any?.should be_true
|
58
58
|
end
|
59
59
|
end
|
60
|
+
|
61
|
+
describe Valcro::ErrorList, '#clear!' do
|
62
|
+
it 'removes all errors' do
|
63
|
+
list = Valcro::ErrorList.new
|
64
|
+
list.add :prop, 'some error'
|
65
|
+
list.any?.should be_true
|
66
|
+
|
67
|
+
list.clear!
|
68
|
+
list.any?.should be_false
|
69
|
+
end
|
70
|
+
end
|
data/spec/runner_spec.rb
CHANGED
@@ -4,12 +4,12 @@ describe Valcro::Runner do
|
|
4
4
|
it 'can store validators' do
|
5
5
|
runner = Valcro::Runner.new
|
6
6
|
runner.add_validator :some_validator
|
7
|
-
runner.validators.should have(1).
|
7
|
+
runner.validators.should have(1).validator
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'runs validators' do
|
11
11
|
error_list = Valcro::ErrorList.new
|
12
|
-
runner
|
12
|
+
runner = Valcro::Runner.new(error_list)
|
13
13
|
runner.add_validator lambda { |errors| errors.add :foo, 'Huge mistake' }
|
14
14
|
|
15
15
|
runner.validate
|
data/spec/valcro_spec.rb
CHANGED
@@ -38,8 +38,9 @@ describe Valcro, 'validators' do
|
|
38
38
|
|
39
39
|
class TestClass
|
40
40
|
include Valcro
|
41
|
+
attr_accessor :status
|
41
42
|
def status
|
42
|
-
"fail"
|
43
|
+
@status ||= "fail"
|
43
44
|
end
|
44
45
|
validates_with StatusFail
|
45
46
|
end
|
@@ -50,6 +51,17 @@ describe Valcro, 'validators' do
|
|
50
51
|
test_instance.validate
|
51
52
|
test_instance.should_not be_valid
|
52
53
|
end
|
54
|
+
|
55
|
+
it 'clears validations on subsequent runs' do
|
56
|
+
test_instance = TestClass.new
|
57
|
+
|
58
|
+
test_instance.validate
|
59
|
+
test_instance.should_not be_valid
|
60
|
+
|
61
|
+
test_instance.status = 'win'
|
62
|
+
test_instance.validate
|
63
|
+
test_instance.should be_valid
|
64
|
+
end
|
53
65
|
end
|
54
66
|
|
55
67
|
describe Valcro, '#error_messages' do
|
data/valcro.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Harold Giménez"]
|
9
9
|
s.email = ["harold.gimenez@gmail.com"]
|
10
10
|
s.homepage = ""
|
11
|
-
s.summary = %q{
|
12
|
-
s.description = %q{
|
11
|
+
s.summary = %q{The simplest possible validation library for ruby}
|
12
|
+
s.description = %q{The simplest possible validation library for ruby}
|
13
13
|
|
14
14
|
s.rubyforge_project = "valcro"
|
15
15
|
|
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "rake"
|
22
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valcro
|
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,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70251622748220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,8 +21,19 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
|
24
|
+
version_requirements: *70251622748220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70251622747460 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70251622747460
|
36
|
+
description: The simplest possible validation library for ruby
|
26
37
|
email:
|
27
38
|
- harold.gimenez@gmail.com
|
28
39
|
executables: []
|
@@ -30,7 +41,10 @@ extensions: []
|
|
30
41
|
extra_rdoc_files: []
|
31
42
|
files:
|
32
43
|
- .gitignore
|
44
|
+
- .travis.yml
|
33
45
|
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
34
48
|
- Rakefile
|
35
49
|
- lib/valcro.rb
|
36
50
|
- lib/valcro/error.rb
|
@@ -55,18 +69,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
69
|
- - ! '>='
|
56
70
|
- !ruby/object:Gem::Version
|
57
71
|
version: '0'
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
hash: -250877099063039284
|
58
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
76
|
none: false
|
60
77
|
requirements:
|
61
78
|
- - ! '>='
|
62
79
|
- !ruby/object:Gem::Version
|
63
80
|
version: '0'
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
hash: -250877099063039284
|
64
84
|
requirements: []
|
65
85
|
rubyforge_project: valcro
|
66
86
|
rubygems_version: 1.8.10
|
67
87
|
signing_key:
|
68
88
|
specification_version: 3
|
69
|
-
summary:
|
89
|
+
summary: The simplest possible validation library for ruby
|
70
90
|
test_files:
|
71
91
|
- spec/error_list_spec.rb
|
72
92
|
- spec/error_spec.rb
|