Class: Luhnacy
- Inherits:
-
Object
- Object
- Luhnacy
- Defined in:
- lib/luhnacy.rb
Class Method Summary collapse
- .generate(string_size, options = {}) ⇒ Object
- .method_missing(type, *args) ⇒ Object
- .valid?(candidate) ⇒ Boolean
Class Method Details
.generate(string_size, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/luhnacy.rb', line 11 def self.generate(string_size, ={}) raise ArgumentError, "prefix must be numeric" if [:prefix] && !([:prefix] =~ /^\d*$/) output = [:prefix] || '' (string_size-output.size-1).times do |n| output += rand(10).to_s end output += '0' if [:invalid] case calc_modulus(output) when 0 output = output[0...-1] + (rand(8) + 1).to_s when [1..8] output = output[0...-1] + ((10 - calc_modulus(output))/2).to_s when 9 output = output[0...-1] + (rand(7) + 2).to_s end else unless calc_modulus(output) == 0 output = output[0...-1] + (10 - calc_modulus(output)).to_s end end output end |
.method_missing(type, *args) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/luhnacy.rb', line 38 def self.method_missing(type, *args) card_type, method_type = parse_method_name(type) raise NoMethodError unless @cards[card_type] && @cards[card_type][:prefixes] && @cards[card_type][:size] send(method_type, *(args.unshift(card_type))) end |
.valid?(candidate) ⇒ Boolean
7 8 9 |
# File 'lib/luhnacy.rb', line 7 def self.valid?(candidate) calc_modulus(candidate) == 0 end |