Class: NotNaughty::Validator::State
- Defined in:
- lib/not_naughty/validator.rb
Overview
Container for attribute specific validations
See Validator for details.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
Returns validations for given attribute.
-
#add_validation(*params, &block) ⇒ Object
Adds the validation that results from
params
andblock
to validated attributes (see Validation#new for details). -
#has_validations? ⇒ Boolean
Returns true if a attributes has validations assigned, false otherwise.
-
#initialize(name = :default) ⇒ State
constructor
Initializes the state with given name.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
94 95 96 |
# File 'lib/not_naughty/validator.rb', line 94 def name @name end |
#validations ⇒ Object (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.
119 120 121 |
# File 'lib/not_naughty/validator.rb', line 119 def has_validations? @validations.any? { |attribute, validations| validations.any? } end |