Created
July 22, 2016 14:33
-
-
Save andycamp/29f4c36754d0326da244069e5467986c to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
| class ThreePaycheckCalculator | |
| attr_reader :first_payday, :pay_interval, :number_of_paydays, :pay_days | |
| def initialize(first_payday, pay_interval: 14, number_of_paydays: 100) | |
| @first_payday = first_payday | |
| @pay_interval = pay_interval | |
| @number_of_paydays = number_of_paydays | |
| calculate | |
| self | |
| end | |
| def calculate | |
| @pay_days ||= (0..100).map{ |i| first_payday + (i * pay_interval) } | |
| end | |
| def three_payday_months | |
| if @three_payday_months | |
| @three_payday_months | |
| else | |
| pay_months = pay_days.map{ |pd| [pd.year, pd.month] } | |
| @three_payday_months = pay_months.select{ |m| pay_months.count(m) > 2 }.map{ |pm| Date.new(*pm) }.uniq | |
| end | |
| end | |
| def report | |
| to_s | |
| end | |
| def to_s | |
| three_payday_months.map{ |pm| | |
| "#{pm.strftime("%b %Y")}\n\t #{pay_days.select{ |pd| pd.year == pm.year && pd.month == pm.month }.join("\n\t")}" | |
| }.join("\n") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment