Skip to content

Instantly share code, notes, and snippets.

@aokolish
Created March 6, 2013 01:50
Show Gist options
  • Save aokolish/5096086 to your computer and use it in GitHub Desktop.
Save aokolish/5096086 to your computer and use it in GitHub Desktop.
require 'active_support/core_ext'
def dates_with_no_changes(employment_dates)
dates = []
employment_dates.each do |date|
dates << date[:start_at].to_date
if date[:end_at].nil?
dates << Date.current.next_year
else
dates << date[:end_at].to_date
end
end
dates.sort!
ranges_from_dates(dates)
end
def ranges_from_dates(dates)
ranges = []
dates.each_with_index do |date, i|
next_date = dates[i+1]
break unless next_date
next if date == next_date
range = {
start: date,
end: next_date
}
ranges << range
end
ranges
end
dates = [
{ name: 'Chris',
company: 'Krux',
start_at: 3.years.ago,
end_at: nil
},
{ name: 'Bob',
company: 'Homerun',
start_at: 10.months.ago,
end_at: nil
},
{ name: 'Kevin',
company: 'self employed',
start_at: 1.year.ago,
end_at: 3.months.ago
},
{ name: 'James',
company: 'Homerun',
start_at: 1.year.ago,
end_at: 3.months.ago
}
]
# 3yrs -> 1 yr ago
# 1 yr -> 10 months
# 10 months -> 3 months
# 3 months -> unknown end date
p dates_with_no_changes(dates)
p dates_with_no_changes(dates).length == 4
@aokolish
Copy link
Author

aokolish commented Mar 7, 2013

sort! does not work with nil elements in the dates array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment