Class: NotNaughty::Validation

Inherits:
Object
  • Object
show all
Extended by:
Observable
Defined in:
lib/not_naughty/validation.rb

Overview

The superclass for Validations.

See new for more information.

Defined Under Namespace

Classes: Condition

Constant Summary collapse

PATTERN =
File.join %w[%s ** %s_validation.rb]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, attributes, &block) ⇒ Validation

:nodoc:



70
71
72
73
# File 'lib/not_naughty/validation.rb', line 70

def initialize(opts, attributes, &block) #:nodoc:
  build_conditions opts[:if], opts[:unless]
  @attributes, @block, @opts = attributes, block, opts
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



68
69
70
# File 'lib/not_naughty/validation.rb', line 68

def attributes
  @attributes
end

Class Method Details

.inherited(descendant) ⇒ Object

:nodoc:



26
27
28
29
30
31
# File 'lib/not_naughty/validation.rb', line 26

def self.inherited(descendant) #:nodoc:
  changed and notify_observers(descendant)

  descendant.
  instance_variable_set :@observer_peers, @observer_peers.clone
end

.load(*validations) ⇒ Object

Loads validations from load_paths.



16
17
18
19
20
21
22
23
# File 'lib/not_naughty/validation.rb', line 16

def self.load(*validations)
  validations.each do |validation|
    @load_paths.each do |load_path|
      pattern = PATTERN % [load_path, validation]
      Dir[pattern].each { |validation_path| require validation_path }
    end
  end
end

.load_pathsObject

Returns array of paths which are scanned for validations by load.



9
10
11
# File 'lib/not_naughty/validation.rb', line 9

def self.load_paths
  @load_paths
end

.new(*params, &block) ⇒ Object

Builds validations.

Example:

NotNaughty::Validation.new :temp, :if => :water? do |obj, attr, val|
  obj.errors.add attr, 'too hot' unless val < 100
end

Like:

class TempValidation < NotNaughty::Validation
  def initialize(opts, attributes)
    super opts, attributes method(:temp_validation)
  end
  def temp_validation(obj, attr, val)
    obj.errors.add attr, 'too hot' unless val < 100
  end
end

Validation.new TempValidation, :temp, :if => :water?

The last one also notifies all Observers of Validation (see Builder#update). If Builder#update is called because <Name>Validation is inherited from Validation the ValidationBuilder gets the method validates_<name>_of and so does the classes that included the Builder.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/not_naughty/validation.rb', line 56

def self.new(*params, &block)
  attributes = if params.first.is_a? Class and params.first < self
    klass = params.shift
    klass.new(*params, &block)
  else
    options = params.extract_options!
    instance = allocate
    instance.send :initialize, options, params.map {|p|p.to_sym}, &block
    instance
  end
end

Instance Method Details

#call_with_conditions(obj, attr, value) ⇒ Object

:nodoc:



80
81
82
83
84
# File 'lib/not_naughty/validation.rb', line 80

def call_with_conditions(obj, attr, value) #:nodoc:
  if @conditions.all? { |c| c.evaluate obj }
    call_without_conditions obj, attr, value
  end
end

#call_without_conditions(obj, attr, value) ⇒ Object Also known as: call

:nodoc:



75
76
77
# File 'lib/not_naughty/validation.rb', line 75

def call_without_conditions(obj, attr, value) #:nodoc:
  @block.call obj, attr, value
end