module ErrorProne::Rule

Mixed in to objects that can verify whether an {ErrorProne::Model} is valid or not.

@example

class FakeRule

  include ErrorProne::Rule
  validates_as :not_nil

  def verify!
    !value.nil?
  end

  def message
    :field_nil
  end
end

Attributes

field[R]
object[R]
value[R]

Public Class Methods

included(klazz) click to toggle source
# File lib/error_prone.rb, line 105
def self.included(klazz)
  klazz.extend(ClassMethods)
end
new(object, field, options = {}) click to toggle source

@param [Object] object Object to verify conforms to a rule @param [Symbol] field Field on the object that must conform to the rule @param [Hash] options often specific to the the Rule

# File lib/error_prone.rb, line 77
def initialize(object, field, options = {})
  @object = object
  @field = field
  @options = options
end

Public Instance Methods

message() click to toggle source

Message to add when validate! finds an error. Must override in implementing classes. @return [Symbol]

# File lib/error_prone.rb, line 95
def message
  raise "You must set a message for rules to work"
end
validate!() click to toggle source

Adds error to the object if verify! fails @return [Boolean]

# File lib/error_prone.rb, line 85
def validate!
  @value = object.send(field)
  return true if verify!
  object.add_error(field, message)
  false
end
verify!() click to toggle source

Verifies if the rule is followed. Must override in implementing classes. @return [Boolean]

# File lib/error_prone.rb, line 101
def verify!
  raise "You must provide a verify! method for rules to work. verify! should return a boolean"
end