Sha256: 8cf7fa32b3eacaa45bbb53d0533ac5f8ee9f269eec5b4cc6eb3b4b49c4720eba

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

# frozen_string_literal: true

module CodebrackerLogicGame
  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

1 entries across 1 versions & 1 rubygems

Version Path
codebracker_logic_game-0.1.0 lib/modules/validation.rb