Module: VirtualBox::AbstractModel::Validatable

Included in:
VirtualBox::AbstractModel
Defined in:
lib/virtualbox/abstract_model/validatable.rb

Overview

Provides validation methods for a class. Unlike ActiveRecord, validations are instance-level rather than class-level.

Instance Method Summary collapse

Instance Method Details

#__validates_extract_options(fields, defaults) ⇒ Object

Internal method. Should never be called.



162
163
164
# File 'lib/virtualbox/abstract_model/validatable.rb', line 162

def __validates_extract_options(fields, defaults)
  defaults.merge(fields.last.is_a?(Hash) ? fields.pop : {})
end

#add_error(field, error) ⇒ Object

Adds an error to a field. The error is a message.



22
23
24
25
# File 'lib/virtualbox/abstract_model/validatable.rb', line 22

def add_error(field, error)
  errors[field] ||= []
  errors[field].push(error)
end

#clear_errorsObject

Clears all the errors from a model.



28
29
30
# File 'lib/virtualbox/abstract_model/validatable.rb', line 28

def clear_errors
  @errors = {}
end

#errorsHash

Returns the errors on a model. The structure of this is a hash, keyed by the field name. The value of each member in the hash is an array of error messages.

Returns:

  • (Hash)


11
12
13
# File 'lib/virtualbox/abstract_model/validatable.rb', line 11

def errors
  @errors ||= {}
end

#errors_on(field) ⇒ Object

Returns the errors on a specific field. This returns nil if there are no errors, otherwise it returns an array of error messages.



17
18
19
# File 'lib/virtualbox/abstract_model/validatable.rb', line 17

def errors_on(field)
  @errors[field.to_sym]
end

#full_error_messagesObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/virtualbox/abstract_model/validatable.rb', line 32

def full_error_messages
  full_error_messages = Array.new
  errors.each do |field_name, messages|
    messages.each do |message|
      human_field_name = field_name.to_s.gsub('_', ' ').capitalize
      full_error_messages << "#{human_field_name} #{message}"
    end
  end
  full_error_messages
end

#valid?Boolean

This method calls the validate method on the model (which any subclass is expected to implement), and then checks that the validations didn’t add any errors.

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/virtualbox/abstract_model/validatable.rb', line 48

def valid?
  validate
  errors.empty?
end

#validateBoolean

Subclasses should override this method. Validation can be done any way an implementer feels. Helper methods such as #validates_presence_of, #validates_inclusion_of, etc. exist, but they’re use isn’t required. #add_error can be used to add an error to any field. By convention this method should return ‘true` or `false` to signal any errors.

Returns:

  • (Boolean)


60
61
62
# File 'lib/virtualbox/abstract_model/validatable.rb', line 60

def validate
  true
end

#validates_format_of(*fields) ⇒ Object

Validates the format of a field with a given regular expression.

validates_format_of :foo, :with => /\d+/


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/virtualbox/abstract_model/validatable.rb', line 94

def validates_format_of(*fields)
  options = __validates_extract_options(fields, {
    :with => nil,
    :message => "is not properly formatted."
  })

  fields.collect { |field|
    value = send(field)
    # Use validates_presence_of if you need it to be set
    next if value.nil? || value.to_s.empty?
    if options[:with] && value =~ options[:with]
      true
    else
      add_error(field, options[:message])
      false
    end
  }.compact.all? { |v| v == true }
end

#validates_inclusion_of(*fields) ⇒ Object

Validates that a field’s value is within a specific range, array, etc.

validates_inclusion_of :foo, :in => [1,2,3]
validates_inclusion_of :bar, :in => (1..6)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/virtualbox/abstract_model/validatable.rb', line 141

def validates_inclusion_of(*fields)
  options = __validates_extract_options(fields, {
    :in => nil,
    :message => "value %s is not included in the list."
  })

  fields.collect { |field|
    value = send(field)
    # Use validates_presence_of if you need it to be set
    return if value.nil? || value.to_s.empty?
    if options[:in] && options[:in].include?(value)
      true
    else
      message = options[:message] % value
      add_error(field, message)
      false
    end
  }.compact.all? { |v| v == true }
end

#validates_numericality_of(*fields) ⇒ Object

Validates the numericality of a specific field.

validates_numericality_of :field


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/virtualbox/abstract_model/validatable.rb', line 117

def validates_numericality_of(*fields)
  options = __validates_extract_options(fields, {
    :message => "is not a number."
  })

  fields.collect { |field|
    value = send(field)
    # Use validates_presence_of if you need it to be set
    next if value.nil? || value.to_s.empty?
    if value.is_a?(Numeric) && value.to_s =~ /^\d$/
      true
    else
      add_error(field, options[:message])
      false
    end
  }.compact.all? { |v| v == true }
end

#validates_presence_of(*fields) ⇒ Boolean

Validates the presence (non-emptiness) of a field or fields. This validation fails if the specified fields are either blank (“”) or nil.

Additionally, a custom error message can be specified:

validates_presence_of :foo, :bar
validates_presence_of :baz, :message => "must not be blank!"

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/virtualbox/abstract_model/validatable.rb', line 74

def validates_presence_of(*fields)
  options = __validates_extract_options(fields, {
    :message => "can't be blank."
  })

  fields.collect { |field|
    value = send(field)
    if value.nil? || value.to_s.empty?
      add_error(field, options[:message])
      false
    else
      true
    end
  }.compact.all? { |v| v == true }
end