Module: CouchRest::Validation::ClassMethods

Includes:
AutoValidate, ValidatesAbsent, ValidatesFormat, ValidatesIsConfirmed, ValidatesIsNumber, ValidatesLength, ValidatesPresent, ValidatesWithMethod
Defined in:
lib/couchrest/validation.rb

Instance Method Summary (collapse)

Methods included from AutoValidate

#auto_generate_validations, #options_with_message

Methods included from ValidatesWithMethod

#validates_with_method

Methods included from ValidatesIsNumber

#validates_is_number, #validates_numericality_of

Methods included from ValidatesLength

#validates_length, #validates_length_of

Methods included from ValidatesFormat

#validates_format, #validates_format_of

Methods included from ValidatesIsConfirmed

#validates_confirmation_of, #validates_is_confirmed

Methods included from ValidatesAbsent

#validates_absent

Methods included from ValidatesPresent

#validates_presence_of, #validates_present

Instance Method Details

- (Object) 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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/couchrest/validation.rb', line 224

def add_validator_to_context(opts, fields, klazz)
  fields.each do |field|
    validator = klazz.new(field.to_sym, 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

- (Object) 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



210
211
212
213
214
215
216
217
218
219
# File 'lib/couchrest/validation.rb', line 210

def create_context_instance_methods(context)
  name = "valid_for_#{context.to_s}?"           # valid_for_signup?
  if !self.instance_methods.include?(name)
    class_eval <<-EOS, __FILE__, __LINE__ + 1
      def #{name}                               # def valid_for_signup?
        valid?('#{context.to_s}'.to_sym)        #   valid?('signup'.to_sym)
      end                                       # end
    EOS
  end
end

- (Object) 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



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/couchrest/validation.rb', line 194

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.merge!(defaults) unless defaults.nil?
  opts
end

- (Object) validators

Return the set of contextual validators or create a new one



186
187
188
# File 'lib/couchrest/validation.rb', line 186

def validators
  @validations ||= ContextualValidators.new
end