Sha256: 2a494bf1425a1ff28f1bb6b99d9d08d3f1eb98793182b726ec97592029747468

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# -*- encoding: utf-8 -*-

require 'aequitas/rule/format'

module Aequitas
  class Rule
    class Format
      class Regexp < Format

        equalize_on *superclass.equalizer.keys + [:format_name]

        attr_reader :format_name

        def initialize(attribute_name, options = {})
          super

          @format_name = options.fetch(:format_name, nil)
        end

        def valid?(resource)
          value = attribute_value(resource)
          return true if skip?(value)

          value ? value.to_s =~ self.format : false
        rescue ::Encoding::CompatibilityError
          # This is to work around a bug in jruby - see formats/email.rb
          false
        end

        # TODO: integrate format into error message key?
        # def error_message_args
        #   if format_name.is_a?(Symbol)
        #     [ :"invalid_#{format_name}", attribute_name ]
        #   else
        #     [ :invalid, attribute_name ]
        #   end
        # end

      end # class Regexp
    end # class Format
  end # class Rule
end # module Aequitas

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aequitas-0.0.2 lib/aequitas/rule/format/regexp.rb