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" desc "list", "List birthdays"
def list def list
display = birthdays.map do |name, birthday| bdays = birthdays.sort do |a, b|
{ a[:month] == b[:month] ? a[:day] <=> b[:day] : a[:month] <=> b[:month]
name: name,
month: month_of(start_date(birthday)),
day: day_of(start_date(birthday)),
}
end end
display.sort! { |a, b| a[:month] == b[:month] ? a[:day] <=> b[:day] : a[:month] <=> b[:month] } bdays.each { |d| puts "🎂 #{d[:day]}/#{d[:month]} - #{d[:name]}" }
display.each { |d| puts "🎂 #{d[:month]}/#{d[:day]} - #{d[:name]}" }
end end
private private
@ -46,7 +41,14 @@ class Birthdaze < Thor
card = card.parsed.to_s card = card.parsed.to_s
birthday = birthday_regex.match(card)[1] if birthday_regex.match?(card) birthday = birthday_regex.match(card)[1] if birthday_regex.match?(card)
name = name_regex.match(card)[1] if name_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 end
@birthdays @birthdays
end end
@ -55,42 +57,28 @@ class Birthdaze < Thor
return @calendar if defined? @calendar return @calendar if defined? @calendar
@calendar = Icalendar::Calendar.new @calendar = Icalendar::Calendar.new
birthdays.each do |name, birthday| birthdays.each do |birthday|
@calendar.event do |event| @calendar.event do |event|
puts name event.dtstart = Icalendar::Values::Date.new(start_date(birthday, Date.today.year))
event.dtstart = Icalendar::Values::Date.new(start_date(birthday)) event.dtend = Icalendar::Values::Date.new(end_date(birthday, Date.today.year))
event.dtend = Icalendar::Values::Date.new(end_date(birthday)) event.summary = summary(birthday)
event.summary = summary(name, birthday)
end end
end end
@calendar.publish @calendar.publish
end end
# Takes a birthday string, with or without a year, and returns a start date # Takes a birthday string, with or without a year, and returns a start date
def start_date(birthday) def start_date(birthday, year)
year = Date.today.year "#{year}#{birthday[:month]}#{birthday[:day]}"
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
end end
def end_date(birthday) def end_date(birthday, year)
year = Date.today.year date = Date.parse(start_date(birthday, year)).next_day
birthday = birthday.tr("-", "") date.strftime("%Y%m%d")
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
end end
def summary(name, birthday) def summary(birthday)
return "🎂 #{name}s birthday" return "🎂 #{birthday[:name]}s birthday"
end end
def set_reminders def set_reminders
@ -106,10 +94,18 @@ class Birthdaze < Thor
end end
def month_of(date) def month_of(date)
date.length < 8 ? date[0..1] : date[4..5] date.tr("-", "")[-4..-3]
end end
def day_of(date) 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
end end