Module: Ohm::Validations
- Included in:
- Model::Validations
- Defined in:
- lib/ohm/validations.rb
Overview
Provides a base implementation for extensible validation routines. Validations currently only provides the following assertions:
assert
assert_present
assert_format
assert_numeric
The core tenets that Ohm::Validations advocates can be summed up in a few bullet points:
Validations are much simpler and better done using composition rather than macros.
Error messages should be kept separate and possibly in the view or presenter layer.
It should be easy to write your own validation routine.
Since Ohm’s philosophy is to keep the core code small, other validations are simply added on a per-model or per-project basis.
If you want other validations you may want to take a peek at Ohm::Contrib and all of the validation modules it provides.
Defined Under Namespace
Instance Method Summary (collapse)
-
- (Object) assert(value, error)
protected
The grand daddy of all assertions.
-
- (Object) assert_format(att, format, error = [att, :format])
protected
Allows you to do a validation check against a regular expression.
-
- (Object) assert_numeric(att, error = [att, :not_numeric])
protected
Checks if all the characters of an attribute is a digit.
-
- (Object) assert_present(att, error = [att, :not_present])
protected
The most basic and highly useful assertion.
-
- (Object) errors
All errors for this model.
-
- (Boolean) valid?
Check if the current model state is valid.
-
- (Object) validate
Base validate implementation.
Instance Method Details
- (Object) assert(value, error) (protected)
The grand daddy of all assertions. If you want to build custom assertions, or even quick and dirty ones, you can simply use this method.
208 209 210 |
# File 'lib/ohm/validations.rb', line 208 def assert(value, error) value or errors.push(error) && false end |
- (Object) assert_format(att, format, error = [att, :format]) (protected)
Allows you to do a validation check against a regular expression. It’s important to note that this internally calls #assert_present, therefore you need not structure your regular expression to check for a non-empty value.
159 160 161 162 163 |
# File 'lib/ohm/validations.rb', line 159 def assert_format(att, format, error = [att, :format]) if assert_present(att, error) assert(send(att).to_s.match(format), error) end end |
- (Object) assert_numeric(att, error = [att, :not_numeric]) (protected)
Checks if all the characters of an attribute is a digit. If you want to verify that a value is a decimal, try looking at Ohm::Contrib’s assert_decimal assertion.
183 184 185 186 187 |
# File 'lib/ohm/validations.rb', line 183 def assert_numeric(att, error = [att, :not_numeric]) if assert_present(att, error) assert_format(att, /^\d+$/, error) end end |
- (Object) assert_present(att, error = [att, :not_present]) (protected)
The most basic and highly useful assertion. Simply checks if the value of the attribute is empty.
171 172 173 |
# File 'lib/ohm/validations.rb', line 171 def assert_present(att, error = [att, :not_present]) assert(!send(att).to_s.empty?, error) end |
- (Object) errors
All errors for this model.
143 144 145 |
# File 'lib/ohm/validations.rb', line 143 def errors @errors ||= Errors.new(self) end |
- (Boolean) valid?
132 133 134 135 136 |
# File 'lib/ohm/validations.rb', line 132 def valid? errors.clear validate errors.empty? end |
- (Object) validate
Base validate implementation.
139 140 |
# File 'lib/ohm/validations.rb', line 139 def validate end |