Module: Codebreaker::Validation

Included in:
SecretCode, User
Defined in:
lib/modules/validation.rb

Instance Method Summary collapse

Instance Method Details

#validate(name, options = {}) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/modules/validation.rb', line 6

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