Sha256: 5341d945578548537ba3e156be1773ba0ed5f4d49bbfde61f744f466f3bd6761

Contents?: true

Size: 958 Bytes

Versions: 4

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true

module Codebreaker
  module Validation
    # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
    def validate(name, options = {})
      define_method(:validate!) do
        attribute = instance_variable_get("@#{name}".to_sym)
        raise I18n.t(:set_value) if options[:presence] && (attribute.nil? || attribute == '')
        raise I18n.t(:incorrect_type) if options[:type] && !attribute.instance_of?(options[:type])
        raise I18n.t(:incorrect_length) if options[:length] && !options[:length].include?(attribute.to_s.length)
        raise I18n.t(:incorrect_value) if options [:values] && !attribute.digits.select! do |digit|
                                            options[:values].include?(digit)
                                          end.nil?
      end
    end
    # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
codebreaker_al-0.1.2 lib/modules/validation.rb
codebreaker_al-0.1.1 lib/modules/validation.rb
codebreaker_al_rg-0.1.0 lib/modules/validation.rb
codebreaker_al-0.1.0 lib/modules/validation.rb