Skip to content

Instantly share code, notes, and snippets.

@myabc
Created November 13, 2009 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myabc/234230 to your computer and use it in GitHub Desktop.
Save myabc/234230 to your computer and use it in GitHub Desktop.
Bug Repo: DummyDynamicScope should never be used for backref storage
# Bug Repo: DummyDynamicScope should never be used for backref storage
require 'rubygems'
require 'dm-core'
require 'dm-validations'
nv = DataMapper::Validate::NumericValidator.new("field_name",{})
10.times do # 5.times works
nv.send(:validate_with, :integer, 0, [])
[ :gt, :lt, :gte, :lte, :eq, :ne ].each do |validation_type|
nv.send(:validate_with, validation_type, 0, [])
end
end
require 'bigdecimal'
#require 'rubygems'
#require 'extlib'
#require 'dm-validations/validation_errors'
class ValidationErrors
include Enumerable
@@default_error_messages = {
:absent => '%s must be absent',
:inclusion => '%s must be one of %s',
:invalid => '%s has an invalid format',
:confirmation => '%s does not match the confirmation',
:accepted => '%s is not accepted',
:nil => '%s must not be nil',
:blank => '%s must not be blank',
:length_between => '%s must be between %s and %s characters long',
:too_long => '%s must be at most %s characters long',
:too_short => '%s must be at least %s characters long',
:wrong_length => '%s must be %s characters long',
:taken => '%s is already taken',
:not_a_number => '%s must be a number',
:not_an_integer => '%s must be an integer',
:greater_than => '%s must be greater than %s',
:greater_than_or_equal_to => '%s must be greater than or equal to %s',
:equal_to => '%s must be equal to %s',
:not_equal_to => '%s must not be equal to %s',
:less_than => '%s must be less than %s',
:less_than_or_equal_to => '%s must be less than or equal to %s',
:value_between => '%s must be between %s and %s',
:primitive => '%s must be of type %s'
}
# Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
# cattr_writer :default_error_messages
def self.default_error_message(key, field, *values)
# field = Extlib::Inflection.humanize(field)
@@default_error_messages[key] % [field, *values].flatten
end
end
def validate_with_comparison(value, cmp, expected, error_message_name, errors, negated = false)
return if expected.nil?
comparison = value.send(cmp, expected)
return if negated ? !comparison : comparison
errors << ValidationErrors.default_error_message(error_message_name, "field_name", expected)
end
def value_as_string(value)
case value
when Float then value.to_d.to_s('F') # Avoid Scientific Notation in Float to_s
when BigDecimal then value.to_s('F')
else value.to_s
end
end
# WORKS: validate_with_comparison(0, :=~, /\A[+-]?\d+\z/, :not_an_integer, [])
# DOES NOT WORK:
validate_with_comparison(value_as_string(0), :=~, /\A[+-]?\d+\z/, :not_an_integer, [])
class ValidationErrors
def self.default_error_message(*values)
# do something
end
def error_message(*values)
end
end
def validate_with_comparison(value, cmp, expected)
comparison = value.send(cmp, expected)
puts expected # DOES NOT FAIL
# ValidationErrors.new.error_message(expected) # Calling #error_message
# first and below no longer
# fails.
ValidationErrors.default_error_message(expected) # FAILS
end
def value_as_string(value)
value.to_s
end
# DOES NOT FAIL:
validate_with_comparison(0, :=~, /\A[+-]?\d+\z/)
# FAILS:
validate_with_comparison(value_as_string(0), :=~, /\A[+-]?\d+\z/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment