Module: DataMapper::Validate::ClassMethods
- Includes:
- AutoValidate, ValidatesAbsent, ValidatesFormat, ValidatesIsAccepted, ValidatesIsConfirmed, ValidatesIsNumber, ValidatesIsPrimitive, ValidatesIsUnique, ValidatesLength, ValidatesPresent, ValidatesWithBlock, ValidatesWithMethod, ValidatesWithin
- Defined in:
- lib/dm-validations.rb
Instance Attribute Summary
Attributes included from AutoValidate
Instance Method Summary collapse
-
#add_validator_to_context(opts, fields, klazz) ⇒ Object
Create a new validator of the given klazz and push it onto the requested context for each of the attributes in the fields list.
-
#create_context_instance_methods(context) ⇒ Object
Given a new context create an instance method of valid_for_<context>? which simply calls valid?(context) if it does not already exist.
-
#opts_from_validator_args(args, defaults = nil) ⇒ Object
Clean up the argument list and return a opts hash, including the merging of any default opts.
-
#validators ⇒ Object
Return the set of contextual validators or create a new one.
Methods included from AutoValidate
#auto_generate_validations, #disabled_auto_validations?, #infer_format_validation_for, #infer_length_validation_for, #infer_presence_validation_for, #infer_type_validation_for, #infer_uniqueness_validation_for, #infer_within_validation_for, #options_with_message, #skip_auto_validation_for?, #without_auto_validations
Methods included from ValidatesIsUnique
Methods included from ValidatesWithBlock
Methods included from ValidatesWithMethod
Methods included from ValidatesIsNumber
Methods included from ValidatesWithin
Methods included from ValidatesLength
Methods included from ValidatesFormat
Methods included from ValidatesIsAccepted
Methods included from ValidatesIsPrimitive
Methods included from ValidatesIsConfirmed
Methods included from ValidatesAbsent
Methods included from ValidatesPresent
Instance Method Details
#add_validator_to_context(opts, fields, klazz) ⇒ Object
Create a new validator of the given klazz and push it onto the requested context for each of the attributes in the fields list
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/dm-validations.rb', line 212 def add_validator_to_context(opts, fields, klazz) fields.each do |field| validator = klazz.new(field, opts.dup) if opts[:context].is_a?(Symbol) unless validators.context(opts[:context]).include?(validator) validators.context(opts[:context]) << validator create_context_instance_methods(opts[:context]) end elsif opts[:context].is_a?(Array) opts[:context].each do |c| unless validators.context(c).include?(validator) validators.context(c) << validator create_context_instance_methods(c) end end end end end |
#create_context_instance_methods(context) ⇒ Object
Given a new context create an instance method of valid_for_<context>? which simply calls valid?(context) if it does not already exist
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/dm-validations.rb', line 180 def create_context_instance_methods(context) name = "valid_for_#{context.to_s}?" # valid_for_signup? if !instance_methods.include?(name) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} # def valid_for_signup? valid?(#{context.to_sym.inspect}) # valid?(:signup) end # end RUBY end all = "all_valid_for_#{context.to_s}?" # all_valid_for_signup? if !instance_methods.include?(all) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{all} # def all_valid_for_signup? all_valid?(#{context.to_sym.inspect}) # all_valid?(:signup) end # end RUBY end end |
#opts_from_validator_args(args, defaults = nil) ⇒ Object
Clean up the argument list and return a opts hash, including the merging of any default opts. Set the context to default if none is provided. Also allow :context to be aliased to :on, :when & group
168 169 170 171 172 173 174 |
# File 'lib/dm-validations.rb', line 168 def opts_from_validator_args(args, defaults = nil) opts = args.last.kind_of?(Hash) ? args.pop : {} context = opts.delete(:group) || opts.delete(:on) || opts.delete(:when) || opts.delete(:context) || :default opts[:context] = context opts.merge!(defaults) unless defaults.nil? opts end |
#validators ⇒ Object
Return the set of contextual validators or create a new one
160 161 162 |
# File 'lib/dm-validations.rb', line 160 def validators @validations ||= ContextualValidators.new end |