module DataMapper::Validations::ClassMethods

Private Class Methods

create_context_instance_methods(model, context) click to toggle source

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

@api private

# File lib/dm-validations.rb, line 141
      def self.create_context_instance_methods(model, context)
        # TODO: deprecate `valid_for_#{context}?`
        # what's wrong with requiring the caller to pass the context as an arg?
        #   eg, `valid?(:context)`
        # these methods are handy for symbol-based callbacks,
        #   eg. `:if => :valid_for_context?`
        # but these methods are so trivial to add where needed, making it
        # overkill to do this for all contexts on all validated objects.
        context = context.to_sym

        name = "valid_for_#{context}?"
        present = model.respond_to?(:resource_method_defined) ? model.resource_method_defined?(name) : model.instance_methods.include?(name)
        unless present
          model.class_eval "            def #{name}                         # def valid_for_signup?
              valid?(#{context.inspect})        #   valid?(:signup)
            end                                 # end
", __FILE__, __LINE__ + 1
        end
      end

Public Instance Methods

create(attributes = {}, *args) click to toggle source

@api public

# File lib/dm-validations.rb, line 128
def create(attributes = {}, *args)
  resource = new(attributes)
  resource.save(*args)
  resource
end
inherited(base) click to toggle source

@api private

Calls superclass method
# File lib/dm-validations.rb, line 117
def inherited(base)
  super
  self.validators.contexts.each do |context, validators|
    validators.each do |v|
      options = v.options.merge(:context => context)
      base.validators.add(v.class, v.field_name, options)
    end
  end
end
validators() click to toggle source

Return the set of contextual validators or create a new one

@api public

# File lib/dm-validations.rb, line 112
def validators
  @validators ||= ContextualValidators.new(self)
end