Sha256: d3c12bf25dff6e605495bb0a8da279a13f6da82ca5bc277f496ab712edf8de7e

Contents?: true

Size: 749 Bytes

Versions: 2

Compression:

Stored size: 749 Bytes

Contents

class AlphaValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.alpha'))
    end
  end

  private

  def valid_format?(value, options)
    case options[:case]
    when :lower
      options[:strict] ? (value =~ /^[a-z]+$/) : (value =~ /^[a-z ]+$/)
    when :upper
      options[:strict] ? (value =~ /^[A-Z]+$/) : (value =~ /^[A-Z ]+$/)
    else
      options[:strict] ? (value =~ /^[A-Za-z]+$/i) : (value =~ /^[A-Za-z ]+$/i)
    end
  end

  def valid_length?(value)
    value != ' '
  end

  def valid?(value, options)
    valid_format?(value, options) &&
    valid_length?(value)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yukonisuru-1.0.0 lib/yukonisuru/validators/alpha_validator.rb
yukonisuru-0.0.1 lib/yukonisuru/validators/alpha_validator.rb