Class: NotNaughty::Validation
- Inherits:
-
Object
- Object
- NotNaughty::Validation
- Extended by:
- Observable
- Defined in:
- lib/not_naughty/validation.rb
Overview
The superclass for Validations.
See new for more information.
Direct Known Subclasses
AcceptanceValidation, ConfirmationValidation, FormatValidation, LengthValidation, PresenceValidation, UniquenessValidation
Defined Under Namespace
Classes: Condition
Constant Summary collapse
- BASEDIR =
File.dirname __FILE__
- PATTERN =
Loader pattern
File.join BASEDIR, %w[validations ** %s_validation.rb]
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.inherited(descendant) ⇒ Object
:nodoc:.
-
.load(*validations) ⇒ Object
Loads validations.
-
.new(*params, &block) ⇒ Object
Builds validations.
Instance Method Summary collapse
-
#call_with_conditions(obj, attr, value) ⇒ Object
:nodoc:.
-
#call_without_conditions(obj, attr, value) ⇒ Object
(also: #call)
:nodoc:.
-
#initialize(opts, attributes, &block) ⇒ Validation
constructor
:nodoc:.
Constructor Details
#initialize(opts, attributes, &block) ⇒ Validation
:nodoc:
64 65 66 67 |
# File 'lib/not_naughty/validation.rb', line 64 def initialize(opts, attributes, &block) #:nodoc: build_conditions opts[:if], opts[:unless] @attributes, @block, @opts = attributes, block, opts end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
62 63 64 |
# File 'lib/not_naughty/validation.rb', line 62 def attributes @attributes end |
Class Method Details
.inherited(descendant) ⇒ Object
:nodoc:
20 21 22 23 24 25 |
# File 'lib/not_naughty/validation.rb', line 20 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.
13 14 15 16 17 |
# File 'lib/not_naughty/validation.rb', line 13 def self.load(*validations) validations.each do |validation| Dir.glob(PATTERN % validation).each { |path| require path } end 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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/not_naughty/validation.rb', line 50 def self.new(*params, &block) attributes = if params.first.is_a? Class and params.first < self klass = params.shift klass.new(*params, &block) else = params. instance = allocate instance.send! :initialize, , params.map {|p|p.to_sym}, &block instance end end |
Instance Method Details
#call_with_conditions(obj, attr, value) ⇒ Object
:nodoc:
74 75 76 77 78 |
# File 'lib/not_naughty/validation.rb', line 74 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:
69 70 71 |
# File 'lib/not_naughty/validation.rb', line 69 def call_without_conditions(obj, attr, value) #:nodoc: @block.call obj, attr, value end |