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

Instance Method Details

#append_error(attribute_name, error) ⇒ Object

Append an error to the given attribute.

Parameters:

  • attribute_name (Symbol)
  • error (Symbol)

    The error identifier.



38
39
40
# File 'lib/organ/validations.rb', line 38

def append_error(attribute_name, error)
  errors[attribute_name] << error
end

#errorsHash<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.

Returns:

  • (Hash<Symbol, Array<Symbol>] The errors Hash, having the Symbol attribute names as keys and an array of errors (Symbols) as the value.)

    Hash<Symbol, Array<Symbol>] The errors Hash, having the Symbol attribute names as keys and an array of errors (Symbols) as the value.



15
16
17
# File 'lib/organ/validations.rb', line 15

def errors
  @errors ||= Hash.new { |hash, key| hash[key] = [] }
end

#valid?Boolean

Determine if current form instance is valid by running the validations specified on #validate.

Returns:

  • (Boolean)


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 the value does not match the email format, append the :invalid error to the attribute.

Parameters:

  • attribute_name (Symbol)


78
79
80
# File 'lib/organ/validations.rb', line 78

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 the value does not match the regexp given, append :invalid error to the attribute.

Parameters:

  • attribute_name (Symbol)
  • format (Regexp)


89
90
91
92
93
94
# File 'lib/organ/validations.rb', line 89

def validate_format(attribute_name, format)
  value = send(attribute_name)
  if 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 the value is not included in the list, append the :not_included error to the attribute.

Parameters:

  • attribute_name (Symbol)
  • attribute_name (Symbol)
  • list (Array)


131
132
133
134
135
136
# File 'lib/organ/validations.rb', line 131

def validate_inclusion(attribute_name, list)
  value = send(attribute_name)
  if 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 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.

Parameters:

  • attribute_name (Symbol)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :min (Integer, nil) — default: nil
  • :max (Integer, nil) — default: nil


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/organ/validations.rb', line 106

def validate_length(attribute_name, options = {})
  min = options.fetch(:min, nil)
  max = options.fetch(:max, nil)
  value = send(attribute_name)

  if 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.

Parameters:

  • attribute_name (Symbol)


48
49
50
51
52
53
# File 'lib/organ/validations.rb', line 48

def validate_presence(attribute_name)
  value = send(attribute_name)
  if !value || value.to_s.empty?
    append_error(attribute_name, :blank)
  end
end

#validate_range(attribute_name, options = {}) ⇒ Object

Validate the range in which the attribute can be. 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.

Parameters:

  • attribute_name (Symbol)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :min (Integer) — default: nil

    The minimum value the attribute can take, if nil, no validation is made.

  • :max (Integer) — default: nil

    The maximum value the attribute can take, if nil, no validation is made.



149
150
151
152
153
154
155
156
157
158
# File 'lib/organ/validations.rb', line 149

def validate_range(attribute_name, options = {})
  value = send(attribute_name)

  return unless value

  min = options.fetch(:min, nil)
  max = options.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. The uniqueness is determined by the block given. If the value is not unique, append the :taken error to the attribute.

Parameters:

  • attribute_name (Symbol)
  • block (Proc)

    A block to determine if a given value is unique or not. It receives the value and should return true if the value is unique.



65
66
67
68
69
70
# File 'lib/organ/validations.rb', line 65

def validate_uniqueness(attribute_name, &block)
  value = send(attribute_name)
  unless block.call(value)
    append_error(attribute_name, :taken)
  end
end