Sha256: cbec4e1e592c90b2cbc457917613079da9ee47a3e62c8d5dbb88a4ccb7ccef30

Contents?: true

Size: 589 Bytes

Versions: 1

Compression:

Stored size: 589 Bytes

Contents

# frozen_string_literal: true

module SimpleValidate
  class ValidatesFormatOf < ValidatesBase
    attr_accessor :regex

    def initialize(attribute, options)
      @allow_nil = options[:allow_nil]
      @regex = options[:with]
      super(
        attribute,
        options[:message] || "is incorrect format",
        options[:if] || proc { true }
      )
    end

    def valid?(instance)
      val = instance.send(attribute)

      return true if val.nil? && @allow_nil == true
      raise ArgumentError if regex.nil? || !regex.is_a?(Regexp)

      !!(val =~ regex)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_validate-2.2.2 lib/simple_validate/validates_format_of.rb