valcro 0.0.2 → 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +3 -3
- data/lib/valcro.rb +10 -0
- data/lib/valcro/version.rb +1 -1
- data/spec/error_list_spec.rb +9 -9
- data/spec/error_spec.rb +4 -4
- data/spec/runner_spec.rb +2 -2
- data/spec/spec_helper.rb +15 -1
- data/spec/valcro_spec.rb +50 -23
- data/valcro.gemspec +3 -3
- metadata +13 -11
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Valcro
|
2
2
|
|
3
|
-
[](http://travis-ci.org/hgmnz/valcro)
|
4
|
+
[](https://codeclimate.com/github/hgmnz/valcro)
|
4
5
|
|
5
6
|
Valcro is the simple validation library for Ruby. It provides
|
6
7
|
|
@@ -25,8 +26,7 @@ class Dog
|
|
25
26
|
|
26
27
|
attr_accessor :name
|
27
28
|
|
28
|
-
|
29
|
-
super
|
29
|
+
validate do
|
30
30
|
errors.add(:name, 'must be great') unless name =~ /super|great|cool/
|
31
31
|
end
|
32
32
|
end
|
data/lib/valcro.rb
CHANGED
@@ -24,6 +24,9 @@ module Valcro
|
|
24
24
|
self.class.validators.each do |validator_class|
|
25
25
|
validation_runner.add_validator validator_class.new(self)
|
26
26
|
end
|
27
|
+
self.class.validation_blocks.each do |validation_block|
|
28
|
+
instance_eval(&validation_block)
|
29
|
+
end
|
27
30
|
validation_runner.validate
|
28
31
|
end
|
29
32
|
|
@@ -39,5 +42,12 @@ module Valcro
|
|
39
42
|
def validators
|
40
43
|
@validators ||= []
|
41
44
|
end
|
45
|
+
def validation_blocks
|
46
|
+
@validation_blocks ||= []
|
47
|
+
end
|
48
|
+
|
49
|
+
def validate(&block)
|
50
|
+
validation_blocks << block
|
51
|
+
end
|
42
52
|
end
|
43
53
|
end
|
data/lib/valcro/version.rb
CHANGED
data/spec/error_list_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe Valcro::ErrorList, 'adding errors' do
|
|
5
5
|
list = Valcro::ErrorList.new
|
6
6
|
list << Valcro::Error.new(:prop, 'message')
|
7
7
|
|
8
|
-
list.
|
8
|
+
expect(list.errors.length).to eq(1)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -13,13 +13,13 @@ describe Valcro::ErrorList, '#add' do
|
|
13
13
|
it 'finds and agregates error messages' do
|
14
14
|
list = Valcro::ErrorList.new
|
15
15
|
|
16
|
-
list[:one].
|
16
|
+
expect(list[:one]).to be_empty
|
17
17
|
|
18
18
|
list.add(:one, 'message one')
|
19
19
|
list.add(:two, 'message two')
|
20
20
|
list.add(:one, 'another message one')
|
21
21
|
|
22
|
-
list[:one].
|
22
|
+
expect(list[:one]).to eq(['message one', 'another message one'])
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -31,7 +31,7 @@ describe Valcro::ErrorList, '#full_messages' do
|
|
31
31
|
list.add :prop, 'two'
|
32
32
|
list.add :prop,'three'
|
33
33
|
|
34
|
-
list.full_messages.
|
34
|
+
expect(list.full_messages).to eq(["prop one", "prop two", "prop three"])
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -43,7 +43,7 @@ describe Valcro::ErrorList, '#to_s' do
|
|
43
43
|
list.add :prop, 'two'
|
44
44
|
list.add :prop, 'three'
|
45
45
|
|
46
|
-
list.to_s.
|
46
|
+
expect(list.to_s).to eq("prop one prop two prop three")
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -51,10 +51,10 @@ describe Valcro::ErrorList, '#any?' do
|
|
51
51
|
it 'is true when there are errors 'do
|
52
52
|
list = Valcro::ErrorList.new
|
53
53
|
|
54
|
-
list.any
|
54
|
+
expect(list.any?).to be_false
|
55
55
|
list.add :prop, 'some error'
|
56
56
|
|
57
|
-
list.any
|
57
|
+
expect(list.any?).to be_true
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -62,9 +62,9 @@ describe Valcro::ErrorList, '#clear!' do
|
|
62
62
|
it 'removes all errors' do
|
63
63
|
list = Valcro::ErrorList.new
|
64
64
|
list.add :prop, 'some error'
|
65
|
-
list.any
|
65
|
+
expect(list.any?).to be_true
|
66
66
|
|
67
67
|
list.clear!
|
68
|
-
list.any
|
68
|
+
expect(list.any?).to be_false
|
69
69
|
end
|
70
70
|
end
|
data/spec/error_spec.rb
CHANGED
@@ -4,18 +4,18 @@ describe Valcro::Error do
|
|
4
4
|
it 'has a property and a message' do
|
5
5
|
error = create_error(:prop, 'message')
|
6
6
|
|
7
|
-
error.property.
|
8
|
-
error.message.
|
7
|
+
expect(error.property).to eq(:prop)
|
8
|
+
expect(error.message).to eq('message')
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'can coerce to a string' do
|
12
12
|
error = create_error
|
13
|
-
error.to_s.
|
13
|
+
expect(error.to_s).to eq('prop message')
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'does not include property if it is base' do
|
17
17
|
error = create_error(:base)
|
18
|
-
error.to_s.
|
18
|
+
expect(error.to_s).to eq('message')
|
19
19
|
end
|
20
20
|
|
21
21
|
def create_error(prop = :prop, message = 'message')
|
data/spec/runner_spec.rb
CHANGED
@@ -4,7 +4,7 @@ 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.
|
7
|
+
expect(runner.validators).to have(1).validator
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'runs validators' do
|
@@ -14,6 +14,6 @@ describe Valcro::Runner do
|
|
14
14
|
|
15
15
|
runner.validate
|
16
16
|
|
17
|
-
error_list.any
|
17
|
+
expect(error_list.any?).to be_true
|
18
18
|
end
|
19
19
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,16 @@
|
|
1
|
-
require 'rspec'
|
2
1
|
require 'valcro'
|
2
|
+
RSpec.configure do |config|
|
3
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
4
|
+
config.run_all_when_everything_filtered = true
|
5
|
+
config.filter_run :focus
|
6
|
+
|
7
|
+
# Run specs in random order to surface order dependencies. If you find an
|
8
|
+
# order dependency and want to debug it, you can fix the order by providing
|
9
|
+
# the seed, which is printed after each run.
|
10
|
+
# --seed 1234
|
11
|
+
config.order = 'random'
|
12
|
+
|
13
|
+
config.expect_with :rspec do |c|
|
14
|
+
c.syntax = :expect
|
15
|
+
end
|
16
|
+
end
|
data/spec/valcro_spec.rb
CHANGED
@@ -5,29 +5,31 @@ describe Valcro do
|
|
5
5
|
include Valcro
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
it 'is valid with no validations defined' do
|
9
|
+
expect(TestClass.new.valid?).to be_true
|
10
|
+
end
|
10
11
|
end
|
11
12
|
|
12
13
|
describe Valcro, 'adding some errors' do
|
13
|
-
|
14
|
+
let(:test_class) do
|
15
|
+
Class.new do
|
14
16
|
include Valcro
|
17
|
+
end
|
15
18
|
end
|
16
19
|
|
17
20
|
it 'gives access to the error list' do
|
18
|
-
test_instance =
|
19
|
-
test_instance.should be_valid
|
21
|
+
test_instance = test_class.new
|
20
22
|
|
21
23
|
test_instance.errors.add(:foo, 'too foo for my taste')
|
22
24
|
|
23
|
-
test_instance.errors[:foo].
|
25
|
+
expect(test_instance.errors[:foo]).to have(1).error
|
24
26
|
|
25
|
-
test_instance.
|
27
|
+
expect(test_instance).not_to be_valid
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
29
31
|
describe Valcro, 'validators' do
|
30
|
-
class
|
32
|
+
class StatusFailValidator
|
31
33
|
def initialize(context)
|
32
34
|
@context = context
|
33
35
|
end
|
@@ -36,43 +38,68 @@ describe Valcro, 'validators' do
|
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
let(:test_class) do
|
42
|
+
Class.new do
|
43
|
+
include Valcro
|
44
|
+
attr_accessor :status
|
45
|
+
def status
|
46
|
+
@status ||= "fail"
|
47
|
+
end
|
48
|
+
validates_with StatusFailValidator
|
44
49
|
end
|
45
|
-
validates_with StatusFail
|
46
50
|
end
|
47
51
|
|
48
52
|
it 'can be added validators' do
|
49
|
-
test_instance =
|
53
|
+
test_instance = test_class.new
|
50
54
|
|
51
55
|
test_instance.validate
|
52
|
-
test_instance.
|
56
|
+
expect(test_instance).not_to be_valid
|
53
57
|
end
|
54
58
|
|
55
59
|
it 'clears validations on subsequent runs' do
|
56
|
-
test_instance =
|
60
|
+
test_instance = test_class.new
|
57
61
|
|
58
62
|
test_instance.validate
|
59
|
-
test_instance.
|
63
|
+
expect(test_instance).not_to be_valid
|
60
64
|
|
61
65
|
test_instance.status = 'win'
|
62
66
|
test_instance.validate
|
63
|
-
test_instance.
|
67
|
+
expect(test_instance).to be_valid
|
64
68
|
end
|
65
69
|
end
|
66
70
|
|
67
71
|
describe Valcro, '#error_messages' do
|
68
|
-
|
69
|
-
include Valcro
|
72
|
+
let(:test_class) do
|
73
|
+
Class.new { include Valcro }
|
70
74
|
end
|
71
75
|
|
72
76
|
it 'delegates to errors' do
|
73
|
-
test_instance =
|
77
|
+
test_instance = test_class.new
|
74
78
|
test_instance.stub!(errors: double(to_s: 'some errors'))
|
75
79
|
|
76
|
-
test_instance.error_messages.
|
80
|
+
expect(test_instance.error_messages).to eq('some errors')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe Valcro, '.validate' do
|
85
|
+
let(:test_class) do
|
86
|
+
Class.new do
|
87
|
+
include Valcro
|
88
|
+
attr_accessor :works
|
89
|
+
validate do
|
90
|
+
errors.add(:works, 'does not work') unless works
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'sets validations on the included class' do
|
96
|
+
test_instance = test_class.new
|
97
|
+
test_instance.works = false
|
98
|
+
test_instance.validate
|
99
|
+
expect(test_instance.valid?).to be_false
|
100
|
+
|
101
|
+
test_instance.works = true
|
102
|
+
test_instance.validate
|
103
|
+
expect(test_instance.valid?).to be_true
|
77
104
|
end
|
78
105
|
end
|
data/valcro.gemspec
CHANGED
@@ -7,9 +7,9 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Valcro::VERSION
|
8
8
|
s.authors = ["Harold Giménez"]
|
9
9
|
s.email = ["harold.gimenez@gmail.com"]
|
10
|
-
s.homepage = ""
|
11
|
-
s.summary = %q{The simplest possible validation library for
|
12
|
-
s.description = %q{
|
10
|
+
s.homepage = "https://github.com/hgimenez/valcro"
|
11
|
+
s.summary = %q{The simplest possible validation library for Ruby}
|
12
|
+
s.description = %q{Valcro is a validation library for arbitrary ruby objects. I's primary goal is to remain as simple as possible so that it can be useful to a large surface of use cases remaining easily extensible.}
|
13
13
|
|
14
14
|
s.rubyforge_project = "valcro"
|
15
15
|
|
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.
|
4
|
+
version: '0.1'
|
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-
|
12
|
+
date: 2012-10-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70279594936620 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70279594936620
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70279594952580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,8 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
-
description:
|
35
|
+
version_requirements: *70279594952580
|
36
|
+
description: Valcro is a validation library for arbitrary ruby objects. I's primary
|
37
|
+
goal is to remain as simple as possible so that it can be useful to a large surface
|
38
|
+
of use cases remaining easily extensible.
|
37
39
|
email:
|
38
40
|
- harold.gimenez@gmail.com
|
39
41
|
executables: []
|
@@ -57,7 +59,7 @@ files:
|
|
57
59
|
- spec/spec_helper.rb
|
58
60
|
- spec/valcro_spec.rb
|
59
61
|
- valcro.gemspec
|
60
|
-
homepage:
|
62
|
+
homepage: https://github.com/hgimenez/valcro
|
61
63
|
licenses: []
|
62
64
|
post_install_message:
|
63
65
|
rdoc_options: []
|
@@ -71,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
73
|
version: '0'
|
72
74
|
segments:
|
73
75
|
- 0
|
74
|
-
hash: -
|
76
|
+
hash: -54788049467793668
|
75
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
78
|
none: false
|
77
79
|
requirements:
|
@@ -80,13 +82,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
82
|
version: '0'
|
81
83
|
segments:
|
82
84
|
- 0
|
83
|
-
hash: -
|
85
|
+
hash: -54788049467793668
|
84
86
|
requirements: []
|
85
87
|
rubyforge_project: valcro
|
86
88
|
rubygems_version: 1.8.10
|
87
89
|
signing_key:
|
88
90
|
specification_version: 3
|
89
|
-
summary: The simplest possible validation library for
|
91
|
+
summary: The simplest possible validation library for Ruby
|
90
92
|
test_files:
|
91
93
|
- spec/error_list_spec.rb
|
92
94
|
- spec/error_spec.rb
|