Module: Organ::Validations
- Included in:
- Form
- Defined in:
- lib/organ/validations.rb
Constant Summary collapse
- EMAIL_FORMAT =
/\A ([0-9a-zA-Z\.][-\w\+\.]*)@ ([0-9a-zA-Z_][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}\z/x
Instance Method Summary collapse
-
#append_error(attribute_name, error) ⇒ Object
Append an error to the given attribute.
-
#errors ⇒ Hash<Symbol, Array<Symbol>] The errors Hash, having the Symbol attribute names as keys and an array of errors (Symbols) as the value.
Get the current form errors Hash.
-
#present?(value) ⇒ Boolean
private
Determine if the value is present (not nil, false or an empty string).
-
#valid? ⇒ Boolean
Determine if current form instance is valid by running the validations specified on #validate.
-
#validate_email_format(attribute_name) ⇒ Object
Validate the email format (if present).
-
#validate_format(attribute_name, format) ⇒ Object
Validate the format of the attribute value (if present).
-
#validate_inclusion(attribute_name, list) ⇒ Object
Validate the value of the given attribute is included in the list (if present).
-
#validate_length(attribute_name, options = {}) ⇒ Object
Validate the length of a String, Array or any other form attribute which responds to #size (if present).
-
#validate_presence(attribute_name) ⇒ Object
Validate the presence of the attribute value.
-
#validate_range(attribute_name, options = {}) ⇒ Object
Validate the range in which the attribute can be (if present).
-
#validate_uniqueness(attribute_name, &block) ⇒ Object
Validate the uniqueness of the attribute value (if present).
-
#validation_block(&block) ⇒ Object
Call the given block if there are no errors.
Instance Method Details
#append_error(attribute_name, error) ⇒ Object
Append an error to the given attribute.
38 39 40 |
# File 'lib/organ/validations.rb', line 38 def append_error(attribute_name, error) errors[attribute_name] << error end |
#errors ⇒ Hash<Symbol, Array<Symbol>] The errors Hash, having the Symbol attribute names as keys and an array of errors (Symbols) as the value.
Get the current form errors Hash.
15 16 17 |
# File 'lib/organ/validations.rb', line 15 def errors @errors ||= Hash.new { |hash, key| hash[key] = [] } end |
#present?(value) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine if the value is present (not nil, false or an empty string).
176 177 178 |
# File 'lib/organ/validations.rb', line 176 def present?(value) value && !value.to_s.empty? end |
#valid? ⇒ Boolean
Determine if current form instance is valid by running the validations specified on #validate.
25 26 27 28 29 |
# File 'lib/organ/validations.rb', line 25 def valid? errors.clear validate errors.empty? end |
#validate_email_format(attribute_name) ⇒ Object
Validate the email format (if present). If the value does not match the email format, append the :invalid error to the attribute.
87 88 89 |
# File 'lib/organ/validations.rb', line 87 def validate_email_format(attribute_name) validate_format(attribute_name, EMAIL_FORMAT) end |
#validate_format(attribute_name, format) ⇒ Object
Validate the format of the attribute value (if present). If the value does not match the regexp given, append :invalid error to the attribute.
98 99 100 101 102 103 |
# File 'lib/organ/validations.rb', line 98 def validate_format(attribute_name, format) value = send(attribute_name) if present?(value) && !(format =~ value) append_error(attribute_name, :invalid) end end |
#validate_inclusion(attribute_name, list) ⇒ Object
Validate the value of the given attribute is included in the list (if present). If the value is not included in the list, append the :not_included error to the attribute.
140 141 142 143 144 145 |
# File 'lib/organ/validations.rb', line 140 def validate_inclusion(attribute_name, list) value = send(attribute_name) if present?(value) && !list.include?(value) append_error(attribute_name, :not_included) end end |
#validate_length(attribute_name, options = {}) ⇒ Object
Validate the length of a String, Array or any other form attribute which responds to #size (if present). If the value is too short, append the :too_short error to the attribute. If the value is too long append the :too_long error to the attribute.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/organ/validations.rb', line 115 def validate_length(attribute_name, = {}) min = .fetch(:min, nil) max = .fetch(:max, nil) value = send(attribute_name) if present?(value) length = value.size if min && length < min append_error(attribute_name, :too_short) end if max && length > max append_error(attribute_name, :too_long) end end end |
#validate_presence(attribute_name) ⇒ Object
Validate the presence of the attribute value. If the value is nil or false append a :blank error to the attribute.
57 58 59 60 61 62 |
# File 'lib/organ/validations.rb', line 57 def validate_presence(attribute_name) value = send(attribute_name) unless present?(value) append_error(attribute_name, :blank) end end |
#validate_range(attribute_name, options = {}) ⇒ Object
Validate the range in which the attribute can be (if present). If the value is less than the min a :less_than_min error will be appended. If the value is greater than the max a :greater_than_max error will be appended.
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/organ/validations.rb', line 158 def validate_range(attribute_name, = {}) value = send(attribute_name) return unless present?(value) min = .fetch(:min, nil) max = .fetch(:max, nil) append_error(attribute_name, :less_than) if min && value < min append_error(attribute_name, :greater_than) if max && value > max end |
#validate_uniqueness(attribute_name, &block) ⇒ Object
Validate the uniqueness of the attribute value (if present). The uniqueness is determined by the block given. If the value is not unique, append the :taken error to the attribute.
74 75 76 77 78 79 |
# File 'lib/organ/validations.rb', line 74 def validate_uniqueness(attribute_name, &block) value = send(attribute_name) if present?(value) && !block.call(value) append_error(attribute_name, :taken) end end |
#validation_block(&block) ⇒ Object
Call the given block if there are no errors.
47 48 49 |
# File 'lib/organ/validations.rb', line 47 def validation_block(&block) block.call if errors.empty? end |