Module: DataMapper::Validate::ClassMethods
- Includes:
- AutoValidate, ValidatesAbsent, ValidatesFormat, ValidatesIsAccepted, ValidatesIsConfirmed, ValidatesIsNumber, ValidatesIsPrimitive, ValidatesIsUnique, ValidatesLength, ValidatesPresent, ValidatesWithBlock, ValidatesWithMethod, ValidatesWithin
- Defined in:
- lib/gems/dm-validations-0.9.9/lib/dm-validations.rb
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, #options_with_message, #orig_auto_generate_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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations.rb', line 211 def add_validator_to_context(opts, fields, klazz) fields.each do |field| validator = klazz.new(field, opts) 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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations.rb', line 188 def create_context_instance_methods(context) name = "valid_for_#{context.to_s}?" if !self.instance_methods.include?(name) class_eval <<-EOS, __FILE__, __LINE__ def #{name} valid?('#{context.to_s}'.to_sym) end EOS end all = "all_valid_for_#{context.to_s}?" if !self.instance_methods.include?(all) class_eval <<-EOS, __FILE__, __LINE__ def #{all} all_valid?('#{context.to_s}'.to_sym) end EOS 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
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations.rb', line 172 def opts_from_validator_args(args, defaults = nil) opts = args.last.kind_of?(Hash) ? args.pop : {} context = :default context = opts[:context] if opts.has_key?(:context) context = opts.delete(:on) if opts.has_key?(:on) context = opts.delete(:when) if opts.has_key?(:when) context = opts.delete(:group) if opts.has_key?(:group) opts[:context] = context opts.mergs!(defaults) unless defaults.nil? opts end |
#validators ⇒ Object
Return the set of contextual validators or create a new one
164 165 166 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations.rb', line 164 def validators @validations ||= ContextualValidators.new end |