Created
May 3, 2012 12:26
-
-
Save mxrguspxrt/2585313 to your computer and use it in GitHub Desktop.
Calculate Luhn (http://en.wikipedia.org/wiki/Luhn_algorithm)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LuhnNumber | |
attr_accessor :full_number | |
def initialize(full_number) | |
self.full_number = full_number | |
end | |
def full_number=(full_number) | |
@full_number = full_number.gsub(/[^0-9]/, '').to_s | |
end | |
def number_part | |
full_number[0, full_number.length-1] if full_number | |
end | |
def check_digit_part | |
full_number[-1, 1] if full_number | |
end | |
def valid? | |
return false unless full_number and full_number.length > 2 | |
parts = full_number.split(//).map(&:to_i) | |
double = parts.length % 2 == 0 | |
parts = parts.collect do |part| | |
number = double ? part*2 : part | |
double = !double | |
number | |
end | |
sum = parts.join("").split(//).map(&:to_i).inject(:+) | |
sum % 10 == 0 | |
end | |
def invalid? | |
!valid? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe LuhnNumber do | |
subject { described_class.new("4485071359608368") } | |
describe "#initialize" do | |
it "sets full_number" do | |
described_class.any_instance.should_receive(:full_number=).with('123') | |
described_class.new('123') | |
end | |
end | |
describe "#full_number" do | |
it "returns only number part of set value" do | |
subject.full_number = '123 abc-' | |
subject.full_number.should == '123' | |
end | |
end | |
describe "#number_part" do | |
it "returns number part of full_number" do | |
subject.number_part.should == "448507135960836" | |
end | |
end | |
describe "#check_digit_part" do | |
it "returns number part of full_number" do | |
subject.check_digit_part.should == "8" | |
end | |
end | |
describe "#valid?" do | |
it "returns if luhn algorytm % 10 is true" do | |
subject.should be_valid | |
end | |
end | |
describe "#valid?" do | |
context "valid luhn" do | |
it "returns true" do | |
described_class.new("5500005555555559").should be_valid | |
described_class.new("5555555555555557").should be_valid | |
described_class.new("5454545454545454").should be_valid | |
described_class.new("5555515555555551").should be_valid | |
described_class.new("5405222222222226").should be_valid | |
described_class.new("5478050000000007").should be_valid | |
end | |
end | |
context "invalid luhn" do | |
it "returns false" do | |
described_class.new("5500005555555558").should be_invalid | |
described_class.new("5555555555555556").should be_invalid | |
described_class.new("5454545454545453").should be_invalid | |
described_class.new("5555515555555550").should be_invalid | |
described_class.new("5405222222222225").should be_invalid | |
described_class.new("5478050000000006").should be_invalid | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment