mack-data_mapper 0.8.1 → 0.8.2
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/lib/gems/addressable-2.0.0/lib/addressable/idna.rb +4867 -0
- data/lib/gems/addressable-2.0.0/lib/addressable/uri.rb +2469 -0
- data/lib/gems/addressable-2.0.0/lib/addressable/version.rb +35 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/adapters/data_objects_adapter.rb +85 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/aggregate_functions.rb +201 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/collection.rb +11 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/model.rb +11 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/repository.rb +7 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/support/symbol.rb +21 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/version.rb +7 -0
- data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates.rb +15 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/abstract_adapter.rb +209 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/data_objects_adapter.rb +709 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/in_memory_adapter.rb +87 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/mysql_adapter.rb +136 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/postgres_adapter.rb +188 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/sqlite3_adapter.rb +105 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters.rb +22 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/many_to_many.rb +147 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/many_to_one.rb +107 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/one_to_many.rb +318 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/one_to_one.rb +61 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/relationship.rb +223 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/relationship_chain.rb +81 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/associations.rb +200 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/auto_migrations.rb +105 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/collection.rb +642 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/dependency_queue.rb +32 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/hook.rb +11 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/identity_map.rb +42 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/is.rb +16 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/logger.rb +232 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/migrations/destructive_migrations.rb +17 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/migrator.rb +29 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/model.rb +488 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/naming_conventions.rb +84 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/property.rb +663 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/property_set.rb +169 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/query.rb +628 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/repository.rb +159 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/resource.rb +637 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/scope.rb +58 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/support/array.rb +13 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/support/assertions.rb +8 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/support/errors.rb +23 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/support/kernel.rb +11 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/support/symbol.rb +41 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/support.rb +7 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/transaction.rb +267 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/type.rb +160 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/type_map.rb +80 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/boolean.rb +7 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/discriminator.rb +34 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/object.rb +24 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/paranoid_boolean.rb +34 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/paranoid_datetime.rb +33 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/serial.rb +9 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types/text.rb +10 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/types.rb +19 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core/version.rb +3 -0
- data/lib/gems/dm-core-0.9.7/lib/dm-core.rb +217 -0
- data/lib/gems/dm-core-0.9.7/script/all +5 -0
- data/lib/gems/dm-core-0.9.7/script/performance.rb +284 -0
- data/lib/gems/dm-core-0.9.7/script/profile.rb +87 -0
- data/lib/gems/dm-migrations-0.9.7/lib/dm-migrations/version.rb +5 -0
- data/lib/gems/dm-migrations-0.9.7/lib/dm-migrations.rb +1 -0
- data/lib/gems/dm-migrations-0.9.7/lib/migration.rb +215 -0
- data/lib/gems/dm-migrations-0.9.7/lib/migration_runner.rb +88 -0
- data/lib/gems/dm-migrations-0.9.7/lib/spec/example/migration_example_group.rb +73 -0
- data/lib/gems/dm-migrations-0.9.7/lib/spec/matchers/migration_matchers.rb +107 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/column.rb +9 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/mysql.rb +52 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/postgresql.rb +78 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/sqlite3.rb +43 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/table.rb +19 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/table_creator.rb +81 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql/table_modifier.rb +53 -0
- data/lib/gems/dm-migrations-0.9.7/lib/sql.rb +10 -0
- data/lib/gems/dm-observer-0.9.7/lib/dm-observer/version.rb +5 -0
- data/lib/gems/dm-observer-0.9.7/lib/dm-observer.rb +91 -0
- data/lib/gems/dm-serializer-0.9.7/lib/dm-serializer/version.rb +5 -0
- data/lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb +183 -0
- data/lib/gems/dm-timestamps-0.9.7/lib/dm-timestamps/version.rb +5 -0
- data/lib/gems/dm-timestamps-0.9.7/lib/dm-timestamps.rb +57 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/bcrypt_hash.rb +31 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/csv.rb +28 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/enum.rb +70 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/epoch_time.rb +27 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/file_path.rb +27 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/flag.rb +61 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/ip_address.rb +30 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/json.rb +40 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/regexp.rb +20 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/serial.rb +8 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/slug.rb +37 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/uri.rb +29 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/uuid.rb +64 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/version.rb +5 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types/yaml.rb +36 -0
- data/lib/gems/dm-types-0.9.7/lib/dm-types.rb +28 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/absent_field_validator.rb +60 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/acceptance_validator.rb +76 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/auto_validate.rb +153 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/block_validator.rb +60 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/confirmation_validator.rb +80 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb +56 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/custom_validator.rb +72 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/format_validator.rb +97 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/formats/email.rb +40 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/formats/url.rb +20 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/generic_validator.rb +100 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/length_validator.rb +113 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/method_validator.rb +68 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/numeric_validator.rb +83 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/primitive_validator.rb +60 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/required_field_validator.rb +88 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/support/object.rb +5 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/uniqueness_validator.rb +64 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/validation_errors.rb +63 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/version.rb +5 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations/within_validator.rb +53 -0
- data/lib/gems/dm-validations-0.9.7/lib/dm-validations.rb +234 -0
- data/lib/gems/json_pure-1.1.3/GPL +340 -0
- data/lib/gems/json_pure-1.1.3/VERSION +1 -0
- data/lib/gems/json_pure-1.1.3/bin/edit_json.rb +10 -0
- data/lib/gems/json_pure-1.1.3/bin/prettify_json.rb +76 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Array.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/FalseClass.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Hash.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Key.xpm +73 -0
- data/lib/gems/json_pure-1.1.3/lib/json/NilClass.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/Numeric.xpm +28 -0
- data/lib/gems/json_pure-1.1.3/lib/json/String.xpm +96 -0
- data/lib/gems/json_pure-1.1.3/lib/json/TrueClass.xpm +21 -0
- data/lib/gems/json_pure-1.1.3/lib/json/add/core.rb +135 -0
- data/lib/gems/json_pure-1.1.3/lib/json/add/rails.rb +58 -0
- data/lib/gems/json_pure-1.1.3/lib/json/common.rb +354 -0
- data/lib/gems/json_pure-1.1.3/lib/json/editor.rb +1362 -0
- data/lib/gems/json_pure-1.1.3/lib/json/ext.rb +13 -0
- data/lib/gems/json_pure-1.1.3/lib/json/json.xpm +1499 -0
- data/lib/gems/json_pure-1.1.3/lib/json/pure/generator.rb +394 -0
- data/lib/gems/json_pure-1.1.3/lib/json/pure/parser.rb +259 -0
- data/lib/gems/json_pure-1.1.3/lib/json/pure.rb +75 -0
- data/lib/gems/json_pure-1.1.3/lib/json/version.rb +9 -0
- data/lib/gems/json_pure-1.1.3/lib/json.rb +235 -0
- data/lib/gems/launchy-0.3.2/bin/launchy +12 -0
- data/lib/gems/launchy-0.3.2/lib/launchy/application.rb +163 -0
- data/lib/gems/launchy-0.3.2/lib/launchy/browser.rb +85 -0
- data/lib/gems/launchy-0.3.2/lib/launchy/command_line.rb +48 -0
- data/lib/gems/launchy-0.3.2/lib/launchy/gemspec.rb +53 -0
- data/lib/gems/launchy-0.3.2/lib/launchy/specification.rb +133 -0
- data/lib/gems/launchy-0.3.2/lib/launchy/version.rb +18 -0
- data/lib/gems/launchy-0.3.2/lib/launchy.rb +58 -0
- data/lib/gems/uuidtools-1.0.3/lib/uuidtools/version.rb +32 -0
- data/lib/gems/uuidtools-1.0.3/lib/uuidtools.rb +648 -0
- data/lib/gems.rb +13 -0
- data/lib/mack-data_mapper/migration_generator/migration_generator.rb +5 -0
- data/lib/mack-data_mapper/migration_generator/templates/db/migrations/%=@migration_name%.rb.template +1 -1
- data/lib/mack-data_mapper/model_generator/manifest.yml +3 -3
- data/lib/mack-data_mapper/model_generator/model_generator.rb +8 -1
- data/lib/mack-data_mapper/model_generator/templates/model.rb.template +1 -1
- data/lib/mack-data_mapper/model_generator/templates/rspec.rb.template +1 -1
- data/lib/mack-data_mapper/model_generator/templates/test_case.rb.template +1 -1
- data/lib/mack-data_mapper.rb +3 -2
- data/lib/mack-data_mapper_tasks.rb +7 -0
- metadata +235 -86
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
|
|
4
|
+
##
|
|
5
|
+
#
|
|
6
|
+
# @author teamon
|
|
7
|
+
# @since 0.9
|
|
8
|
+
module ValidatesWithBlock
|
|
9
|
+
|
|
10
|
+
##
|
|
11
|
+
# Validate using the given block. The block given needs to return:
|
|
12
|
+
# [result::<Boolean>, Error Message::<String>]
|
|
13
|
+
#
|
|
14
|
+
# @example [Usage]
|
|
15
|
+
# require 'dm-validations'
|
|
16
|
+
#
|
|
17
|
+
# class Page
|
|
18
|
+
# include DataMapper::Resource
|
|
19
|
+
#
|
|
20
|
+
# property :zip_code, String
|
|
21
|
+
#
|
|
22
|
+
# validates_with_block do
|
|
23
|
+
# if @zip_code == "94301"
|
|
24
|
+
# true
|
|
25
|
+
# else
|
|
26
|
+
# [false, "You're in the wrong zip code"]
|
|
27
|
+
# end
|
|
28
|
+
# end
|
|
29
|
+
#
|
|
30
|
+
# # A call to valid? will return false and
|
|
31
|
+
# # populate the object's errors with "You're in the
|
|
32
|
+
# # wrong zip code" unless zip_code == "94301"
|
|
33
|
+
#
|
|
34
|
+
# # You can also specify field:
|
|
35
|
+
#
|
|
36
|
+
# validates_with_block :zip_code do
|
|
37
|
+
# if @zip_code == "94301"
|
|
38
|
+
# true
|
|
39
|
+
# else
|
|
40
|
+
# [false, "You're in the wrong zip code"]
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# # it will add returned error message to :zip_code field
|
|
45
|
+
#
|
|
46
|
+
def validates_with_block(*fields, &block)
|
|
47
|
+
@__validates_with_block_count ||= 0
|
|
48
|
+
@__validates_with_block_count += 1
|
|
49
|
+
# create method and pass it to MethodValidator
|
|
50
|
+
raise ArgumentError.new('You need to pass a block to validates_with_block method') unless block_given?
|
|
51
|
+
method_name = "__validates_with_block_#{@__validates_with_block_count}".to_sym
|
|
52
|
+
define_method(method_name, block)
|
|
53
|
+
opts = opts_from_validator_args(fields)
|
|
54
|
+
opts[:method] = method_name
|
|
55
|
+
add_validator_to_context(opts, fields.empty? ? [method_name] : fields, DataMapper::Validate::MethodValidator)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end # module ValidatesWithMethod
|
|
59
|
+
end # module Validate
|
|
60
|
+
end # module DataMapper
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
|
|
4
|
+
##
|
|
5
|
+
#
|
|
6
|
+
# @author Guy van den Berg
|
|
7
|
+
# @since 0.9
|
|
8
|
+
class ConfirmationValidator < GenericValidator
|
|
9
|
+
|
|
10
|
+
def initialize(field_name, options = {})
|
|
11
|
+
super
|
|
12
|
+
@options = options
|
|
13
|
+
@field_name, @confirm_field_name = field_name, (options[:confirm] || "#{field_name}_confirmation").to_sym
|
|
14
|
+
@options[:allow_nil] = true unless @options.has_key?(:allow_nil)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(target)
|
|
18
|
+
unless valid?(target)
|
|
19
|
+
error_message = @options[:message] || '%s does not match the confirmation'.t(Extlib::Inflection.humanize(@field_name))
|
|
20
|
+
add_error(target, error_message , @field_name)
|
|
21
|
+
return false
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
return true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def valid?(target)
|
|
28
|
+
field_value = target.instance_variable_get("@#{@field_name}")
|
|
29
|
+
return true if @options[:allow_nil] && field_value.nil?
|
|
30
|
+
return false if !@options[:allow_nil] && field_value.nil?
|
|
31
|
+
|
|
32
|
+
if target.class.properties.has_property?(@field_name)
|
|
33
|
+
return true unless target.attribute_dirty?(@field_name)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
confirm_value = target.instance_variable_get("@#{@confirm_field_name}")
|
|
37
|
+
field_value == confirm_value
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end # class ConfirmationValidator
|
|
41
|
+
|
|
42
|
+
module ValidatesIsConfirmed
|
|
43
|
+
|
|
44
|
+
##
|
|
45
|
+
# Validates that the given attribute is confirmed by another attribute.
|
|
46
|
+
# A common use case scenario is when you require a user to confirm their
|
|
47
|
+
# password, for which you use both password and password_confirmation
|
|
48
|
+
# attributes.
|
|
49
|
+
#
|
|
50
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
|
51
|
+
# @option :confirm<Symbol> the attribute that you want to validate
|
|
52
|
+
# against (default is firstattr_confirmation)
|
|
53
|
+
#
|
|
54
|
+
# @example [Usage]
|
|
55
|
+
# require 'dm-validations'
|
|
56
|
+
#
|
|
57
|
+
# class Page
|
|
58
|
+
# include DataMapper::Resource
|
|
59
|
+
#
|
|
60
|
+
# property :password, String
|
|
61
|
+
# property :email, String
|
|
62
|
+
# attr_accessor :password_confirmation
|
|
63
|
+
# attr_accessor :email_repeated
|
|
64
|
+
#
|
|
65
|
+
# validates_is_confirmed :password
|
|
66
|
+
# validates_is_confirmed :email, :confirm => :email_repeated
|
|
67
|
+
#
|
|
68
|
+
# # a call to valid? will return false unless:
|
|
69
|
+
# # password == password_confirmation
|
|
70
|
+
# # and
|
|
71
|
+
# # email == email_repeated
|
|
72
|
+
#
|
|
73
|
+
def validates_is_confirmed(*fields)
|
|
74
|
+
opts = opts_from_validator_args(fields)
|
|
75
|
+
add_validator_to_context(opts, fields, DataMapper::Validate::ConfirmationValidator)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end # module ValidatesIsConfirmed
|
|
79
|
+
end # module Validate
|
|
80
|
+
end # module DataMapper
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
|
|
4
|
+
##
|
|
5
|
+
#
|
|
6
|
+
# @author Guy van den Berg
|
|
7
|
+
# @since 0.9
|
|
8
|
+
class ContextualValidators
|
|
9
|
+
|
|
10
|
+
def dump
|
|
11
|
+
contexts.each_pair do |key,context|
|
|
12
|
+
puts "Key=#{key} Context: #{context}"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Get a hash of named context validators for the resource
|
|
17
|
+
#
|
|
18
|
+
# @return <Hash> a hash of validators <GenericValidator>
|
|
19
|
+
def contexts
|
|
20
|
+
@contexts ||= @contexts = {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Return an array of validators for a named context
|
|
24
|
+
#
|
|
25
|
+
# @return <Array> An array of validators
|
|
26
|
+
def context(name)
|
|
27
|
+
contexts[name] = [] unless contexts.has_key?(name)
|
|
28
|
+
contexts[name]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Clear all named context validators off of the resource
|
|
32
|
+
#
|
|
33
|
+
def clear!
|
|
34
|
+
contexts.clear
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Execute all validators in the named context against the target
|
|
38
|
+
#
|
|
39
|
+
# @param <Symbol> named_context the context we are validating against
|
|
40
|
+
# @param <Object> target the resource that we are validating
|
|
41
|
+
# @return <Boolean> true if all are valid, otherwise false
|
|
42
|
+
def execute(named_context, target)
|
|
43
|
+
raise(ArgumentError, 'invalid context specified') if !named_context || (contexts.length > 0 && !contexts[named_context])
|
|
44
|
+
target.errors.clear!
|
|
45
|
+
result = true
|
|
46
|
+
context(named_context).each do |validator|
|
|
47
|
+
next unless validator.execute?(target)
|
|
48
|
+
result = false unless validator.call(target)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
result
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
end # module ContextualValidators
|
|
55
|
+
end # module Validate
|
|
56
|
+
end # module DataMapper
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#require File.dirname(__FILE__) + '/formats/email'
|
|
2
|
+
|
|
3
|
+
module DataMapper
|
|
4
|
+
module Validate
|
|
5
|
+
|
|
6
|
+
##
|
|
7
|
+
#
|
|
8
|
+
# @author Guy van den Berg
|
|
9
|
+
# @since 0.9
|
|
10
|
+
class CustomValidator < GenericValidator
|
|
11
|
+
|
|
12
|
+
def initialize(field_name, options = {}, &b)
|
|
13
|
+
#super(field_name, options)
|
|
14
|
+
#@field_name, @options = field_name, options
|
|
15
|
+
#@options[:allow_nil] = false unless @options.has_key?(:allow_nil)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def call(target)
|
|
19
|
+
#field_value = target.instance_variable_get("@#{@field_name}")
|
|
20
|
+
#return true if @options[:allow_nil] && field_value.nil?
|
|
21
|
+
|
|
22
|
+
#validation = (@options[:as] || @options[:with])
|
|
23
|
+
#error_message = nil
|
|
24
|
+
|
|
25
|
+
# Figure out what to use as the actual validator.
|
|
26
|
+
# If a symbol is passed to :as, look up
|
|
27
|
+
# the canned validation in FORMATS.
|
|
28
|
+
#
|
|
29
|
+
#validator = if validation.is_a? Symbol
|
|
30
|
+
# if FORMATS[validation].is_a? Array
|
|
31
|
+
# error_message = FORMATS[validation][1]
|
|
32
|
+
# FORMATS[validation][0]
|
|
33
|
+
# else
|
|
34
|
+
# FORMATS[validation] || validation
|
|
35
|
+
# end
|
|
36
|
+
#else
|
|
37
|
+
# validation
|
|
38
|
+
#end
|
|
39
|
+
|
|
40
|
+
#valid = case validator
|
|
41
|
+
#when Proc then validator.call(field_value)
|
|
42
|
+
#when Regexp then validator =~ field_value
|
|
43
|
+
#else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}"
|
|
44
|
+
#end
|
|
45
|
+
|
|
46
|
+
#unless valid
|
|
47
|
+
# field = Inflector.humanize(@field_name)
|
|
48
|
+
# value = field_value
|
|
49
|
+
#
|
|
50
|
+
# error_message = @options[:message] || error_message || '%s is invalid'.t(field)
|
|
51
|
+
# error_message = error_message.call(field, value) if Proc === error_message
|
|
52
|
+
#
|
|
53
|
+
# add_error(target, error_message , @field_name)
|
|
54
|
+
#end
|
|
55
|
+
|
|
56
|
+
#return valid
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#class UnknownValidationFormat < StandardError; end
|
|
60
|
+
|
|
61
|
+
end # class CustomValidator
|
|
62
|
+
|
|
63
|
+
module ValidatesFormatOf
|
|
64
|
+
|
|
65
|
+
#def validates_format_of(*fields)
|
|
66
|
+
#opts = opts_from_validator_args(fields)
|
|
67
|
+
#add_validator_to_context(opts, fields, DataMapper::Validate::FormatValidator)
|
|
68
|
+
#end
|
|
69
|
+
|
|
70
|
+
end # module ValidatesFormatOf
|
|
71
|
+
end # module Validate
|
|
72
|
+
end # module DataMapper
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#require File.dirname(__FILE__) + '/formats/email'
|
|
2
|
+
|
|
3
|
+
require 'pathname'
|
|
4
|
+
require Pathname(__FILE__).dirname.expand_path + 'formats/email'
|
|
5
|
+
require Pathname(__FILE__).dirname.expand_path + 'formats/url'
|
|
6
|
+
|
|
7
|
+
module DataMapper
|
|
8
|
+
module Validate
|
|
9
|
+
|
|
10
|
+
##
|
|
11
|
+
#
|
|
12
|
+
# @author Guy van den Berg
|
|
13
|
+
# @since 0.9
|
|
14
|
+
class FormatValidator < GenericValidator
|
|
15
|
+
|
|
16
|
+
FORMATS = {}
|
|
17
|
+
include DataMapper::Validate::Format::Email
|
|
18
|
+
include DataMapper::Validate::Format::Url
|
|
19
|
+
|
|
20
|
+
def initialize(field_name, options = {}, &b)
|
|
21
|
+
super(field_name, options)
|
|
22
|
+
@field_name, @options = field_name, options
|
|
23
|
+
@options[:allow_nil] = false unless @options.has_key?(:allow_nil)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def call(target)
|
|
27
|
+
value = target.validation_property_value(@field_name)
|
|
28
|
+
return true if @options[:allow_nil] && value.nil?
|
|
29
|
+
|
|
30
|
+
validation = @options[:as] || @options[:with]
|
|
31
|
+
|
|
32
|
+
raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation)
|
|
33
|
+
validator = validation.is_a?(Symbol) ? FORMATS[validation][0] : validation
|
|
34
|
+
|
|
35
|
+
valid = case validator
|
|
36
|
+
when Proc then validator.call(value)
|
|
37
|
+
when Regexp then value =~ validator
|
|
38
|
+
else
|
|
39
|
+
raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
return true if valid
|
|
43
|
+
|
|
44
|
+
field = Extlib::Inflection.humanize(@field_name)
|
|
45
|
+
error_message = @options[:message] || '%s has an invalid format'.t(field)
|
|
46
|
+
error_message = error_message.call(field, value) if error_message.respond_to?(:call)
|
|
47
|
+
|
|
48
|
+
add_error(target, error_message, @field_name)
|
|
49
|
+
|
|
50
|
+
false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
#class UnknownValidationFormat < StandardError; end
|
|
54
|
+
|
|
55
|
+
end # class FormatValidator
|
|
56
|
+
|
|
57
|
+
module ValidatesFormat
|
|
58
|
+
|
|
59
|
+
##
|
|
60
|
+
# Validates that the attribute is in the specified format. You may use the
|
|
61
|
+
# :as (or :with, it's an alias) option to specify the pre-defined format
|
|
62
|
+
# that you want to validate against. You may also specify your own format
|
|
63
|
+
# via a Proc or Regexp passed to the the :as or :with options.
|
|
64
|
+
#
|
|
65
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
|
66
|
+
# @option :as<Format, Proc, Regexp> the pre-defined format, Proc or Regexp to validate against
|
|
67
|
+
# @option :with<Format, Proc, Regexp> an alias for :as
|
|
68
|
+
#
|
|
69
|
+
# @details [Pre-defined Formats]
|
|
70
|
+
# :email_address (format is specified in DataMapper::Validate::Format::Email)
|
|
71
|
+
# :url (format is specified in DataMapper::Validate::Format::Url)
|
|
72
|
+
#
|
|
73
|
+
# @example [Usage]
|
|
74
|
+
# require 'dm-validations'
|
|
75
|
+
#
|
|
76
|
+
# class Page
|
|
77
|
+
# include DataMapper::Resource
|
|
78
|
+
#
|
|
79
|
+
# property :email, String
|
|
80
|
+
# property :zip_code, String
|
|
81
|
+
#
|
|
82
|
+
# validates_format :email, :as => :email_address
|
|
83
|
+
# validates_format :zip_code, :with => /^\d{5}$/
|
|
84
|
+
#
|
|
85
|
+
# # a call to valid? will return false unless:
|
|
86
|
+
# # email is formatted like an email address
|
|
87
|
+
# # and
|
|
88
|
+
# # zip_code is a string of 5 digits
|
|
89
|
+
#
|
|
90
|
+
def validates_format(*fields)
|
|
91
|
+
opts = opts_from_validator_args(fields)
|
|
92
|
+
add_validator_to_context(opts, fields, DataMapper::Validate::FormatValidator)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
end # module ValidatesFormat
|
|
96
|
+
end # module Validate
|
|
97
|
+
end # module DataMapper
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
module Format
|
|
4
|
+
module Email
|
|
5
|
+
|
|
6
|
+
def self.included(base)
|
|
7
|
+
DataMapper::Validate::FormatValidator::FORMATS.merge!(
|
|
8
|
+
:email_address => [ EmailAddress, lambda { |field, value| '%s is not a valid email address'.t(value) }]
|
|
9
|
+
)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
EmailAddress = begin
|
|
13
|
+
alpha = "a-zA-Z"
|
|
14
|
+
digit = "0-9"
|
|
15
|
+
atext = "[#{alpha}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
|
|
16
|
+
dot_atom_text = "#{atext}+([.]#{atext}*)*"
|
|
17
|
+
dot_atom = "#{dot_atom_text}"
|
|
18
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
|
19
|
+
text = "[\\x01-\\x09\\x11\\x12\\x14-\\x7f]"
|
|
20
|
+
quoted_pair = "(\\x5c#{text})"
|
|
21
|
+
qcontent = "(?:#{qtext}|#{quoted_pair})"
|
|
22
|
+
quoted_string = "[\"]#{qcontent}+[\"]"
|
|
23
|
+
atom = "#{atext}+"
|
|
24
|
+
word = "(?:#{atom}|#{quoted_string})"
|
|
25
|
+
obs_local_part = "#{word}([.]#{word})*"
|
|
26
|
+
local_part = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
|
|
27
|
+
no_ws_ctl = "\\x01-\\x08\\x11\\x12\\x14-\\x1f\\x7f"
|
|
28
|
+
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
|
29
|
+
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
|
30
|
+
domain_literal = "\\[#{dcontent}+\\]"
|
|
31
|
+
obs_domain = "#{atom}([.]#{atom})*"
|
|
32
|
+
domain = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
|
|
33
|
+
addr_spec = "#{local_part}\@#{domain}"
|
|
34
|
+
pattern = /^#{addr_spec}$/
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end # module Email
|
|
38
|
+
end # module Format
|
|
39
|
+
end # module Validate
|
|
40
|
+
end # module DataMapper
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
module Format
|
|
4
|
+
module Url
|
|
5
|
+
|
|
6
|
+
def self.included(base)
|
|
7
|
+
DataMapper::Validate::FormatValidator::FORMATS.merge!(
|
|
8
|
+
:url => [ Url, lambda { |field, value| '%s is not a valid URL'.t(value) }]
|
|
9
|
+
)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
Url = begin
|
|
13
|
+
# Regex from http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/
|
|
14
|
+
/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end # module Url
|
|
18
|
+
end # module Format
|
|
19
|
+
end # module Validate
|
|
20
|
+
end # module DataMapper
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
|
|
4
|
+
# All validators extend this base class. Validators must:
|
|
5
|
+
#
|
|
6
|
+
# * Implement the initialize method to capture its parameters, also calling
|
|
7
|
+
# super to have this parent class capture the optional, general :if and
|
|
8
|
+
# :unless parameters.
|
|
9
|
+
# * Implement the call method, returning true or false. The call method
|
|
10
|
+
# provides the validation logic.
|
|
11
|
+
#
|
|
12
|
+
# @author Guy van den Berg
|
|
13
|
+
# @since 0.9
|
|
14
|
+
class GenericValidator
|
|
15
|
+
|
|
16
|
+
attr_accessor :if_clause
|
|
17
|
+
attr_accessor :unless_clause
|
|
18
|
+
|
|
19
|
+
# Construct a validator. Capture the :if and :unless clauses when present.
|
|
20
|
+
#
|
|
21
|
+
# @param field<String, Symbol> The property specified for validation
|
|
22
|
+
#
|
|
23
|
+
# @option :if<Symbol, Proc> The name of a method or a Proc to call to
|
|
24
|
+
# determine if the validation should occur.
|
|
25
|
+
# @option :unless<Symbol, Proc> The name of a method or a Proc to call to
|
|
26
|
+
# determine if the validation should not occur
|
|
27
|
+
# All additional key/value pairs are passed through to the validator
|
|
28
|
+
# that is sub-classing this GenericValidator
|
|
29
|
+
#
|
|
30
|
+
def initialize(field, opts = {})
|
|
31
|
+
@if_clause = opts.has_key?(:if) ? opts[:if] : nil
|
|
32
|
+
@unless_clause = opts.has_key?(:unless) ? opts[:unless] : nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Add an error message to a target resource. If the error corresponds to a
|
|
36
|
+
# specific field of the resource, add it to that field, otherwise add it
|
|
37
|
+
# as a :general message.
|
|
38
|
+
#
|
|
39
|
+
# @param <Object> target the resource that has the error
|
|
40
|
+
# @param <String> message the message to add
|
|
41
|
+
# @param <Symbol> field_name the name of the field that caused the error
|
|
42
|
+
#
|
|
43
|
+
# TODO - should the field_name for a general message be :default???
|
|
44
|
+
#
|
|
45
|
+
def add_error(target, message, field_name = :general)
|
|
46
|
+
target.errors.add(field_name,message)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Call the validator. "call" is used so the operation is BoundMethod and
|
|
50
|
+
# Block compatible. This must be implemented in all concrete classes.
|
|
51
|
+
#
|
|
52
|
+
# @param <Object> target the resource that the validator must be called
|
|
53
|
+
# against
|
|
54
|
+
# @return <Boolean> true if valid, otherwise false
|
|
55
|
+
def call(target)
|
|
56
|
+
raise "DataMapper::Validate::GenericValidator::call must be overriden in #{self.class.to_s}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def field_name
|
|
60
|
+
@field_name
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Determines if this validator should be run against the
|
|
64
|
+
# target by evaluating the :if and :unless clauses
|
|
65
|
+
# optionally passed while specifying any validator.
|
|
66
|
+
#
|
|
67
|
+
# @param <Object> target the resource that we check against
|
|
68
|
+
# @return <Boolean> true if should be run, otherwise false
|
|
69
|
+
def execute?(target)
|
|
70
|
+
if unless_clause = self.unless_clause
|
|
71
|
+
if unless_clause.is_a?(Symbol)
|
|
72
|
+
return false if target.send(unless_clause)
|
|
73
|
+
elsif unless_clause.respond_to?(:call)
|
|
74
|
+
return false if unless_clause.call(target)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
if if_clause = self.if_clause
|
|
79
|
+
if if_clause.is_a?(Symbol)
|
|
80
|
+
return target.send(if_clause)
|
|
81
|
+
elsif if_clause.respond_to?(:call)
|
|
82
|
+
return if_clause.call(target)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
true
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def ==(other)
|
|
90
|
+
self.class == other.class &&
|
|
91
|
+
self.field_name == other.field_name &&
|
|
92
|
+
self.class == other.class &&
|
|
93
|
+
self.if_clause == other.if_clause &&
|
|
94
|
+
self.unless_clause == other.unless_clause &&
|
|
95
|
+
self.instance_variable_get(:@options) == other.instance_variable_get(:@options)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
end # class GenericValidator
|
|
99
|
+
end # module Validate
|
|
100
|
+
end # module DataMapper
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
module DataMapper
|
|
2
|
+
module Validate
|
|
3
|
+
|
|
4
|
+
##
|
|
5
|
+
#
|
|
6
|
+
# @author Guy van den Berg
|
|
7
|
+
# @since 0.9
|
|
8
|
+
class LengthValidator < GenericValidator
|
|
9
|
+
|
|
10
|
+
def initialize(field_name, options)
|
|
11
|
+
super
|
|
12
|
+
@field_name = field_name
|
|
13
|
+
@options = options
|
|
14
|
+
|
|
15
|
+
@min = options[:minimum] || options[:min]
|
|
16
|
+
@max = options[:maximum] || options[:max]
|
|
17
|
+
@equal = options[:is] || options[:equals]
|
|
18
|
+
@range = options[:within] || options[:in]
|
|
19
|
+
|
|
20
|
+
@validation_method ||= :range if @range
|
|
21
|
+
@validation_method ||= :min if @min && @max.nil?
|
|
22
|
+
@validation_method ||= :max if @max && @min.nil?
|
|
23
|
+
@validation_method ||= :equals unless @equal.nil?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def call(target)
|
|
27
|
+
field_value = target.validation_property_value(@field_name)
|
|
28
|
+
return true if @options[:allow_nil] && field_value.nil?
|
|
29
|
+
|
|
30
|
+
field_value = '' if field_value.nil?
|
|
31
|
+
|
|
32
|
+
# XXX: HACK seems hacky to do this on every validation, probably should
|
|
33
|
+
# do this elsewhere?
|
|
34
|
+
field = Extlib::Inflection.humanize(@field_name)
|
|
35
|
+
min = @range ? @range.min : @min
|
|
36
|
+
max = @range ? @range.max : @max
|
|
37
|
+
equal = @equal
|
|
38
|
+
|
|
39
|
+
case @validation_method
|
|
40
|
+
when :range then
|
|
41
|
+
unless valid = @range.include?(field_value.size)
|
|
42
|
+
error_message = '%s must be between %s and %s characters long'.t(field, min, max)
|
|
43
|
+
end
|
|
44
|
+
when :min then
|
|
45
|
+
unless valid = field_value.size >= min
|
|
46
|
+
error_message = '%s must be more than %s characters long'.t(field, min)
|
|
47
|
+
end
|
|
48
|
+
when :max then
|
|
49
|
+
unless valid = field_value.size <= max
|
|
50
|
+
error_message = '%s must be less than %s characters long'.t(field, max)
|
|
51
|
+
end
|
|
52
|
+
when :equals then
|
|
53
|
+
unless valid = field_value.size == equal
|
|
54
|
+
error_message = '%s must be %s characters long'.t(field, equal)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
error_message = @options[:message] || error_message
|
|
59
|
+
|
|
60
|
+
add_error(target, error_message, @field_name) unless valid
|
|
61
|
+
|
|
62
|
+
return valid
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end # class LengthValidator
|
|
66
|
+
|
|
67
|
+
module ValidatesLength
|
|
68
|
+
|
|
69
|
+
# Validates that the length of the attribute is equal to, less than,
|
|
70
|
+
# greater than or within a certain range (depending upon the options
|
|
71
|
+
# you specify).
|
|
72
|
+
#
|
|
73
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
|
74
|
+
# @option :minimum ensures that the attribute's length is greater than
|
|
75
|
+
# or equal to the supplied value
|
|
76
|
+
# @option :min alias for :minimum
|
|
77
|
+
# @option :maximum ensures the attribute's length is less than or equal
|
|
78
|
+
# to the supplied value
|
|
79
|
+
# @option :max alias for :maximum
|
|
80
|
+
# @option :equals ensures the attribute's length is equal to the
|
|
81
|
+
# supplied value
|
|
82
|
+
# @option :is alias for :equals
|
|
83
|
+
# @option :in<Range> given a Range, ensures that the attributes length is
|
|
84
|
+
# include?'ed in the Range
|
|
85
|
+
# @option :within<Range> alias for :in
|
|
86
|
+
#
|
|
87
|
+
# @example [Usage]
|
|
88
|
+
# require 'dm-validations'
|
|
89
|
+
#
|
|
90
|
+
# class Page
|
|
91
|
+
# include DataMapper::Resource
|
|
92
|
+
#
|
|
93
|
+
# property high, Integer
|
|
94
|
+
# property low, Integer
|
|
95
|
+
# property just_right, Integer
|
|
96
|
+
#
|
|
97
|
+
# validates_length :high, :min => 100000000000
|
|
98
|
+
# validates_length :low, :equals => 0
|
|
99
|
+
# validates_length :just_right, :within => 1..10
|
|
100
|
+
#
|
|
101
|
+
# # a call to valid? will return false unless:
|
|
102
|
+
# # high is greater than or equal to 100000000000
|
|
103
|
+
# # low is equal to 0
|
|
104
|
+
# # just_right is between 1 and 10 (inclusive of both 1 and 10)
|
|
105
|
+
#
|
|
106
|
+
def validates_length(*fields)
|
|
107
|
+
opts = opts_from_validator_args(fields)
|
|
108
|
+
add_validator_to_context(opts, fields, DataMapper::Validate::LengthValidator)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
end # module ValidatesLength
|
|
112
|
+
end # module Validate
|
|
113
|
+
end # module DataMapper
|