Module: NotNaughty::Builder

Defined in:
lib/not_naughty/builder.rb

Overview

Builder that builds

With this you get syntactical sugar for all descendants of Validation, see validates for examples.

Defined Under Namespace

Classes: ValidationDelegator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update(validation) ⇒ Object

Observer method that creates Validation builder methods.

A Validation with the class name TestValidation will get the builder method validate_test_of that adds a validation via add_validation in the current validator.



14
15
16
17
18
19
20
# File 'lib/not_naughty/builder.rb', line 14

def self.update(validation)
  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?


32
33
34
# File 'lib/not_naughty/builder.rb', line 32

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

#validates_each(*attributes, &block) ⇒ Object

Allows adding validations the legacy way.



37
38
39
# File 'lib/not_naughty/builder.rb', line 37

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