ardm-validations 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +11 -0
- data/Gemfile +51 -0
- data/LICENSE +21 -0
- data/README.rdoc +122 -0
- data/Rakefile +4 -0
- data/ardm-validations.gemspec +28 -0
- data/lib/ardm-validations.rb +1 -0
- data/lib/dm-validations.rb +169 -0
- data/lib/dm-validations/auto_validate.rb +252 -0
- data/lib/dm-validations/context.rb +66 -0
- data/lib/dm-validations/contextual_validators.rb +220 -0
- data/lib/dm-validations/exceptions.rb +5 -0
- data/lib/dm-validations/formats/email.rb +65 -0
- data/lib/dm-validations/formats/url.rb +27 -0
- data/lib/dm-validations/support/object.rb +18 -0
- data/lib/dm-validations/support/ordered_hash.rb +434 -0
- data/lib/dm-validations/validation_errors.rb +137 -0
- data/lib/dm-validations/validators/absent_field_validator.rb +58 -0
- data/lib/dm-validations/validators/acceptance_validator.rb +79 -0
- data/lib/dm-validations/validators/block_validator.rb +61 -0
- data/lib/dm-validations/validators/confirmation_validator.rb +92 -0
- data/lib/dm-validations/validators/format_validator.rb +124 -0
- data/lib/dm-validations/validators/generic_validator.rb +184 -0
- data/lib/dm-validations/validators/length_validator.rb +249 -0
- data/lib/dm-validations/validators/method_validator.rb +64 -0
- data/lib/dm-validations/validators/numeric_validator.rb +182 -0
- data/lib/dm-validations/validators/primitive_validator.rb +58 -0
- data/lib/dm-validations/validators/required_field_validator.rb +83 -0
- data/lib/dm-validations/validators/uniqueness_validator.rb +67 -0
- data/lib/dm-validations/validators/within_validator.rb +74 -0
- data/lib/dm-validations/version.rb +5 -0
- data/spec/fixtures/barcode.rb +40 -0
- data/spec/fixtures/basketball_court.rb +58 -0
- data/spec/fixtures/basketball_player.rb +34 -0
- data/spec/fixtures/beta_tester_account.rb +33 -0
- data/spec/fixtures/bill_of_landing.rb +47 -0
- data/spec/fixtures/boat_dock.rb +26 -0
- data/spec/fixtures/city.rb +24 -0
- data/spec/fixtures/company.rb +93 -0
- data/spec/fixtures/corporate_world.rb +39 -0
- data/spec/fixtures/country.rb +24 -0
- data/spec/fixtures/ethernet_frame.rb +56 -0
- data/spec/fixtures/event.rb +44 -0
- data/spec/fixtures/g3_concert.rb +57 -0
- data/spec/fixtures/jabberwock.rb +27 -0
- data/spec/fixtures/kayak.rb +28 -0
- data/spec/fixtures/lernean_hydra.rb +39 -0
- data/spec/fixtures/llama_spaceship.rb +15 -0
- data/spec/fixtures/mathematical_function.rb +34 -0
- data/spec/fixtures/memory_object.rb +30 -0
- data/spec/fixtures/mittelschnauzer.rb +39 -0
- data/spec/fixtures/motor_launch.rb +21 -0
- data/spec/fixtures/multibyte.rb +16 -0
- data/spec/fixtures/page.rb +32 -0
- data/spec/fixtures/phone_number.rb +28 -0
- data/spec/fixtures/pirogue.rb +28 -0
- data/spec/fixtures/programming_language.rb +83 -0
- data/spec/fixtures/reservation.rb +38 -0
- data/spec/fixtures/scm_operation.rb +56 -0
- data/spec/fixtures/sms_message.rb +22 -0
- data/spec/fixtures/udp_packet.rb +49 -0
- data/spec/integration/absent_field_validator/absent_field_validator_spec.rb +90 -0
- data/spec/integration/absent_field_validator/spec_helper.rb +7 -0
- data/spec/integration/acceptance_validator/acceptance_validator_spec.rb +196 -0
- data/spec/integration/acceptance_validator/spec_helper.rb +7 -0
- data/spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +57 -0
- data/spec/integration/automatic_validation/disabling_inferred_validation_spec.rb +49 -0
- data/spec/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +100 -0
- data/spec/integration/automatic_validation/inferred_float_property_validation_spec.rb +45 -0
- data/spec/integration/automatic_validation/inferred_format_validation_spec.rb +35 -0
- data/spec/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +70 -0
- data/spec/integration/automatic_validation/inferred_length_validation_spec.rb +142 -0
- data/spec/integration/automatic_validation/inferred_presence_validation_spec.rb +45 -0
- data/spec/integration/automatic_validation/inferred_primitive_validation_spec.rb +22 -0
- data/spec/integration/automatic_validation/inferred_uniqueness_validation_spec.rb +52 -0
- data/spec/integration/automatic_validation/inferred_within_validation_spec.rb +39 -0
- data/spec/integration/automatic_validation/spec_helper.rb +57 -0
- data/spec/integration/block_validator/block_validator_spec.rb +32 -0
- data/spec/integration/block_validator/spec_helper.rb +5 -0
- data/spec/integration/conditional_validation/if_condition_spec.rb +63 -0
- data/spec/integration/conditional_validation/spec_helper.rb +5 -0
- data/spec/integration/confirmation_validator/confirmation_validator_spec.rb +76 -0
- data/spec/integration/confirmation_validator/spec_helper.rb +5 -0
- data/spec/integration/datamapper_models/association_validation_spec.rb +29 -0
- data/spec/integration/datamapper_models/inheritance_spec.rb +82 -0
- data/spec/integration/dirty_attributes/dirty_attributes_spec.rb +13 -0
- data/spec/integration/duplicated_validations/duplicated_validations_spec.rb +24 -0
- data/spec/integration/duplicated_validations/spec_helper.rb +5 -0
- data/spec/integration/format_validator/email_format_validator_spec.rb +139 -0
- data/spec/integration/format_validator/format_validator_spec.rb +64 -0
- data/spec/integration/format_validator/regexp_validator_spec.rb +33 -0
- data/spec/integration/format_validator/spec_helper.rb +5 -0
- data/spec/integration/format_validator/url_format_validator_spec.rb +93 -0
- data/spec/integration/length_validator/default_value_spec.rb +14 -0
- data/spec/integration/length_validator/equality_spec.rb +87 -0
- data/spec/integration/length_validator/error_message_spec.rb +22 -0
- data/spec/integration/length_validator/maximum_spec.rb +49 -0
- data/spec/integration/length_validator/minimum_spec.rb +54 -0
- data/spec/integration/length_validator/range_spec.rb +87 -0
- data/spec/integration/length_validator/spec_helper.rb +7 -0
- data/spec/integration/method_validator/method_validator_spec.rb +241 -0
- data/spec/integration/method_validator/spec_helper.rb +5 -0
- data/spec/integration/numeric_validator/equality_with_float_type_spec.rb +65 -0
- data/spec/integration/numeric_validator/equality_with_integer_type_spec.rb +41 -0
- data/spec/integration/numeric_validator/float_type_spec.rb +90 -0
- data/spec/integration/numeric_validator/gt_with_float_type_spec.rb +37 -0
- data/spec/integration/numeric_validator/gte_with_float_type_spec.rb +37 -0
- data/spec/integration/numeric_validator/integer_only_true_spec.rb +91 -0
- data/spec/integration/numeric_validator/integer_type_spec.rb +86 -0
- data/spec/integration/numeric_validator/lt_with_float_type_spec.rb +37 -0
- data/spec/integration/numeric_validator/lte_with_float_type_spec.rb +37 -0
- data/spec/integration/numeric_validator/spec_helper.rb +5 -0
- data/spec/integration/primitive_validator/primitive_validator_spec.rb +92 -0
- data/spec/integration/primitive_validator/spec_helper.rb +5 -0
- data/spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +118 -0
- data/spec/integration/required_field_validator/association_spec.rb +72 -0
- data/spec/integration/required_field_validator/boolean_type_value_spec.rb +155 -0
- data/spec/integration/required_field_validator/date_type_value_spec.rb +127 -0
- data/spec/integration/required_field_validator/datetime_type_value_spec.rb +127 -0
- data/spec/integration/required_field_validator/float_type_value_spec.rb +131 -0
- data/spec/integration/required_field_validator/integer_type_value_spec.rb +99 -0
- data/spec/integration/required_field_validator/plain_old_ruby_object_spec.rb +35 -0
- data/spec/integration/required_field_validator/shared_examples.rb +26 -0
- data/spec/integration/required_field_validator/spec_helper.rb +7 -0
- data/spec/integration/required_field_validator/string_type_value_spec.rb +167 -0
- data/spec/integration/required_field_validator/text_type_value_spec.rb +49 -0
- data/spec/integration/shared/default_validation_context.rb +13 -0
- data/spec/integration/shared/valid_and_invalid_model.rb +35 -0
- data/spec/integration/uniqueness_validator/spec_helper.rb +5 -0
- data/spec/integration/uniqueness_validator/uniqueness_validator_spec.rb +116 -0
- data/spec/integration/within_validator/spec_helper.rb +5 -0
- data/spec/integration/within_validator/within_validator_spec.rb +168 -0
- data/spec/public/resource_spec.rb +105 -0
- data/spec/rcov.opts +6 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/unit/contextual_validators/emptiness_spec.rb +50 -0
- data/spec/unit/contextual_validators/execution_spec.rb +48 -0
- data/spec/unit/contextual_validators/spec_helper.rb +37 -0
- data/spec/unit/generic_validator/equality_operator_spec.rb +26 -0
- data/spec/unit/generic_validator/optional_spec.rb +54 -0
- data/spec/unit/validation_errors/adding_spec.rb +54 -0
- data/spec/unit/validation_errors/emptiness_spec.rb +38 -0
- data/spec/unit/validation_errors/enumerable_spec.rb +32 -0
- data/spec/unit/validation_errors/reading_spec.rb +35 -0
- data/spec/unit/validation_errors/respond_to_spec.rb +15 -0
- data/spec/unit/validators/within_validator_spec.rb +23 -0
- data/tasks/spec.rake +38 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +256 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9a6e3b907d785ecb7d7391fe2b8bca97c6e3634e
|
4
|
+
data.tar.gz: 16609fac5103d7fcd7bfa76d2a1fc03f57a3940d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 21b19deb551d6a604fb7cef0aea03725c92754d07a11f28c1d2d19c77869736b2a032f092745c7ef32dd2a4b0db1d8ea443eb025e80ce39e80faa86d8d539ec9
|
7
|
+
data.tar.gz: 6e24b74280d713ec077723b54dc30b775f3932495e186008266e2c2f73bd7dc7d630bc45b7dd9dc9ca135b00b83312993ff1b25706ad43e3c96f53b068f92795
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
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
|
+
## Rubinius
|
17
|
+
*.rbc
|
18
|
+
|
19
|
+
## PROJECT::GENERAL
|
20
|
+
*.gem
|
21
|
+
coverage
|
22
|
+
rdoc
|
23
|
+
pkg
|
24
|
+
tmp
|
25
|
+
doc
|
26
|
+
log
|
27
|
+
.yardoc
|
28
|
+
measurements
|
29
|
+
|
30
|
+
## BUNDLER
|
31
|
+
.bundle
|
32
|
+
Gemfile.*
|
33
|
+
|
34
|
+
## PROJECT::SPECIFIC
|
35
|
+
spec/db/
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
SOURCE = ENV.fetch('SOURCE', :git).to_sym
|
8
|
+
REPO_POSTFIX = SOURCE == :path ? '' : '.git'
|
9
|
+
DATAMAPPER = SOURCE == :path ? Pathname(__FILE__).dirname.parent : 'http://github.com/ar-dm'
|
10
|
+
DM_VERSION = '~> 1.2.0'
|
11
|
+
DO_VERSION = '~> 0.10.4'
|
12
|
+
DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
|
13
|
+
CURRENT_BRANCH = ENV.fetch('GIT_BRANCH', 'master')
|
14
|
+
|
15
|
+
gem 'ardm-core', DM_VERSION, SOURCE => "#{DATAMAPPER}/ardm-core#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
|
16
|
+
|
17
|
+
group :development do
|
18
|
+
gem 'ardm-types', DM_VERSION, SOURCE => "#{DATAMAPPER}/ardm-types#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
|
19
|
+
end
|
20
|
+
|
21
|
+
group :datamapper do
|
22
|
+
|
23
|
+
adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
|
24
|
+
adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
|
25
|
+
|
26
|
+
if (do_adapters = DM_DO_ADAPTERS & adapters).any?
|
27
|
+
do_options = {}
|
28
|
+
do_options[:git] = "#{DATAMAPPER}/do#{REPO_POSTFIX}" if ENV['DO_GIT'] == 'true'
|
29
|
+
|
30
|
+
gem 'data_objects', DO_VERSION, do_options.dup
|
31
|
+
|
32
|
+
do_adapters.each do |adapter|
|
33
|
+
adapter = 'sqlite3' if adapter == 'sqlite'
|
34
|
+
gem "do_#{adapter}", DO_VERSION, do_options.dup
|
35
|
+
end
|
36
|
+
|
37
|
+
gem 'ardm-do-adapter', DM_VERSION, SOURCE => "#{DATAMAPPER}/ardm-do-adapter#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
|
38
|
+
end
|
39
|
+
|
40
|
+
adapters.each do |adapter|
|
41
|
+
gem "ardm-#{adapter}-adapter", DM_VERSION, SOURCE => "#{DATAMAPPER}/ardm-#{adapter}-adapter#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
|
42
|
+
end
|
43
|
+
|
44
|
+
plugins = ENV['PLUGINS'] || ENV['PLUGIN']
|
45
|
+
plugins = plugins.to_s.tr(',', ' ').split.push('ardm-migrations').uniq
|
46
|
+
|
47
|
+
plugins.each do |plugin|
|
48
|
+
gem plugin, DM_VERSION, SOURCE => "#{DATAMAPPER}/#{plugin}#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2007 Guy van den Berg
|
2
|
+
Copyright (c) 2011 DataMapper development team
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
This is a DataMapper plugin that provides validations for DataMapper model classes.
|
2
|
+
|
3
|
+
== Setup
|
4
|
+
DataMapper validation capabilities are automatically available for DataMapper resources when you require dm-validations' in your application. There is no need to manually include anything, every DataMapper::Resource will be able to handle validations once this gem got required.
|
5
|
+
|
6
|
+
== Specifying Model Validations
|
7
|
+
|
8
|
+
There are two primary ways to implement validations for your models
|
9
|
+
|
10
|
+
1) Placing validation methods with properties as params in your class
|
11
|
+
|
12
|
+
require 'dm-core'
|
13
|
+
require 'dm-validations'
|
14
|
+
|
15
|
+
class ProgrammingLanguage
|
16
|
+
include DataMapper::Resource
|
17
|
+
property :name, String
|
18
|
+
validates_presence_of :name
|
19
|
+
end
|
20
|
+
|
21
|
+
2) Using auto-validations, please see DataMapper::Validations::AutoValidations. Note that not all validations that are provided via validation methods, are also available as autovalidation options. If they are available, they're functionally equivalent though.
|
22
|
+
|
23
|
+
class ProgrammingLanguage
|
24
|
+
include DataMapper::Resource
|
25
|
+
property :name, String, :required => true
|
26
|
+
end
|
27
|
+
|
28
|
+
See all of the DataMapper::Validate module's XYZValidator(s) to learn about the complete collections of validators available to you.
|
29
|
+
|
30
|
+
== Validating
|
31
|
+
|
32
|
+
DataMapper validations, when included, alter the default save/create/update process for a model. Unless you specify a context the resource must be valid in the :default context before saving.
|
33
|
+
|
34
|
+
You may manually validate a resource using the valid? method, which will return true if the resource is valid, and false if it is invalid.
|
35
|
+
|
36
|
+
== Working with Validation Errors
|
37
|
+
|
38
|
+
If your validators find errors in your model, they will populate the DataMapper::Validations::ValidationErrors object that is available through each of your models via calls to your model's errors method.
|
39
|
+
|
40
|
+
For example:
|
41
|
+
|
42
|
+
my_account = Account.new(:name => "Jose")
|
43
|
+
if my_account.save
|
44
|
+
# my_account is valid and has been saved
|
45
|
+
else
|
46
|
+
my_account.errors.each do |e|
|
47
|
+
puts e
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
See DataMapper::Validations::ValidationErrors for all you can do with your model's
|
52
|
+
errors method.
|
53
|
+
|
54
|
+
== Contextual Validations
|
55
|
+
|
56
|
+
DataMapper Validations also provide a means of grouping your validations into
|
57
|
+
contexts. This enables you to run different sets of validations when you
|
58
|
+
need it. For instance, the same model may not only behave differently
|
59
|
+
when initially saved or saved on update, but also require special validation sets
|
60
|
+
for publishing, exporting, importing and so on.
|
61
|
+
|
62
|
+
Again, using our example for pure Ruby class validations:
|
63
|
+
|
64
|
+
class ProgrammingLanguage
|
65
|
+
|
66
|
+
include DataMapper::Resource
|
67
|
+
|
68
|
+
property :name, String
|
69
|
+
|
70
|
+
def ensure_allows_manual_memory_management
|
71
|
+
# ...
|
72
|
+
end
|
73
|
+
|
74
|
+
def ensure_allows_optional_parentheses
|
75
|
+
# ...
|
76
|
+
end
|
77
|
+
|
78
|
+
validates_presence_of :name
|
79
|
+
validates_with_method :ensure_allows_optional_parentheses, :when => [:implementing_a_dsl]
|
80
|
+
validates_with_method :ensure_allows_manual_memory_management, :when => [:doing_system_programming]
|
81
|
+
end
|
82
|
+
|
83
|
+
ProgrammingLanguage instance now use #valid? method with one of two context symbols:
|
84
|
+
|
85
|
+
@ruby.valid?(:implementing_a_dsl) # => true
|
86
|
+
@ruby.valid?(:doing_system_programming) # => false
|
87
|
+
|
88
|
+
@c.valid?(:implementing_a_dsl) # => false
|
89
|
+
@c.valid?(:doing_system_programming) # => true
|
90
|
+
|
91
|
+
Each context causes different set of validations to be triggered. If you don't
|
92
|
+
specify a context using :when, :on or :group options (they are all aliases and do
|
93
|
+
the same thing), default context name is :default. When you do model.valid? (without
|
94
|
+
specifying context explicitly), again, :default context is used. One validation
|
95
|
+
can be used in two, three or five contexts if you like:
|
96
|
+
|
97
|
+
class Book
|
98
|
+
|
99
|
+
include ::DataMapper::Resource
|
100
|
+
|
101
|
+
property :id, Serial
|
102
|
+
property :name, String
|
103
|
+
|
104
|
+
property :agreed_title, String
|
105
|
+
property :finished_toc, Boolean
|
106
|
+
|
107
|
+
# used in all contexts, including default
|
108
|
+
validates_presence_of :name, :when => [:default, :sending_to_print]
|
109
|
+
validates_presence_of :agreed_title, :when => [:sending_to_print]
|
110
|
+
|
111
|
+
validates_with_block :toc, :when => [:sending_to_print] do
|
112
|
+
if self.finished_toc
|
113
|
+
[true]
|
114
|
+
else
|
115
|
+
[false, "TOC must be finalized before you send a book to print"]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
In the example above, name is validated for presence in both :default context and
|
121
|
+
:sending_to_print context, while TOC related block validation and title presence validation
|
122
|
+
only take place in :sending_to_print context.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/dm-validations/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "ardm-validations"
|
6
|
+
gem.version = DataMapper::Validations::VERSION
|
7
|
+
|
8
|
+
gem.authors = ['Martin Emde', "Guy van den Berg"]
|
9
|
+
gem.email = ['me@martinemde.com', "vandenberg.guy [a] gmail [d] com"]
|
10
|
+
gem.description = "Ardm fork of dm-validations"
|
11
|
+
gem.summary = gem.description
|
12
|
+
gem.license = "MIT"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {spec}/*`.split("\n")
|
16
|
+
gem.extra_rdoc_files = %w[LICENSE README.rdoc]
|
17
|
+
|
18
|
+
gem.homepage = "http://github.com/ar-dm/ardm-validations"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.rubygems_version = "1.8.11"
|
21
|
+
|
22
|
+
gem.add_runtime_dependency 'ardm-core', '~> 1.2'
|
23
|
+
|
24
|
+
gem.add_development_dependency 'ardm-types', '~> 1.2'
|
25
|
+
gem.add_development_dependency 'rake', '~> 0.9'
|
26
|
+
gem.add_development_dependency 'rspec', '~> 1.3'
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'dm-validations'
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'dm-validations/support/ordered_hash'
|
3
|
+
require 'dm-validations/support/object'
|
4
|
+
|
5
|
+
require 'dm-validations/exceptions'
|
6
|
+
require 'dm-validations/validation_errors'
|
7
|
+
require 'dm-validations/contextual_validators'
|
8
|
+
require 'dm-validations/auto_validate'
|
9
|
+
require 'dm-validations/context'
|
10
|
+
|
11
|
+
require 'dm-validations/validators/generic_validator'
|
12
|
+
require 'dm-validations/validators/required_field_validator'
|
13
|
+
require 'dm-validations/validators/primitive_validator'
|
14
|
+
require 'dm-validations/validators/absent_field_validator'
|
15
|
+
require 'dm-validations/validators/confirmation_validator'
|
16
|
+
require 'dm-validations/validators/format_validator'
|
17
|
+
require 'dm-validations/validators/length_validator'
|
18
|
+
require 'dm-validations/validators/within_validator'
|
19
|
+
require 'dm-validations/validators/numeric_validator'
|
20
|
+
require 'dm-validations/validators/method_validator'
|
21
|
+
require 'dm-validations/validators/block_validator'
|
22
|
+
require 'dm-validations/validators/uniqueness_validator'
|
23
|
+
require 'dm-validations/validators/acceptance_validator'
|
24
|
+
|
25
|
+
module DataMapper
|
26
|
+
module Validations
|
27
|
+
|
28
|
+
Model.append_inclusions self
|
29
|
+
|
30
|
+
def self.included(model)
|
31
|
+
model.extend ClassMethods
|
32
|
+
end
|
33
|
+
|
34
|
+
# Ensures the object is valid for the context provided, and otherwise
|
35
|
+
# throws :halt and returns false.
|
36
|
+
#
|
37
|
+
# @api public
|
38
|
+
def save(context = default_validation_context)
|
39
|
+
model.validators.assert_valid(context)
|
40
|
+
Validations::Context.in_context(context) { super() }
|
41
|
+
end
|
42
|
+
|
43
|
+
# @api public
|
44
|
+
def update(attributes = {}, context = default_validation_context)
|
45
|
+
model.validators.assert_valid(context)
|
46
|
+
Validations::Context.in_context(context) { super(attributes) }
|
47
|
+
end
|
48
|
+
|
49
|
+
# @api private
|
50
|
+
def save_self(*)
|
51
|
+
if Validations::Context.any? && !valid?(model.validators.current_context)
|
52
|
+
false
|
53
|
+
else
|
54
|
+
super
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Return the ValidationErrors
|
59
|
+
#
|
60
|
+
# @api public
|
61
|
+
def errors
|
62
|
+
@errors ||= ValidationErrors.new(self)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Mark this resource as validatable. When we validate associations of a
|
66
|
+
# resource we can check if they respond to validatable? before trying to
|
67
|
+
# recursively validate them
|
68
|
+
#
|
69
|
+
# @api semipublic
|
70
|
+
def validatable?
|
71
|
+
true
|
72
|
+
end
|
73
|
+
|
74
|
+
# Alias for valid?(:default)
|
75
|
+
#
|
76
|
+
# TODO: deprecate
|
77
|
+
def valid_for_default?
|
78
|
+
valid?(:default)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Check if a resource is valid in a given context
|
82
|
+
#
|
83
|
+
# @api public
|
84
|
+
def valid?(context = :default)
|
85
|
+
model = respond_to?(:model) ? self.model : self.class
|
86
|
+
model.validators.execute(context, self)
|
87
|
+
end
|
88
|
+
|
89
|
+
# @api semipublic
|
90
|
+
def validation_property_value(name)
|
91
|
+
__send__(name) if respond_to?(name, true)
|
92
|
+
end
|
93
|
+
|
94
|
+
module ClassMethods
|
95
|
+
include DataMapper::Validations::ValidatesPresence
|
96
|
+
include DataMapper::Validations::ValidatesAbsence
|
97
|
+
include DataMapper::Validations::ValidatesConfirmation
|
98
|
+
include DataMapper::Validations::ValidatesPrimitiveType
|
99
|
+
include DataMapper::Validations::ValidatesAcceptance
|
100
|
+
include DataMapper::Validations::ValidatesFormat
|
101
|
+
include DataMapper::Validations::ValidatesLength
|
102
|
+
include DataMapper::Validations::ValidatesWithin
|
103
|
+
include DataMapper::Validations::ValidatesNumericality
|
104
|
+
include DataMapper::Validations::ValidatesWithMethod
|
105
|
+
include DataMapper::Validations::ValidatesWithBlock
|
106
|
+
include DataMapper::Validations::ValidatesUniqueness
|
107
|
+
include DataMapper::Validations::AutoValidations
|
108
|
+
|
109
|
+
# Return the set of contextual validators or create a new one
|
110
|
+
#
|
111
|
+
# @api public
|
112
|
+
def validators
|
113
|
+
@validators ||= ContextualValidators.new(self)
|
114
|
+
end
|
115
|
+
|
116
|
+
# @api private
|
117
|
+
def inherited(base)
|
118
|
+
super
|
119
|
+
self.validators.contexts.each do |context, validators|
|
120
|
+
validators.each do |v|
|
121
|
+
options = v.options.merge(:context => context)
|
122
|
+
base.validators.add(v.class, v.field_name, options)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# @api public
|
128
|
+
def create(attributes = {}, *args)
|
129
|
+
resource = new(attributes)
|
130
|
+
resource.save(*args)
|
131
|
+
resource
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
# Given a new context create an instance method of
|
137
|
+
# valid_for_<context>? which simply calls valid?(context)
|
138
|
+
# if it does not already exist
|
139
|
+
#
|
140
|
+
# @api private
|
141
|
+
def self.create_context_instance_methods(model, context)
|
142
|
+
# TODO: deprecate `valid_for_#{context}?`
|
143
|
+
# what's wrong with requiring the caller to pass the context as an arg?
|
144
|
+
# eg, `valid?(:context)`
|
145
|
+
# these methods are handy for symbol-based callbacks,
|
146
|
+
# eg. `:if => :valid_for_context?`
|
147
|
+
# but these methods are so trivial to add where needed, making it
|
148
|
+
# overkill to do this for all contexts on all validated objects.
|
149
|
+
context = context.to_sym
|
150
|
+
|
151
|
+
name = "valid_for_#{context}?"
|
152
|
+
present = model.respond_to?(:resource_method_defined) ? model.resource_method_defined?(name) : model.instance_methods.include?(name)
|
153
|
+
unless present
|
154
|
+
model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
155
|
+
def #{name} # def valid_for_signup?
|
156
|
+
valid?(#{context.inspect}) # valid?(:signup)
|
157
|
+
end # end
|
158
|
+
RUBY
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end # module ClassMethods
|
163
|
+
end # module Validations
|
164
|
+
|
165
|
+
# Provide a const alias for backward compatibility with plugins
|
166
|
+
# This is scheduled to go away though, definitely before 1.0
|
167
|
+
Validate = Validations
|
168
|
+
|
169
|
+
end # module DataMapper
|