Class: Luhn::LuhnValue
- Inherits:
-
Object
- Object
- Luhn::LuhnValue
- Defined in:
- lib/luhn.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Class Method Summary collapse
Instance Method Summary collapse
- #control_digit ⇒ Object
-
#initialize(number) ⇒ LuhnValue
constructor
A new instance of LuhnValue.
- #valid? ⇒ Boolean
Constructor Details
#initialize(number) ⇒ LuhnValue
Returns a new instance of LuhnValue.
20 21 22 |
# File 'lib/luhn.rb', line 20 def initialize number self.number = number end |
Instance Attribute Details
#number ⇒ Object
Returns the value of attribute number.
18 19 20 |
# File 'lib/luhn.rb', line 18 def number @number end |
Class Method Details
.generate(size, options = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/luhn.rb', line 24 def self.generate size, = {} generated = [:prefix] || '' (size - generated.size - 1).times { |i| generated += rand(10).to_s } generated + self.new(generated).control_digit.to_s end |
Instance Method Details
#control_digit ⇒ Object
35 36 37 38 |
# File 'lib/luhn.rb', line 35 def control_digit sum = checksum(:even) (sum % 10 != 0) ? 10 - (sum % 10) : 0 end |
#valid? ⇒ Boolean
31 32 33 |
# File 'lib/luhn.rb', line 31 def valid? number != "" && checksum(:odd) % 10 == 0 end |