Sha256: 5d02b56b66d6259512febaabe123d7288178b54e20494920a91419c4b0d65f13

Contents?: true

Size: 737 Bytes

Versions: 10

Compression:

Stored size: 737 Bytes

Contents

require 'hermod/validators/base'

module Hermod
  module Validators
    # Checks the value matches the given regular expression
    class RegularExpression < Base
      attr_reader :pattern

      # Sets up the pattern the value is expected to match
      def initialize(pattern)
        @pattern = pattern
      end

      private

      # Public: Checks the value matches the pattern. Blank values are ignored
      # because those are checked by the ValuePresence validator if necessary.
      #
      # Returns a boolean
      def test(value, attributes)
        value.blank? || value =~ pattern
      end

      def message(value, attributes)
        "#{value.inspect} does not match #{pattern.inspect}"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hermod-3.3.0 lib/hermod/validators/regular_expression.rb
hermod-3.2.0 lib/hermod/validators/regular_expression.rb
hermod-3.0.0 lib/hermod/validators/regular_expression.rb
hermod-2.7.0 lib/hermod/validators/regular_expression.rb
hermod-2.7.0.pre.rc.1 lib/hermod/validators/regular_expression.rb
hermod-2.6.2 lib/hermod/validators/regular_expression.rb
hermod-2.6.1 lib/hermod/validators/regular_expression.rb
hermod-2.5.3 lib/hermod/validators/regular_expression.rb
hermod-2.5.2 lib/hermod/validators/regular_expression.rb
hermod-2.5.1 lib/hermod/validators/regular_expression.rb