Module: Bodhi::Validations
- Included in:
- Context, Profile, ResourceBatch, Simulation::NormalDistribution, Simulation::NormalDistributionCurve, SimulationFrame, Simulator, Type, TypeIndex, User
- Defined in:
- lib/bodhi-slam/validations.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#errors ⇒ Object
Returns a
Bodhi::Errors
object that holds all information about attribute error messages. -
#invalid? ⇒ Boolean
Runs all validations and returns
false
if no errors are present otherwisetrue
. -
#valid? ⇒ Boolean
Runs all validations and returns
true
if no errors are present otherwisefalse
. -
#validate! ⇒ Object
Runs all class validations on object and adds any errors to the
Bodhi::Errors
object.
Class Method Details
.included(base) ⇒ Object
135 136 137 138 |
# File 'lib/bodhi-slam/validations.rb', line 135 def self.included(base) base.extend(ClassMethods) base.instance_variable_set(:@validators, Hash.new) end |
Instance Method Details
#errors ⇒ Object
Returns a Bodhi::Errors
object that holds all information about attribute error messages.
class User
include Bodhi::Validations
attr_accessor :name
validates :name, required: true
end
user = User.new
user.valid? # => false
user.errors # => #<Bodhi::Errors:0x007fbff403e808 @messages={name:["is required"]}>
79 80 81 |
# File 'lib/bodhi-slam/validations.rb', line 79 def errors @errors ||= Bodhi::Errors.new end |
#invalid? ⇒ Boolean
Runs all validations and returns false
if no errors are present otherwise true
.
131 132 133 |
# File 'lib/bodhi-slam/validations.rb', line 131 def invalid? !valid? end |
#valid? ⇒ Boolean
Runs all validations and returns true
if no errors are present otherwise false
.
class User
include Bodhi::Validations
attr_accessor :name
validates :name, required: true
end
user = User.new
user.valid? # => false
user.errors. # => ["name is required"]
user.name = "Bob"
user.valid? # => true
user.errors. # => []
125 126 127 128 |
# File 'lib/bodhi-slam/validations.rb', line 125 def valid? validate! !errors..any? end |
#validate! ⇒ Object
Runs all class validations on object and adds any errors to the Bodhi::Errors
object
class User
include Bodhi::Validations
attr_accessor :name
validates :name, required: true
end
user = User.new
user.validate! # => nil
user.errors. # => ["name is required"]
user.name = "Bob"
user.validate! # => nil
user.errors. # => []
99 100 101 102 103 104 105 106 107 |
# File 'lib/bodhi-slam/validations.rb', line 99 def validate! errors.clear self.class.validators.each_pair do |attribute, array| value = self.send(attribute) array.each do |validator| validator.validate(self, attribute, value) end end end |