Module: Aequitas::Macros
- Included in:
- ClassMethods
- Defined in:
- lib/aequitas/macros.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#validates_absence_of(*attribute_names) ⇒ Object
Validates that the specified attribute is “blank” via the attribute’s #blank? method.
-
#validates_acceptance_of(*attribute_names) ⇒ Object
Validates that the attributes’s value is in the set of accepted values.
-
#validates_confirmation_of(*attribute_names) ⇒ Object
Validates that the given attribute is confirmed by another attribute.
-
#validates_format_of(*attribute_names) ⇒ Object
Validates that the attribute is in the specified format.
-
#validates_length_of(*attribute_names) ⇒ Object
Validates that the length of the attribute is equal to, less than, greater than or within a certain range (depending upon the options you specify).
-
#validates_numericalness_of(*attribute_names) ⇒ Object
Validate whether a field is numeric.
-
#validates_presence_of(*attribute_names) ⇒ Object
Validates that the specified attribute is present.
-
#validates_primitive_type_of(*attribute_names) ⇒ Object
Validates that the specified attribute is of the correct primitive type.
- #validates_value_of(*attribute_names) ⇒ Object
-
#validates_with_block(*attribute_names, &block) ⇒ Object
Validate using the given block.
-
#validates_with_method(*attribute_names) ⇒ Object
Validate using method called on validated object.
-
#validates_within(*attribute_names) ⇒ Object
Validates that the value of a field is within a range/set.
Class Method Details
.extract_options(arguments) ⇒ Object
19 20 21 |
# File 'lib/aequitas/macros.rb', line 19 def self.(arguments) arguments.last.kind_of?(Hash) ? arguments.pop : {} end |
Instance Method Details
#validates_absence_of(*attribute_names) ⇒ Object
dm-core’s support lib adds the #blank? method to many classes,
Validates that the specified attribute is “blank” via the attribute’s #blank? method.
49 50 51 52 |
# File 'lib/aequitas/macros.rb', line 49 def validates_absence_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Absence::Blank, attribute_names, ) end |
#validates_acceptance_of(*attribute_names) ⇒ Object
Validates that the attributes’s value is in the set of accepted values.
83 84 85 86 |
# File 'lib/aequitas/macros.rb', line 83 def validates_acceptance_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Acceptance, attribute_names, ) end |
#validates_confirmation_of(*attribute_names) ⇒ Object
Validates that the given attribute is confirmed by another attribute. A common use case scenario is when you require a user to confirm their password, for which you use both password and password_confirmation attributes.
125 126 127 128 |
# File 'lib/aequitas/macros.rb', line 125 def validates_confirmation_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Confirmation, attribute_names, ) end |
#validates_format_of(*attribute_names) ⇒ Object
Validates that the attribute is in the specified format. You may use the :as (or :with, it’s an alias) option to specify the pre-defined format that you want to validate against. You may also specify your own format via a Proc or Regexp passed to the the :as or :with options.
171 172 173 174 |
# File 'lib/aequitas/macros.rb', line 171 def validates_format_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Format, attribute_names, ) end |
#validates_length_of(*attribute_names) ⇒ Object
Validates that the length of the attribute is equal to, less than, greater than or within a certain range (depending upon the options you specify).
235 236 237 238 |
# File 'lib/aequitas/macros.rb', line 235 def validates_length_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Length, attribute_names, ) end |
#validates_numericalness_of(*attribute_names) ⇒ Object
Validate whether a field is numeric.
280 281 282 283 284 |
# File 'lib/aequitas/macros.rb', line 280 def validates_numericalness_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Value, attribute_names, ) validation_rules.add(Rule::Numericalness, attribute_names, ) end |
#validates_presence_of(*attribute_names) ⇒ Object
dm-core’s support lib adds the blank? method to many classes,
Validates that the specified attribute is present.
For most property types “being present” is the same as being “not blank” as determined by the attribute’s #blank? method. However, in the case of Boolean, “being present” means not nil; i.e. true or false.
316 317 318 319 |
# File 'lib/aequitas/macros.rb', line 316 def validates_presence_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Presence::NotBlank, attribute_names, ) end |
#validates_primitive_type_of(*attribute_names) ⇒ Object
Validates that the specified attribute is of the correct primitive type.
340 341 342 343 |
# File 'lib/aequitas/macros.rb', line 340 def validates_primitive_type_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::PrimitiveType, attribute_names, ) end |
#validates_value_of(*attribute_names) ⇒ Object
345 346 347 348 |
# File 'lib/aequitas/macros.rb', line 345 def validates_value_of(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Value, attribute_names, ) end |
#validates_with_block(*attribute_names, &block) ⇒ Object
Validate using the given block. The block given needs to return:
- result::<Boolean>, Error Message::<String>
420 421 422 423 424 425 426 427 |
# File 'lib/aequitas/macros.rb', line 420 def validates_with_block(*attribute_names, &block) unless block_given? raise ArgumentError, 'You need to pass a block to validates_with_block' end = Macros.(attribute_names) validation_rules.add(Rule::Block, attribute_names, , &block) end |
#validates_with_method(*attribute_names) ⇒ Object
Validate using method called on validated object. The method must to return either true, or a pair of [false, error message string], and is specified as a symbol passed with :method option.
This validator does support multiple attribute_names being specified at a time, but we encourage you to use it with one property/method at a time.
Real world experience shows that method validation is often useful when attribute needs to be virtual and not a property name.
466 467 468 469 |
# File 'lib/aequitas/macros.rb', line 466 def validates_with_method(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Method, attribute_names, ) end |
#validates_within(*attribute_names) ⇒ Object
Validates that the value of a field is within a range/set.
This validation is defined by passing a field along with a :set parameter. The :set can be a Range or any object which responds to the #include? method (an array, for example).
377 378 379 380 |
# File 'lib/aequitas/macros.rb', line 377 def validates_within(*attribute_names) = Macros.(attribute_names) validation_rules.add(Rule::Within, attribute_names, ) end |