Created
March 17, 2014 08:00
-
-
Save dladowitz/9595511 to your computer and use it in GitHub Desktop.
Given a date, this class is used to find the next payday being the 1st or the 15th. If the payday falls on a weekend or holiday the payday is adjusted to the business day. Note that you'll need to install the gems 'holidays' and 'active_support'. Not sure if this last on if available outside of rails
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 'date' | |
require 'active_support/core_ext/integer/inflections' # Used for printing date as human text | |
require 'holidays' # Gives access to holiays | |
class Payday | |
def initialize(query_date) | |
@query_date = query_date | |
@next_payday = 'unknown' | |
end | |
# Gets next business day payday | |
def get_next_payday | |
self.set_next_standard_payday | |
self.holiday_adjustment | |
self.weekend_adjustment | |
"Payday will be " + @next_payday.strftime("%a %b #{@next_payday.day.ordinalize}") | |
end | |
# Gives us the next payday for of either the 1st or 15th of the month | |
def set_next_standard_payday | |
day, month, year = self.set_date_components | |
if day == 1 | |
@next_payday = @query_date | |
elsif day.between?(2, 15) | |
@next_payday = Date.new(year, month, 15) | |
elsif day.between?(16, 31) | |
month == 12 ? @next_payday = Date.new(year + 1, 1, 1) : @next_payday = Date.new(year, month + 1, 1) | |
end | |
end | |
# Sets the day, month and year from @query_date | |
def set_date_components | |
return @query_date.day, @query_date.month, @query_date.year | |
end | |
# Adjust payday if it falls on a holiday | |
def holiday_adjustment | |
#checks to see if @next_payday is a US holday | |
if @next_payday.holidays(:us).any? | |
@next_payday = @next_payday - 1 | |
end | |
end | |
# Adjust payday if it falls on a weekend | |
def weekend_adjustment | |
if @next_payday.saturday? | |
@next_payday = @next_payday - 1 | |
elsif @next_payday.sunday? | |
@next_payday = @next_payday - 2 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the output from running in IRB
irb(main):054:0* date = Date.new(2014, 04, 01) => #<Date: 2014-04-01 ((2456749j,0s,0n),+0s,2299161j)> irb(main):055:0> payday = Payday.new(date) => #<Payday:0x007fe0b5c88fa0 @query_date=#<Date: 2014-04-01 ((2456749j,0s,0n),+0s,2299161j)>, @next_payday="unknown"> irb(main):056:0> payday.get_next_payday => "Payday will be Tue Apr 1st" irb(main):057:0> irb(main):058:0* date = Date.new(2014, 06, 14) => #<Date: 2014-06-14 ((2456823j,0s,0n),+0s,2299161j)> irb(main):059:0> payday = Payday.new(date) => #<Payday:0x007fe0b5c6bf90 @query_date=#<Date: 2014-06-14 ((2456823j,0s,0n),+0s,2299161j)>, @next_payday="unknown"> irb(main):060:0> payday.get_next_payday => "Payday will be Fri Jun 13th" irb(main):061:0> irb(main):062:0* date = Date.new(2014, 12, 30) => #<Date: 2014-12-30 ((2457022j,0s,0n),+0s,2299161j)> irb(main):063:0> payday = Payday.new(date) => #<Payday:0x007fe0b5c50628 @query_date=#<Date: 2014-12-30 ((2457022j,0s,0n),+0s,2299161j)>, @next_payday="unknown"> irb(main):064:0> payday.get_next_payday => "Payday will be Wed Dec 31st" irb(main):065:0> irb(main):066:0* date = Date.new(2005, 12, 30) => #<Date: 2005-12-30 ((2453735j,0s,0n),+0s,2299161j)> irb(main):067:0> payday = Payday.new(date) => #<Payday:0x007fe0b5c30c38 @query_date=#<Date: 2005-12-30 ((2453735j,0s,0n),+0s,2299161j)>, @next_payday="unknown"> irb(main):068:0> payday.get_next_payday => "Payday will be Fri Dec 30th" irb(main):069:0>