Sha256: f8f7e9a430b5ae05e33549f1ff83b1fbd30f32b0537e42c069144031ebad6786

Contents?: true

Size: 747 Bytes

Versions: 6

Compression:

Stored size: 747 Bytes

Contents

module ValidatesFormattingOf

  class InvalidRegularExpression < StandardError
    def initialize(name)
      super("You must specify a Regexp, a proc, or a lambda for the #{name.inspect} validation.")
    end
  end

  class Validation
    attr_reader :name, :regex, :message

    def initialize(name, regexp, message = "is not correctly formatted")
      callable = regexp.respond_to? :call
      if !callable && regexp.class.to_s != "Regexp"
        raise InvalidRegularExpression.new(name)
      end
      @name, @regex, @message = name, regexp, message
    end

    def inspect
      "#<Validation name: #{name.inspect}, regex: #{regex.inspect}, message: #{message}>"
    end

    def to_s
      "<Validation::#{name.to_s}>"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
validates_formatting_of-0.9.0 lib/validates_formatting_of/validation.rb
validates_formatting_of-0.8.1 lib/validates_formatting_of/validation.rb
validates_formatting_of-0.8.0 lib/validates_formatting_of/validation.rb
validates_formatting_of-0.7.2 lib/validates_formatting_of/validation.rb
validates_formatting_of-0.7.1 lib/validates_formatting_of/validation.rb
validates_formatting_of-0.7.0 lib/validates_formatting_of/validation.rb