Refactor to store birthdays in hash

Much more flexible
This commit is contained in:
Trevor Vallender 2024-03-03 20:19:33 +00:00
parent db982373c7
commit 57024374ee
1 changed files with 32 additions and 36 deletions

View File

@ -12,15 +12,10 @@ class Birthdaze < Thor
desc "list", "List birthdays"
def list
display = birthdays.map do |name, birthday|
{
name: name,
month: month_of(start_date(birthday)),
day: day_of(start_date(birthday)),
}
bdays = birthdays.sort do |a, b|
a[:month] == b[:month] ? a[:day] <=> b[:day] : a[:month] <=> b[:month]
end
display.sort! { |a, b| a[:month] == b[:month] ? a[:day] <=> b[:day] : a[:month] <=> b[:month] }
display.each { |d| puts "🎂 #{d[:month]}/#{d[:day]} - #{d[:name]}" }
bdays.each { |d| puts "🎂 #{d[:day]}/#{d[:month]} - #{d[:name]}" }
end
private
@ -46,7 +41,14 @@ class Birthdaze < Thor
card = card.parsed.to_s
birthday = birthday_regex.match(card)[1] if birthday_regex.match?(card)
name = name_regex.match(card)[1] if name_regex.match?(card)
@birthdays << [ name, birthday ] if name && birthday
if name && birthday
@birthdays << {
name: name,
month: month_of(birthday),
day: day_of(birthday),
birth_year: birth_year_of(birthday)
}
end
end
@birthdays
end
@ -55,42 +57,28 @@ class Birthdaze < Thor
return @calendar if defined? @calendar
@calendar = Icalendar::Calendar.new
birthdays.each do |name, birthday|
birthdays.each do |birthday|
@calendar.event do |event|
puts name
event.dtstart = Icalendar::Values::Date.new(start_date(birthday))
event.dtend = Icalendar::Values::Date.new(end_date(birthday))
event.summary = summary(name, birthday)
event.dtstart = Icalendar::Values::Date.new(start_date(birthday, Date.today.year))
event.dtend = Icalendar::Values::Date.new(end_date(birthday, Date.today.year))
event.summary = summary(birthday)
end
end
@calendar.publish
end
# Takes a birthday string, with or without a year, and returns a start date
def start_date(birthday)
year = Date.today.year
birthday = birthday.tr("-", "")
birthday = birthday.gsub("1604", "") if birthday.start_with?("1604")
if birthday.length < 8 # No year specified
"#{year}#{birthday[0..3]}"
else
"#{year}#{birthday[4..7]}"
end
def start_date(birthday, year)
"#{year}#{birthday[:month]}#{birthday[:day]}"
end
def end_date(birthday)
year = Date.today.year
birthday = birthday.tr("-", "")
birthday = birthday.gsub("1604", "") if birthday.start_with?("1604")
if birthday.length < 8 # No year specified
"#{year}#{birthday[0..1]}#{birthday[2..3].to_i + 1}"
else
"#{year}#{birthday[4..5]}#{birthday[6..7].to_i + 1}"
end
def end_date(birthday, year)
date = Date.parse(start_date(birthday, year)).next_day
date.strftime("%Y%m%d")
end
def summary(name, birthday)
return "🎂 #{name}s birthday"
def summary(birthday)
return "🎂 #{birthday[:name]}s birthday"
end
def set_reminders
@ -106,10 +94,18 @@ class Birthdaze < Thor
end
def month_of(date)
date.length < 8 ? date[0..1] : date[4..5]
date.tr("-", "")[-4..-3]
end
def day_of(date)
date.length < 8 ? date[2..3] : date[6..7]
date.tr("-", "")[-2..-1]
end
def birth_year_of(date)
return nil if date.length < 8
birth_year = date[0..3]
return nil if birth_year == "1604" # This is set (for some reason) by DAVx⁵
birth_year
end
end