Sha256: 832be40aeba9dcbceb20cbfe91fd320aa273e5078425bc96fd85ca226c13e5e1

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Mixture
  module Validate
    # A base for validations.  All validators should inherit this
    # class.
    #
    # @abstract
    class Base
      # Registers this validator as the given name.
      #
      # @see Validate.register
      # @param name [Symbol] The name of the validator.
      # @return [void]
      def self.register_as(name)
        Validate.register(name, self)
      end

      # Initialize the validator.
      #
      # @param options [Hash] The options for the validator.
      def initialize(options)
        @options = options
      end

      # Performs the validation.
      #
      # @param record [Mixture::Model] The model that has the
      #   attribute.  At least, it should respond to `#errors`.
      # @param attribute [Attribute] The attribute to validate.
      # @param value [Object] The value of the attribute.
      # @return [void]
      # @abstract
      def validate(record, attribute, value)
        @record = record
        @attribute = attribute
        @value = value
      end

    private

      # Raises an error with the given a message.
      #
      # @param message [String] The message to raise.
      # @raise [ValidationError]
      def error(message)
        fail ValidationError, message
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mixture-0.7.1 lib/mixture/validate/base.rb
mixture-0.7.0 lib/mixture/validate/base.rb