sexy_validations 0.0.1 → 0.1.0
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.rdoc +34 -5
- data/VERSION +1 -1
- data/lib/sexy_validations.rb +21 -4
- data/lib/sexy_validations/errors.rb +20 -0
- data/sexy_validations.gemspec +3 -2
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -16,20 +16,49 @@ Or when using bundler, add it got your Gemfile:
|
|
16
16
|
|
17
17
|
gem sexy_validations
|
18
18
|
|
19
|
-
This should also install the geokit gem.
|
20
|
-
|
21
19
|
==Quick Start
|
22
20
|
|
23
21
|
In your model:
|
24
22
|
|
25
23
|
class User
|
26
|
-
|
24
|
+
include SexyValidations
|
27
25
|
|
28
26
|
validates :name, :presence => true, :length => 2..20
|
29
|
-
validates :
|
30
|
-
|
27
|
+
validates :description, :length => 0..2000
|
28
|
+
|
29
|
+
def valid?
|
30
|
+
validate!
|
31
|
+
errors.empty?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
You can also pass a symbol, which will be used a a method name:
|
36
|
+
|
37
|
+
class User
|
38
|
+
validates :special
|
39
|
+
|
40
|
+
def special_validation
|
41
|
+
if 5 > 9
|
42
|
+
errors.add(:boah, "ruby seems to be buggy....")
|
43
|
+
end
|
44
|
+
end
|
31
45
|
end
|
32
46
|
|
47
|
+
And even a block is accepted
|
48
|
+
|
49
|
+
class User
|
50
|
+
validates do
|
51
|
+
if 5 > 9
|
52
|
+
errors.add(:boah, "ruby seems to be buggy....")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
==Additional sequel specific validators
|
58
|
+
|
59
|
+
Please have a look at my sequel_sexy_validations gem (depends on this one).
|
60
|
+
This gem contains extra validators like Uniqueness.
|
61
|
+
|
33
62
|
==Todo
|
34
63
|
|
35
64
|
* Use I18n for messages
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/sexy_validations.rb
CHANGED
@@ -1,17 +1,34 @@
|
|
1
|
+
require 'sexy_validations/errors'
|
2
|
+
|
1
3
|
module SexyValidations
|
2
4
|
def self.included(klass)
|
3
5
|
klass.instance_eval do
|
4
|
-
|
5
|
-
|
6
|
+
class_inheritable_array :validations
|
7
|
+
self.validations = []
|
6
8
|
|
7
9
|
extend ClassMethods
|
8
10
|
include InstanceMethods
|
11
|
+
|
12
|
+
unless method_defined?(:errors)
|
13
|
+
class_eval do
|
14
|
+
def errors
|
15
|
+
@errors ||= Errors.new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
unless method_defined?(:valid?)
|
21
|
+
class_eval do
|
22
|
+
def valid?
|
23
|
+
validate!
|
24
|
+
errors.empty?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
9
28
|
end
|
10
29
|
end
|
11
30
|
|
12
31
|
module ClassMethods
|
13
|
-
attr_accessor :errors
|
14
|
-
|
15
32
|
def load_validator(name)
|
16
33
|
require "sexy_validations/validators/#{name}"
|
17
34
|
"SexyValidations::Validators::#{name.to_s.capitalize}".constantize
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SexyValidations
|
2
|
+
class Errors < ::Hash
|
3
|
+
def [](k)
|
4
|
+
has_key?(k) ? super : (self[k] = [])
|
5
|
+
end
|
6
|
+
|
7
|
+
def add(att, msg)
|
8
|
+
self[att] << msg
|
9
|
+
end
|
10
|
+
|
11
|
+
def count
|
12
|
+
values.inject(0){ |m, v| m+v.length }
|
13
|
+
end
|
14
|
+
|
15
|
+
def empty?
|
16
|
+
count == 0
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/sexy_validations.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sexy_validations}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Corin Langosch"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-10}
|
13
13
|
s.description = %q{Module which provides sexy validations for models.}
|
14
14
|
s.email = %q{info@netskin.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/sexy_validations.rb",
|
27
|
+
"lib/sexy_validations/errors.rb",
|
27
28
|
"lib/sexy_validations/validators/acceptance.rb",
|
28
29
|
"lib/sexy_validations/validators/age.rb",
|
29
30
|
"lib/sexy_validations/validators/confirmation.rb",
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Corin Langosch
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-10 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- Rakefile
|
50
50
|
- VERSION
|
51
51
|
- lib/sexy_validations.rb
|
52
|
+
- lib/sexy_validations/errors.rb
|
52
53
|
- lib/sexy_validations/validators/acceptance.rb
|
53
54
|
- lib/sexy_validations/validators/age.rb
|
54
55
|
- lib/sexy_validations/validators/confirmation.rb
|