Module: Codebreaker::Validation
- Included in:
- SecretCode, User
- Defined in:
- lib/modules/validation.rb
Instance Method Summary collapse
-
#validate(name, options = {}) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize.
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, = {}) define_method(:validate!) do attribute = instance_variable_get("@#{name}".to_sym) raise I18n.t(:set_value) if [:presence] && (attribute.nil? || attribute == '') raise I18n.t(:incorrect_type) if [:type] && !attribute.instance_of?([:type]) raise I18n.t(:incorrect_length) if [:length] && ![:length].include?(attribute.to_s.length) raise I18n.t(:incorrect_value) if [:values] && !attribute.digits.select! do |digit| [:values].include?(digit) end.nil? end end |