Module: NotNaughty::ClassMethods

Defined in:
lib/not_naughty/class_methods.rb

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update(validation) ⇒ Object

:nodoc:



4
5
6
7
8
9
10
# File 'lib/not_naughty/class_methods.rb', line 4

def self.update(validation) # :nodoc:
  if basename = validation.name[/([^:]+)Validation$/, 1]
    define_method 'validates_%s_of' % basename.downcase do |*params|
      validator.add_validation(validation, *params)
    end
  end
end

Instance Method Details

#validates(*params, &block) ⇒ Object

Syntactic sugar.

Example:

validates { presence_of :example, :if => :reading? }
  # => validate_presence_of :example, :if => :reading?
validates(:name) { presence and length :minimum => 6 }
  # => validates_presence_of :name
       validates_length_of :name, :minimum => 6
validates(:temp, :if => water?) { value :lt => 100 }
  # => validates_value_of :temp, :lt => 100, :if => water?


22
23
24
# File 'lib/not_naughty/class_methods.rb', line 22

def validates(*params, &block)
  Builder.new(self, *params).instance_eval(&block)
end

#validates_each(*attributes, &block) ⇒ Object

Allows adding validations the legacy way.



27
28
29
# File 'lib/not_naughty/class_methods.rb', line 27

def validates_each(*attributes, &block)
  validator.add_validation(*attributes, &block)
end