Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
netskin-ci committed Sep 9, 2010
0 parents commit 09abcb4
Show file tree
Hide file tree
Showing 23 changed files with 552 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .document
@@ -0,0 +1,5 @@
README.rdoc
lib/**/*.rb
bin/*
features/**/*.feature
LICENSE
21 changes: 21 additions & 0 deletions .gitignore
@@ -0,0 +1,21 @@
## MAC OS
.DS_Store

## TEXTMATE
*.tmproj
tmtags

## EMACS
*~
\#*
.\#*

## VIM
*.swp

## PROJECT::GENERAL
coverage
rdoc
pkg

## PROJECT::SPECIFIC
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2010-2010 Corin Langosch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

51 changes: 51 additions & 0 deletions README.rdoc
@@ -0,0 +1,51 @@
=About

This gem provides sexy validations/ validators functionality for models.
It was heavily inspired by the new validators in rails 3. Basically
it's a replacement for "inlcude ActiveModel::Validations", because those
are strangely complex and don't work together properly with sequel for
example.

==Install

Simply install it as any other gem:

gem install sexy_validations

Or when using bundler, add it got your Gemfile:

gem sexy_validations

This should also install the geokit gem.

==Quick Start

In your model:

class User
plugin :sexy_validations

validates :name, :presence => true, :length => 2..20
validates :desfription, :length => 0..2000
validates :photo, :image => "250x250" # see the sequel_paperclip gem
end

==Todo

* Use I18n for messages
* Source documentation (rdoc)
* Tests

==Contributing

If you'd like to contribute a feature or bugfix: Thanks! To make sure your
fix/feature has a high chance of being included, please read the following
guidelines:

1. Fork the project.
2. Make your feature addition or bug fix.
3. Add tests for it. This is important so we don’t break anything in a future version unintentionally.
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)
5. Send me a pull request. Bonus points for topic branches.


46 changes: 46 additions & 0 deletions Rakefile
@@ -0,0 +1,46 @@
require 'rubygems'
require 'rake'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = 'sexy_validations'
gem.authors = ['Corin Langosch']
gem.date = Date.today.to_s
gem.email = 'info@netskin.com'
gem.homepage = 'http://github.com/gucki/sexy_validations'
gem.summary = 'Sexy validations for Models'
gem.description = 'Module which provides sexy validations for models.'
gem.add_development_dependency "rspec", ">= 1.2.9"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

task :spec => :check_dependencies

task :default => :spec

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

rdoc.rdoc_dir = 'rdoc'
rdoc.title = "sexy_validations #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.0.1
64 changes: 64 additions & 0 deletions lib/sexy_validations.rb
@@ -0,0 +1,64 @@
module SexyValidations
def self.included(klass)
klass.instance_eval do
klass.class_inheritable_array :validations
klass.validations = []

extend ClassMethods
include InstanceMethods
end
end

module ClassMethods
attr_accessor :errors

def load_validator(name)
require "sexy_validations/validators/#{name}"
"SexyValidations::Validators::#{name.to_s.capitalize}".constantize
end

def validates(attribute = nil, validations = nil, &block)
if validations
validations.each_pair do |validator, options|
klass = load_validator(validator)
self.validations << {
:attribute => attribute,
:validator => klass,
:options => options,
}
end
else
if attribute
self.validations << {
:method => "#{attribute}_validation",
}
else
self.validations << {
:block => block,
}
end
end
end
end

module InstanceMethods
def validate!
errors.clear
validations.each do |validation|
valid = case
when validation[:validator]
if errors[validation[:attribute]].empty?
validation[:validator].validate(self, validation[:attribute], send(validation[:attribute]), validation[:options])
end
when validation[:method]
send(validation[:method])
when validation[:block]
validation[:block].call
else
raise ArgumentError, "invalid validation (internal error)"
end
end
end
end
end

13 changes: 13 additions & 0 deletions lib/sexy_validations/validators/acceptance.rb
@@ -0,0 +1,13 @@
# encoding: utf-8
module SexyValidations
module Validators
class Acceptance
def self.validate(model, attribute, value, options)
unless value.to_i > 0
model.errors.add(attribute, "muss akzeptiert werden")
end
end
end
end
end

27 changes: 27 additions & 0 deletions lib/sexy_validations/validators/age.rb
@@ -0,0 +1,27 @@
# encoding: utf-8
module SexyValidations
module Validators
class Age
def self.validate(record, attribute, value, options)
return unless value

unless options.is_a?(Hash)
options = {
:within => options,
}
end

min = options[:within].min
if value.calc_age < min
record.errors.add(attribute, "ungültig (mindestes #{min} Jahre)")
end

max = options[:within].max
if value.calc_age > max
record.errors.add(attribute, "ungültig (maximal #{max} Jahre)")
end
end
end
end
end

15 changes: 15 additions & 0 deletions lib/sexy_validations/validators/confirmation.rb
@@ -0,0 +1,15 @@
# encoding: utf-8
module SexyValidations
module Validators
class Confirmation
def self.validate(model, attribute, value, options)
return unless value

if value != model.send("#{attribute}_confirmation")
model.errors.add(attribute, "stimmt nicht mit der Bestätigung überein")
end
end
end
end
end

27 changes: 27 additions & 0 deletions lib/sexy_validations/validators/count.rb
@@ -0,0 +1,27 @@
# encoding: utf-8
module SexyValidations
module Validators
class Count
def self.validate(record, attribute, value, options)
return unless value

unless options.is_a?(Hash)
options = {
:within => options,
}
end

min = options[:within].min
if value.count < min
record.errors.add(attribute, "zu wenig Selektionen (mindestes #{min})")
end

max = options[:within].max
if value.count > max
record.errors.add(attribute, "zu viele Selektionen (maximal #{max})")
end
end
end
end
end

17 changes: 17 additions & 0 deletions lib/sexy_validations/validators/email.rb
@@ -0,0 +1,17 @@
# encoding: utf-8
module SexyValidations
module Validators
class Email
REGEXP = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

def self.validate(record, attribute, value, options)
return unless value

unless value =~ REGEXP
record.errors.add(attribute, "ungültiges Format")
end
end
end
end
end

41 changes: 41 additions & 0 deletions lib/sexy_validations/validators/file.rb
@@ -0,0 +1,41 @@
# encoding: utf-8
module SexyValidations
module Validators
class File
def self.humanized_size(num)
for x in ['Byte','KB','MB','GB','TB']
return "%d %s"%[num, x] if num < 1024.0
num /= 1024.0
end
end

def self.validate(model, attribute, value, options)
return unless value

unless options.is_a?(Hash)
options = {
:size => options,
}
end

unless value.is_a?(File) || value.is_a?(Tempfile)
raise ArgumentError, "#{value} is not a File"
end

if options[:size]
min = options[:size].min
if value.size < min
model.errors.add(attribute, "zu klein (mindestes #{humanized_size(min)})")
end

max = options[:size].max
if value.size > max
model.errors.add(attribute, "zu groß (maximal #{humanized_size(max)})")
end
end

end
end
end
end

21 changes: 21 additions & 0 deletions lib/sexy_validations/validators/format.rb
@@ -0,0 +1,21 @@
# encoding: utf-8
module SexyValidations
module Validators
class Format
def self.validate(model, attribute, value, options)
return unless value

unless options.is_a?(Hash)
options = {
:with => options,
}
end

unless value =~ options[:with]
model.errors.add(attribute, options[:message] || "ungültiges Format")
end
end
end
end
end

0 comments on commit 09abcb4

Please sign in to comment.