Skip to content

Instantly share code, notes, and snippets.

@dkubb
Last active December 26, 2015 09:18
Show Gist options
  • Save dkubb/7128095 to your computer and use it in GitHub Desktop.
Save dkubb/7128095 to your computer and use it in GitHub Desktop.
Spree::CreditCard#expiry= fix
Spree::CreditCard.class_eval do
MONTH_PATTERN = /(?<month>0?[1-9]|1[012])/.freeze
YEAR_PATTERN = /(?<year>(?:20)?\d{2})/.freeze
EXPIRY_PATTERN = /\A#{MONTH_PATTERN}#{YEAR_PATTERN}\z/.freeze
# Set the expiry month and year
#
# @param [#to_s] expiry
#
# @return [undefined]
#
# @api private
def expiry=(expiry)
match = expiry.to_s.delete('^0-9').match(EXPIRY_PATTERN)
if match
date = Date.new(2000 + match[:year].to_i % 100, match[:month].to_i)
self[:month], self[:year] = date.month, date.year
else
self[:month] = self[:year] = nil
end
end
end
require 'spec_helper'
describe Spree::CreditCard do
subject { described_class.new(expiry: '01/13') }
describe '#expiry=' do
context 'when the expiry is valid' do
before do
subject.expiry = expiry
end
months = (1..12).to_a | ('01'..'12').to_a
years = (13..33).to_a | (2013..2033).to_a
# XXX: this is probably overkill
months.each do |month|
years.each do |year|
['/', ''].each do |separator|
expiry = "#{month} #{separator} #{year}".squeeze(' ')
context "expiry #{expiry.inspect}" do
let(:expiry) { expiry }
its(:month) { should be(month.to_i) }
its(:year) { should be(2000 + year.to_i % 100) }
end
end
end
end
{
'1m/14' => [1, 2014],
'01m/14' => [1, 2014],
'12m/14' => [12, 2014],
'1 // 14' => [1, 2014],
'01 // 14' => [1, 2014],
'12 // 14' => [12, 2014],
1_14 => [1, 2014],
12_014 => [1, 2014],
1_214 => [12, 2014],
122_014 => [12, 2014],
}.each do |expiry, (month, year)|
context "expiry #{expiry.inspect}" do
let(:expiry) { expiry }
its(:month) { should be(month) }
its(:year) { should be(year) }
end
end
end
context 'when the expiry is invalid' do
before do
subject.expiry = expiry
end
['1', '01', '12', 1, 12, '', nil].each do |expiry|
context "expiry #{expiry.inspect}" do
let(:expiry) { expiry }
its(:month) { should be_nil }
its(:year) { should be_nil }
end
end
end
context 'when the expiry does not respond to #to_s' do
it 'raises an exception' do
expect { subject.expiry = BasicObject.new }.to raise_error
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment