Module: DataMapper::Validate::ClassMethods

Public Visibility

Public Instance Method Summary

#add_validator_to_context(opts, fields, klazz)

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)

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)

Clean up the argument list and return a opts hash, including the merging of any default opts.

#validators

Return the set of contextual validators or create a new one.

Public Instance Methods Included from DataMapper::Validate::ValidatesPresent

validates_present

Public Instance Methods Included from DataMapper::Validate::ValidatesAbsent

validates_absent

Public Instance Methods Included from DataMapper::Validate::ValidatesFormat

validates_format

Public Instance Methods Included from DataMapper::Validate::ValidatesLength

validates_length

Public Instance Methods Included from DataMapper::Validate::ValidatesWithin

validates_within

Public Instance Methods Included from DataMapper::Validate::ValidatesIsNumber

validates_is_number

Public Instance Methods Included from DataMapper::Validate::ValidatesIsUnique

validates_is_unique

Public Instance Methods Included from DataMapper::Validate::AutoValidate

auto_generate_validations

Public Instance Method Details

add_validator_to_context

public add_validator_to_context(opts, fields, klazz)

Create a new validator of the given klazz and push it onto the requested context for each of the attributes in the fields list

[View source]


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'dm-more/dm-validations/lib/dm-validations.rb', line 172

def add_validator_to_context(opts, fields, klazz)
  fields.each do |field|
    if opts[:context].is_a?(Symbol)




      validators.context(opts[:context]) << klazz.new(field, opts)
      create_context_instance_methods(opts[:context])
    elsif opts[:context].is_a?(Array)
      opts[:context].each do |c|
        validators.context(c) << klazz.new(field, opts)
        create_context_instance_methods(c)
      end
    end
  end
end

create_context_instance_methods

public create_context_instance_methods(context)

Given a new context create an instance method of valid_for_<context>? which simply calls valid?(context) if it does not already exist

[View source]


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'dm-more/dm-validations/lib/dm-validations.rb', line 147

def create_context_instance_methods(context)
  name = "valid_for_#{context.to_s}?"
  if !self.instance_methods.include?(name)




    class_eval "def \#{name}\nvalid?('\#{context.to_s}'.to_sym)\nend\n"
  end

  all = "all_valid_for_#{context.to_s}?"
  if !self.instance_methods.include?(all)




    class_eval "def \#{all}\nall_valid?('\#{context.to_s}'.to_sym)\nend\n"
  end
end

opts_from_validator_args

public opts_from_validator_args(args, defaults = nil)

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

[View source]


131
132
133
134
135
136
137
138
139
140
141
# File 'dm-more/dm-validations/lib/dm-validations.rb', line 131

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

public validators

Return the set of contextual validators or create a new one

[View source]


123
124
125
126
127
128
# File 'dm-more/dm-validations/lib/dm-validations.rb', line 123

def validators



  @validations ||= ContextualValidators.new
end