Module: DataMapper::Validate::ClassMethods

Instance Method Summary collapse

Methods included from AutoValidate

#auto_generate_validations

Methods included from ValidatesIsUnique

#validates_is_unique

Methods included from ValidatesWithMethod

#validates_with_method

Methods included from ValidatesIsNumber

#validates_is_number

Methods included from ValidatesWithin

#validates_within

Methods included from ValidatesLength

#validates_length

Methods included from ValidatesFormat

#validates_format

Methods included from ValidatesIsAccepted

#validates_is_accepted

Methods included from ValidatesIsPrimitive

#validates_is_primitive

Methods included from ValidatesIsConfirmed

#validates_is_confirmed

Methods included from ValidatesAbsent

#validates_absent

Methods included from ValidatesPresent

#validates_present

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



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/dm-validations.rb', line 190

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dm-validations.rb', line 167

def create_context_instance_methods(context)
  name = "valid_for_#{context.to_s}?"
  if !self.instance_methods.include?(name)
    class_eval <<-EOS
      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
      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



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dm-validations.rb', line 151

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

#validatorsObject

Return the set of contextual validators or create a new one



143
144
145
# File 'lib/dm-validations.rb', line 143

def validators
  @validations ||= ContextualValidators.new
end