Sha256: 535b717da2ba83c16c378ab8679aba88e87e1a695272c70cb12a71f28086c03f

Contents?: true

Size: 775 Bytes

Versions: 3

Compression:

Stored size: 775 Bytes

Contents

require 'active_support/core_ext/object/blank'

module ValidatesFormattingOf

  class MissingValidation < StandardError
    def initialize(method)
      super("The validation method #{method.to_sym.inspect} has not been defined.")
    end
  end

  module ValidationAddition
    attr_reader :validations

    def add(name, regex, message = nil)
      @validations ||= {}
      @validations[name.to_sym] = Validation.new(name.to_sym, regex, message)
    end

    def find(attribute, opts = {})
      method = opts.fetch(:using, attribute)
      raise MissingValidation.new(method) if missing? method
      @validations[method.to_sym]
    end

    def missing?(name)
      !exists?(name)
    end

    def exists?(name)
      @validations[name.to_sym].present?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
validates_formatting_of-0.8.0 lib/validates_formatting_of/validation_addition.rb
validates_formatting_of-0.7.2 lib/validates_formatting_of/validation_addition.rb
validates_formatting_of-0.7.1 lib/validates_formatting_of/validation_addition.rb