Sha256: d58a33afbd6f70119a0ef9672e91a5a4d385c66b3ea1acf4da3454b9ab14caaa

Contents?: true

Size: 831 Bytes

Versions: 2

Compression:

Stored size: 831 Bytes

Contents

# frozen_string_literal: true

require 'regexp-examples'

module DirtySeed
  module Assigners
    # Helps with regex validations
    module RegexHelper
      # Returns the regex pattern if value should respect a format
      #   For example when: `validates :email, format: { with: /\w{10}@(hotmail|gmail)\.com/ }`
      # @return [Regex]
      def regex
        regex_validator =
          validators&.find do |validator|
            validator.is_a? ActiveModel::Validations::FormatValidator
          end
        regex_validator&.options&.dig(:with)
      end

      # Returns a random value matching the pattern
      # @return [String]
      # @note Rescue from unreadable regex with nil
      def regex_value
        regex.random_example
      rescue RegexpExamples::IllegalSyntaxError
        nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dirty_seed-0.2.1 lib/dirty_seed/assigners/helpers/regex_helper.rb
dirty_seed-0.2.0 lib/dirty_seed/assigners/helpers/regex_helper.rb