Class: NotNaughty::Validator::State

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

Overview

Container for attribute specific validations

See Validator for details.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :default) ⇒ State

Initializes the state with given name.



97
98
99
# File 'lib/not_naughty/validator.rb', line 97

def initialize(name = :default)
  @name, @validations = name, Hash.new {|h, k| h[k] = []}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



94
95
96
# File 'lib/not_naughty/validator.rb', line 94

def name
  @name
end

#validationsObject (readonly)

Returns the value of attribute validations.



94
95
96
# File 'lib/not_naughty/validator.rb', line 94

def validations
  @validations
end

Instance Method Details

#[](attribute) ⇒ Object

Returns validations for given attribute.



113
114
115
# File 'lib/not_naughty/validator.rb', line 113

def [](attribute)
  @validations[attribute]
end

#add_validation(*params, &block) ⇒ Object

Adds the validation that results from params and block to validated attributes (see Validation#new for details).



104
105
106
107
108
109
110
# File 'lib/not_naughty/validator.rb', line 104

def add_validation(*params, &block)
  validation = Validation.new(*params, &block)
  
  validation.attributes.each do |attribute|
    @validations[attribute] << validation if attribute.is_a? Symbol
  end
end

#has_validations?Boolean

Returns true if a attributes has validations assigned, false otherwise.

Returns:

  • (Boolean)


119
120
121
# File 'lib/not_naughty/validator.rb', line 119

def has_validations?
  @validations.any? { |attribute, validations| validations.any? }
end