Class: CreditCardValidations::Expiration
- Inherits:
-
Object
- Object
- CreditCardValidations::Expiration
- Defined in:
- lib/credit_card_validations/expiration.rb
Constant Summary collapse
- FORMATS =
['%m/%Y', '%m/%y', '%m-%Y', '%m-%y', '%m%Y', '%m%y'].freeze
Instance Attribute Summary collapse
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #expired?(today = Date.today) ⇒ Boolean
-
#initialize(month, year) ⇒ Expiration
constructor
A new instance of Expiration.
- #last_day ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(month, year) ⇒ Expiration
Returns a new instance of Expiration.
20 21 22 23 |
# File 'lib/credit_card_validations/expiration.rb', line 20 def initialize(month, year) @month = month.to_i if month @year = normalize_year(year.to_i) if year end |
Instance Attribute Details
#month ⇒ Object (readonly)
Returns the value of attribute month.
18 19 20 |
# File 'lib/credit_card_validations/expiration.rb', line 18 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
18 19 20 |
# File 'lib/credit_card_validations/expiration.rb', line 18 def year @year end |
Class Method Details
.parse(raw) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/credit_card_validations/expiration.rb', line 25 def self.parse(raw) stripped = raw.to_s.gsub(/\s+/, '') return nil if stripped.empty? FORMATS.each do |fmt| date = begin Date.strptime(stripped, fmt) rescue Date::Error nil end return new(date.month, date.year) if date end nil end |
Instance Method Details
#expired?(today = Date.today) ⇒ Boolean
45 46 47 48 |
# File 'lib/credit_card_validations/expiration.rb', line 45 def expired?(today = Date.today) day = last_day day.nil? || today > day end |
#last_day ⇒ Object
50 51 52 53 54 55 |
# File 'lib/credit_card_validations/expiration.rb', line 50 def last_day return nil unless month && year && (1..12).cover?(month) Date.new(year, month, -1) rescue Date::Error nil end |
#valid? ⇒ Boolean
39 40 41 42 43 |
# File 'lib/credit_card_validations/expiration.rb', line 39 def valid? !expired? rescue Date::Error false end |