Class: NotNaughty::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/not_naughty/validator.rb

Overview

Superclass for all Adapters.

See new and get_state for more information.

Direct Known Subclasses

Sequel::Plugins::NotNaughty

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*states) ⇒ Validator

By default it comes with the :default State unless other states are provided.

Example:

NotNaughty::Validator.new
  # has the :default state
NotNaughty::Validator.new :create, :update
  # has the :create and :update states

Adapters should overwrite this method.



20
21
22
23
24
25
26
27
# File 'lib/not_naughty/validator.rb', line 20

def initialize(*states)
  states << :default if states.empty?
  
  @states         = states.inject({}) {|m, s| m.update s => State.new(s)}
  @initial_state  = @states[states.first]
  
  @error_handler  = NotNaughty::ErrorHandler.new
end

Instance Attribute Details

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



8
9
10
# File 'lib/not_naughty/validator.rb', line 8

def error_handler
  @error_handler
end

#statesObject (readonly)

Returns the value of attribute states.



8
9
10
# File 'lib/not_naughty/validator.rb', line 8

def states
  @states
end

Instance Method Details

#add_validation(*p, &b) ⇒ Object

Adds a validation to all/specified states.

Example:

add_validation(:firstname, :lastname, :on => :default) {|o, a, v|}
  # adds validation to :default state
add_validation(:firstname, :lastname) {|o, a, v|}
  # adds validation to all states
add_validation(:first, :last, :on => [:create, :update]) {|o, a, v|}
  # adds validation to :create and :update states


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/not_naughty/validator.rb', line 61

def add_validation(*p, &b)
  options = (p.last.is_a? Hash) ? p.last : {}
  
  if states = options.delete(:on)
    @states.values_at(*states).each do |state|
      state.add_validation(*p, &b) unless state.nil?
    end
  else
    @states.each { |name, state| state.add_validation(*p, &b) }
  end
end

#cloneObject

:nodoc:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/not_naughty/validator.rb', line 29

def clone #:nodoc:
  states = [@initial_state.name] | @states.keys
  error_handler = @error_handler.clone
  
  clone = self.class.new(*states)
  @states.each do |n, s|
    s.validations.each { |a, v| clone.states[n].validations[a] = v.clone }
  end
  clone.instance_eval do
    @initial_state = @states[states.first]
    @error_handler = error_handler
  end
  
  clone
end

#get_state(obj = nil) ⇒ Object

Returns the state for the given object. By default it does return the initial state.

Adapters that provide multiple states should eventually overwrite this method.



50
# File 'lib/not_naughty/validator.rb', line 50

def get_state(obj = nil) @initial_state end

#has_validations?(obj = nil) ⇒ Boolean

Returns true if given object has validations in its current state. If no object was given it returns true if any state has validations. It otherwise returns false.

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/not_naughty/validator.rb', line 76

def has_validations?(obj = nil)
  if obj.nil? then @states.any? { |name, state| state.has_validations? }
  else get_state(obj).has_validations? end
end

#invoke(obj) ⇒ Object

Runs all validations on object for the object’s state.



82
83
84
85
86
87
# File 'lib/not_naughty/validator.rb', line 82

def invoke(obj)
  get_state(obj).validations.each do |attr, validations|
    val = obj.send! attr
    validations.each { |validation| validation.call obj, attr, val }
  end
end