Skip to content

Commit

Permalink
activemodel validation , except and only options
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Jan 11, 2015
1 parent a7150ab commit 30750f3
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/active_model/credit_card_number_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ module Validations
class CreditCardNumberValidator < EachValidator

def validate_each(record, attribute, value)
brands = options.fetch(:brands, :any)
record.errors.add(attribute) unless credit_card_valid?(value, brands == :any ? [] : Array.wrap(brands))
record.errors.add(attribute) unless credit_card_valid?(value, extract_brands(options))
end

def credit_card_valid?(number, brands = [])
CreditCardValidations::Detector.new(number).valid?(*brands)
end

protected

def extract_brands(options)
if options.has_key?(:brands)
options[:brands] == :any ? [] : Array(options[:brands])
elsif options.has_key?(:only)
Array(options[:only])
elsif options.has_key?(:except)
Array(CreditCardValidations::Detector.brands.keys) - Array(options[:except])
else
[]
end

end

end
end
end
Expand Down

0 comments on commit 30750f3

Please sign in to comment.