sexy_validations 0.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010-2010 Corin Langosch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all 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
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ =About
2
+
3
+ This gem provides sexy validations/ validators functionality for models.
4
+ It was heavily inspired by the new validators in rails 3. Basically
5
+ it's a replacement for "inlcude ActiveModel::Validations", because those
6
+ are strangely complex and don't work together properly with sequel for
7
+ example.
8
+
9
+ ==Install
10
+
11
+ Simply install it as any other gem:
12
+
13
+ gem install sexy_validations
14
+
15
+ Or when using bundler, add it got your Gemfile:
16
+
17
+ gem sexy_validations
18
+
19
+ This should also install the geokit gem.
20
+
21
+ ==Quick Start
22
+
23
+ In your model:
24
+
25
+ class User
26
+ plugin :sexy_validations
27
+
28
+ validates :name, :presence => true, :length => 2..20
29
+ validates :desfription, :length => 0..2000
30
+ validates :photo, :image => "250x250" # see the sequel_paperclip gem
31
+ end
32
+
33
+ ==Todo
34
+
35
+ * Use I18n for messages
36
+ * Source documentation (rdoc)
37
+ * Tests
38
+
39
+ ==Contributing
40
+
41
+ If you'd like to contribute a feature or bugfix: Thanks! To make sure your
42
+ fix/feature has a high chance of being included, please read the following
43
+ guidelines:
44
+
45
+ 1. Fork the project.
46
+ 2. Make your feature addition or bug fix.
47
+ 3. Add tests for it. This is important so we don’t break anything in a future version unintentionally.
48
+ 4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
49
+ 5. Send me a pull request. Bonus points for topic branches.
50
+
51
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = 'sexy_validations'
8
+ gem.authors = ['Corin Langosch']
9
+ gem.date = Date.today.to_s
10
+ gem.email = 'info@netskin.com'
11
+ gem.homepage = 'http://github.com/gucki/sexy_validations'
12
+ gem.summary = 'Sexy validations for Models'
13
+ gem.description = 'Module which provides sexy validations for models.'
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "sexy_validations #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Acceptance
5
+ def self.validate(model, attribute, value, options)
6
+ unless value.to_i > 0
7
+ model.errors.add(attribute, "muss akzeptiert werden")
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Age
5
+ def self.validate(record, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {
10
+ :within => options,
11
+ }
12
+ end
13
+
14
+ min = options[:within].min
15
+ if value.calc_age < min
16
+ record.errors.add(attribute, "ungültig (mindestes #{min} Jahre)")
17
+ end
18
+
19
+ max = options[:within].max
20
+ if value.calc_age > max
21
+ record.errors.add(attribute, "ungültig (maximal #{max} Jahre)")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Confirmation
5
+ def self.validate(model, attribute, value, options)
6
+ return unless value
7
+
8
+ if value != model.send("#{attribute}_confirmation")
9
+ model.errors.add(attribute, "stimmt nicht mit der Bestätigung überein")
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Count
5
+ def self.validate(record, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {
10
+ :within => options,
11
+ }
12
+ end
13
+
14
+ min = options[:within].min
15
+ if value.count < min
16
+ record.errors.add(attribute, "zu wenig Selektionen (mindestes #{min})")
17
+ end
18
+
19
+ max = options[:within].max
20
+ if value.count > max
21
+ record.errors.add(attribute, "zu viele Selektionen (maximal #{max})")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Email
5
+ REGEXP = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
6
+
7
+ def self.validate(record, attribute, value, options)
8
+ return unless value
9
+
10
+ unless value =~ REGEXP
11
+ record.errors.add(attribute, "ungültiges Format")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class File
5
+ def self.humanized_size(num)
6
+ for x in ['Byte','KB','MB','GB','TB']
7
+ return "%d %s"%[num, x] if num < 1024.0
8
+ num /= 1024.0
9
+ end
10
+ end
11
+
12
+ def self.validate(model, attribute, value, options)
13
+ return unless value
14
+
15
+ unless options.is_a?(Hash)
16
+ options = {
17
+ :size => options,
18
+ }
19
+ end
20
+
21
+ unless value.is_a?(File) || value.is_a?(Tempfile)
22
+ raise ArgumentError, "#{value} is not a File"
23
+ end
24
+
25
+ if options[:size]
26
+ min = options[:size].min
27
+ if value.size < min
28
+ model.errors.add(attribute, "zu klein (mindestes #{humanized_size(min)})")
29
+ end
30
+
31
+ max = options[:size].max
32
+ if value.size > max
33
+ model.errors.add(attribute, "zu groß (maximal #{humanized_size(max)})")
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Format
5
+ def self.validate(model, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {
10
+ :with => options,
11
+ }
12
+ end
13
+
14
+ unless value =~ options[:with]
15
+ model.errors.add(attribute, options[:message] || "ungültiges Format")
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Image
5
+ def self.validate(model, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {
10
+ :geometry => options,
11
+ }
12
+ end
13
+
14
+ unless value.is_a?(File) || value.is_a?(Tempfile)
15
+ raise ArgumentError, "#{value} is not a File"
16
+ end
17
+
18
+ if options[:geometry]
19
+ geo1 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_s(options[:geometry])
20
+ geo2 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_file(value)
21
+ if geo2
22
+ if geo2.width < geo1.width
23
+ model.errors.add(attribute, "zu klein (weniger als %d Pixel breit)"%[geo1.width])
24
+ end
25
+ if geo2.height < geo1.height
26
+ model.errors.add(attribute, "zu klein (weniger als %d Pixel hoch)"%[geo1.height])
27
+ end
28
+ else
29
+ model.errors.add(attribute, "Bildformat unbekannt oder Datei beschädigt")
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Inclusion
5
+ def self.validate(model, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {
10
+ :within => options,
11
+ }
12
+ end
13
+
14
+ unless options[:within].include?(value)
15
+ model.errors.add(attribute, options[:message] || "ungültige Auswahl")
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Length
5
+ def self.validate(model, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {
10
+ :within => options,
11
+ }
12
+ end
13
+
14
+ min = options[:within].min
15
+ if value.length < min
16
+ model.errors.add(attribute, "zu kurz (mindestens #{min} Zeichen)")
17
+ end
18
+
19
+ max = options[:within].max
20
+ if value.length > max
21
+ model.errors.add(attribute, "zu lang (maximal #{max} Zeichen)")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Password
5
+ def self.validate(record, attribute, value, options)
6
+ return unless value
7
+
8
+ unless options.is_a?(Hash)
9
+ options = {}
10
+ end
11
+
12
+ options[:length] ||= 6..20
13
+ options[:format] ||= /^[\x20-\x7e]+$/i
14
+
15
+ min = options[:length].min
16
+ if value.length < min
17
+ record.errors.add(attribute, "zu kurz (mindestes #{min} Zeichen)")
18
+ end
19
+
20
+ max = options[:length].max
21
+ if value.length > max
22
+ record.errors.add(attribute, "zu lang (maximal #{max} Zeichen)")
23
+ end
24
+
25
+ unless value =~ options[:format]
26
+ record.errors.add(attribute, "enthält ungültige Zeichen")
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ module SexyValidations
3
+ module Validators
4
+ class Presence
5
+ def self.validate(model, attribute, value, options)
6
+ return unless value.blank?
7
+
8
+ model.errors.add(attribute, "muss ausgefüllt werden")
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,64 @@
1
+ module SexyValidations
2
+ def self.included(klass)
3
+ klass.instance_eval do
4
+ klass.class_inheritable_array :validations
5
+ klass.validations = []
6
+
7
+ extend ClassMethods
8
+ include InstanceMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ attr_accessor :errors
14
+
15
+ def load_validator(name)
16
+ require "sexy_validations/validators/#{name}"
17
+ "SexyValidations::Validators::#{name.to_s.capitalize}".constantize
18
+ end
19
+
20
+ def validates(attribute = nil, validations = nil, &block)
21
+ if validations
22
+ validations.each_pair do |validator, options|
23
+ klass = load_validator(validator)
24
+ self.validations << {
25
+ :attribute => attribute,
26
+ :validator => klass,
27
+ :options => options,
28
+ }
29
+ end
30
+ else
31
+ if attribute
32
+ self.validations << {
33
+ :method => "#{attribute}_validation",
34
+ }
35
+ else
36
+ self.validations << {
37
+ :block => block,
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ module InstanceMethods
45
+ def validate!
46
+ errors.clear
47
+ validations.each do |validation|
48
+ valid = case
49
+ when validation[:validator]
50
+ if errors[validation[:attribute]].empty?
51
+ validation[:validator].validate(self, validation[:attribute], send(validation[:attribute]), validation[:options])
52
+ end
53
+ when validation[:method]
54
+ send(validation[:method])
55
+ when validation[:block]
56
+ validation[:block].call
57
+ else
58
+ raise ArgumentError, "invalid validation (internal error)"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sexy_validations}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Corin Langosch"]
12
+ s.date = %q{2010-09-09}
13
+ s.description = %q{Module which provides sexy validations for models.}
14
+ s.email = %q{info@netskin.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/sexy_validations.rb",
27
+ "lib/sexy_validations/validators/acceptance.rb",
28
+ "lib/sexy_validations/validators/age.rb",
29
+ "lib/sexy_validations/validators/confirmation.rb",
30
+ "lib/sexy_validations/validators/count.rb",
31
+ "lib/sexy_validations/validators/email.rb",
32
+ "lib/sexy_validations/validators/file.rb",
33
+ "lib/sexy_validations/validators/format.rb",
34
+ "lib/sexy_validations/validators/image.rb",
35
+ "lib/sexy_validations/validators/inclusion.rb",
36
+ "lib/sexy_validations/validators/length.rb",
37
+ "lib/sexy_validations/validators/password.rb",
38
+ "lib/sexy_validations/validators/presence.rb",
39
+ "sexy_validations.gemspec",
40
+ "spec/sequel_sexy_validations_spec.rb",
41
+ "spec/spec.opts",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/gucki/sexy_validations}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.7}
48
+ s.summary = %q{Sexy validations for Models}
49
+ s.test_files = [
50
+ "spec/spec_helper.rb",
51
+ "spec/sequel_sexy_validations_spec.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SequelSexyValidations" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'sequel_sexy_validations'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sexy_validations
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Corin Langosch
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-09 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Module which provides sexy validations for models.
36
+ email: info@netskin.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/sexy_validations.rb
52
+ - lib/sexy_validations/validators/acceptance.rb
53
+ - lib/sexy_validations/validators/age.rb
54
+ - lib/sexy_validations/validators/confirmation.rb
55
+ - lib/sexy_validations/validators/count.rb
56
+ - lib/sexy_validations/validators/email.rb
57
+ - lib/sexy_validations/validators/file.rb
58
+ - lib/sexy_validations/validators/format.rb
59
+ - lib/sexy_validations/validators/image.rb
60
+ - lib/sexy_validations/validators/inclusion.rb
61
+ - lib/sexy_validations/validators/length.rb
62
+ - lib/sexy_validations/validators/password.rb
63
+ - lib/sexy_validations/validators/presence.rb
64
+ - sexy_validations.gemspec
65
+ - spec/sequel_sexy_validations_spec.rb
66
+ - spec/spec.opts
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/gucki/sexy_validations
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Sexy validations for Models
100
+ test_files:
101
+ - spec/spec_helper.rb
102
+ - spec/sequel_sexy_validations_spec.rb