hash_validations 0.1.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/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/Rakefile +45 -0
- data/VERSION.yml +4 -0
- data/hash_validations.gemspec +55 -0
- data/init.rb +1 -0
- data/lib/hash_validations.rb +90 -0
- data/spec/hash_validations_spec.rb +44 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +9 -0
- metadata +76 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rodrigo Panachi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= hash_validations
|
2
|
+
|
3
|
+
A lib to help with hash validations.
|
4
|
+
|
5
|
+
== Instalation
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
require "hash_validations"
|
10
|
+
|
11
|
+
def my_method(parameters = {})
|
12
|
+
|
13
|
+
options = {:default => "values"}.merge(parameters)
|
14
|
+
options.validates_presence_of :email, :message => "The email is required"
|
15
|
+
options.validates_numericallity_of :age, :message => "The age must be a number"
|
16
|
+
|
17
|
+
unless options.valid? raise ArgumentError.new("Invalid arguments: " + options.errors.humanize)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
== Note on Patches/Pull Requests
|
22
|
+
|
23
|
+
* Fork the project.
|
24
|
+
* Make your feature addition or bug fix.
|
25
|
+
* Add tests for it. This is important so I don't break it in a
|
26
|
+
future version unintentionally.
|
27
|
+
* Commit, do not mess with rakefile, version, or history.
|
28
|
+
(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)
|
29
|
+
* Send me a pull request. Bonus points for topic branches.
|
30
|
+
|
31
|
+
== Copyright
|
32
|
+
|
33
|
+
Copyright (c) 2009 Rodrigo Panachi. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "hash_validations"
|
8
|
+
gem.summary = %Q{A lib to help with hash validations}
|
9
|
+
gem.description = %Q{A lib to help with hash validations, based on Validatable}
|
10
|
+
gem.email = "rodrigopanachi@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/rpanachi/hash_validations"
|
12
|
+
gem.authors = ["Rodrigo Panachi"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "hash_validations #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,55 @@
|
|
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{hash_validations}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Rodrigo Panachi"]
|
12
|
+
s.date = %q{2009-11-23}
|
13
|
+
s.description = %q{A lib to help with hash validations, based on Validatable}
|
14
|
+
s.email = %q{rodrigopanachi@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION.yml",
|
25
|
+
"hash_validations.gemspec",
|
26
|
+
"init.rb",
|
27
|
+
"lib/hash_validations.rb",
|
28
|
+
"spec/hash_validations_spec.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/rpanachi/hash_validations}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{A lib to help with hash validations}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/hash_validations_spec.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/lib/hash_validations')
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/validatable/validatable")
|
2
|
+
|
3
|
+
module Validatable
|
4
|
+
|
5
|
+
def self.included(klass) #:nodoc:
|
6
|
+
#ignore
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid_for_group?(group) #:nodoc:
|
10
|
+
run_before_validations
|
11
|
+
errors.clear
|
12
|
+
self.validate_children(self, group)
|
13
|
+
self.validate(group)
|
14
|
+
errors.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
# call-seq: validate_only(key)
|
18
|
+
#
|
19
|
+
# Only executes a specified validation. The argument should follow a pattern based on the key of the validation.
|
20
|
+
# Examples:
|
21
|
+
# * validates_presence_of :name can be run with obj.validate_only("presence_of/name")
|
22
|
+
# * validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")
|
23
|
+
def validate_only(key)
|
24
|
+
validation_name, attribute_name = key.split("/")
|
25
|
+
validation_name = validation_name.split("_").collect{|word| word.capitalize}.join
|
26
|
+
validation_key = "#{self.name}/Validatable::Validates#{validation_name}/#{attribute_name}"
|
27
|
+
validation = self.all_validations.find { |validation| validation.key == validation_key }
|
28
|
+
raise ArgumentError.new("validation with key #{validation_key} could not be found") if validation.nil?
|
29
|
+
errors.clear
|
30
|
+
run_validation(validation)
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
def run_validation(validation) #:nodoc:
|
35
|
+
validation_result = validation.valid?(self)
|
36
|
+
add_error(self, validation.attribute, validation.message(self)) unless validation_result
|
37
|
+
increment_times_validated_for(validation)
|
38
|
+
validation.run_after_validate(validation_result, self, validation.attribute)
|
39
|
+
end
|
40
|
+
|
41
|
+
def run_before_validations #:nodoc:
|
42
|
+
self.all_before_validations.each do |block|
|
43
|
+
instance_eval &block
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def all_validations #:nodoc:
|
48
|
+
res = self.validations_to_include.inject(self.all_validations) do |result, included_validation_class|
|
49
|
+
result += self.send(included_validation_class.attribute).all_validations
|
50
|
+
result
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def validation_levels #:nodoc:
|
55
|
+
self.all_validations.inject([1]) { |result, validation| result << validation.level }.uniq.sort
|
56
|
+
end
|
57
|
+
|
58
|
+
class Errors
|
59
|
+
|
60
|
+
def has_key?(key)
|
61
|
+
@errors.has_key?(key)
|
62
|
+
end
|
63
|
+
|
64
|
+
def humanize(lower_case_and_underscored_word) #:nodoc:
|
65
|
+
":#{lower_case_and_underscored_word}"
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
class Hash
|
73
|
+
|
74
|
+
include Validatable
|
75
|
+
include Validatable::ClassMethods
|
76
|
+
include Validatable::Macros
|
77
|
+
|
78
|
+
attr_accessor :name
|
79
|
+
attr_accessor :superclass
|
80
|
+
|
81
|
+
def method_missing(method_name, *args)
|
82
|
+
self[method_name]
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "HashValidations" do
|
4
|
+
|
5
|
+
it "Should initialize" do
|
6
|
+
h = {}
|
7
|
+
h.should be_valid
|
8
|
+
end
|
9
|
+
|
10
|
+
it "Should be valid for a empty hash" do
|
11
|
+
h = {}
|
12
|
+
h.should be_valid
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "ValidatesPresenceOf" do
|
16
|
+
|
17
|
+
it "Should be valid when hash has keys" do
|
18
|
+
|
19
|
+
h = {:foo => "bar"}
|
20
|
+
h.validates_presence_of :foo
|
21
|
+
|
22
|
+
h.should be_valid
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
it "Should be invalid when hash not include keys" do
|
27
|
+
|
28
|
+
h = {}
|
29
|
+
h.validates_presence_of :foo
|
30
|
+
|
31
|
+
h.should_not be_valid
|
32
|
+
h.errors.should have_key(:foo)
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
it "Should return the message supplied for the validation when not pass" do
|
37
|
+
h = {}
|
38
|
+
h.validates_presence_of :foo, :message => "foo is required!"
|
39
|
+
h.should_not be_valid
|
40
|
+
h.errors.raw(:foo).first.should == "foo is required!"
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_validations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rodrigo Panachi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-23 00:00:00 -02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: A lib to help with hash validations, based on Validatable
|
26
|
+
email: rodrigopanachi@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- VERSION.yml
|
40
|
+
- hash_validations.gemspec
|
41
|
+
- init.rb
|
42
|
+
- lib/hash_validations.rb
|
43
|
+
- spec/hash_validations_spec.rb
|
44
|
+
- spec/spec.opts
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/rpanachi/hash_validations
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: A lib to help with hash validations
|
74
|
+
test_files:
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- spec/hash_validations_spec.rb
|