Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RRule::Rule#humanize #49

Merged
merged 10 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
AllCops:
TargetRubyVersion: 2.6
NewCops: disable
Exclude:
- 'gemfiles/**/*'
- Appraisals
Expand Down
1 change: 1 addition & 0 deletions lib/rrule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module RRule
autoload :Rule, 'rrule/rule'
autoload :Context, 'rrule/context'
autoload :Weekday, 'rrule/weekday'
autoload :Humanizer, 'rrule/humanizer'

autoload :Frequency, 'rrule/frequencies/frequency'
autoload :Daily, 'rrule/frequencies/daily'
Expand Down
6 changes: 4 additions & 2 deletions lib/rrule/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ def rebuild(year, month)

if options[:bynweekday] && !options[:bynweekday].empty? && (month != last_month || year != last_year)
possible_date_ranges = []
if options[:freq] == 'YEARLY'

case options[:freq]
when 'YEARLY'
if options[:bymonth]
options[:bymonth].each do |mon|
possible_date_ranges.push(elapsed_days_in_year_by_month[(mon - 1)..mon])
end
else
possible_date_ranges = [[0, year_length_in_days]]
end
elsif options[:freq] == 'MONTHLY'
when 'MONTHLY'
possible_date_ranges = [elapsed_days_in_year_by_month[(month - 1)..month]]
end

Expand Down
215 changes: 215 additions & 0 deletions lib/rrule/humanizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# frozen_string_literal: true

module RRule
# Based off https://github.com/jakubroztocil/rrule/blob/master/src/nlp/totext.ts
#
class Humanizer
attr_reader :rrule, :options

OPTION_ATTRIBUTE_RE = /_option/.freeze

DAY_NAMES = %w[
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
].freeze

def initialize(rrule, options)
@rrule = rrule
@options = options

# Define instance method for each of the options.
options.each { |name, value| define_singleton_method("#{name}_option") { value } }
end

def to_s
@buffer = 'every'

send freq_option.downcase

if until_option
raise 'Implement Until'
elsif count_option
add 'for'
add count_option
add plural?(count_option) ? 'times' : 'time'
end

@buffer
end

# Return nil if we're trying to access an option that isn't present.
def method_missing(method_name, *args)
if method_name.to_s.match?(OPTION_ATTRIBUTE_RE)
nil
else
super
end
end

def respond_to_missing?(method_name)
super || method_name.to_s.match?(OPTION_ATTRIBUTE_RE)
end

protected

def list(arr, formatter, final_delimiter = nil, delimiter: ',')
*rest, middle, tail = arr.map(&formatter)

if final_delimiter
[*rest, [middle, tail].compact.join(" #{final_delimiter} ")].join("#{delimiter} ")
else
[*rest, middle, tail].compact.join("#{delimiter} ")
end
end

def add(string)
@buffer += " #{string}"
end

def plural?(num)
num.to_i % 100 != 1
end

def daily
add interval_option if interval_option != 1

if byweekday_option && weekdays?
add plural?(interval_option) ? 'weekdays' : 'weekday'
else
add plural?(interval_option) ? 'days' : 'day'
end

if bymonth_option
add 'in'
_bymonth
end

if bymonthday_option
_bymonthday
elsif byweekday_option
_byweekday
elsif byhour_option
_byhour
end
end

def weekly
if interval_option != 1
add interval_option
add plural?(interval_option) ? 'weeks' : 'week'
end

if byweekday_option && weekdays?
if interval_option == 1
add plural?(interval_option) ? 'weekdays' : 'weekday'
else
add 'on'
add 'weekdays'
end
elsif byweekday_option && every_day?
add plural?(interval_option) ? 'days' : 'day'
else
add 'week' if interval_option == 1

if bymonth_option
add 'in'
_bymonth
end

if bymonthday_option
_bymonthday
elsif byweekday_option
_byweekday
end
end
end

def monthly
if bymonth_option
if interval_option != 1
add interval_option
add 'months'
add 'in' if plural?(interval_option)
end

_bymonth
else
add interval_option if interval_option != 1

add plural?(interval_option) ? 'months' : 'month'
end

if bymonthday_option
_bymonthday
elsif byweekday_option && weekdays?
add 'on'
add 'weekdays'
elsif byweekday_option || bynweekday_option
_byweekday
end
end

def weekdaytext(day)
[day.ordinal && nth(day.ordinal), DAY_NAMES[day.index]].compact.join(' ')
end

def all_weeks?
bynweekday_option.all? { |option| option.ordinal.nil? }
end

def every_day?
byweekday_option.sort_by(&:index).map { |day| WEEKDAYS[day.index]} == RRule::WEEKDAYS
end

def weekdays?
return false if byweekday_option.none?

byweekday_option.sort_by(&:index).map { |day| WEEKDAYS[day.index]} == RRule::WEEKDAYS - %w[SA SU]
end

def _bymonth
add list(this.options.bymonth, method(:monthtext), 'and')
end

def _byweekday
if byweekday_option.any?
add 'on'
add list(byweekday_option, method(:weekdaytext))
end

return unless bynweekday_option.any?

add 'and' if all_weeks?
add 'on the'
add list(bynweekday_option, method(:weekdaytext), 'and')
end

def _byhour
add 'at'
add list byhour_option, :to_s, 'and'
end

def nth(ordinal)
return 'last' if ordinal == -1

nth =
case npos = ordinal.abs
when 1, 21, 31
"#{npos}st"
when 2, 22
"#{npos}nd"
when 3, 23
"#{npos}rd"
else
"#{npos}th"
end

ordinal < 0 ? "#{nth} last" : nth
end
end
end
4 changes: 4 additions & 0 deletions lib/rrule/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def next
enumerator.next
end

def humanize
Humanizer.new(self, options).to_s
end

private

attr_reader :options, :max_year, :max_date, :frequency_type
Expand Down
2 changes: 1 addition & 1 deletion lib/rrule/weekday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(index, ordinal = nil)
def self.parse(weekday)
match = /([+-]?\d+)?([A-Z]{2})/.match(weekday)
index = RRule::WEEKDAYS.index(match[2])
ordinal = match[1] ? match[1].to_i : nil
ordinal = match[1]&.to_i
Copy link
Contributor Author

@craigmcnamara craigmcnamara Dec 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean to change this, but RuboCop was complaining about it. Since safe nav has been around since Ruby 2.3 it should be fairly safe to change. I didn't notice any official Ruby versions listed as supported anywhere.

new(index, ordinal)
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2598,4 +2598,52 @@

expect(rrule.to_s).to eql rrule_string
end

describe '#humanize' do
let(:dtstart) { Time.parse('Tue Sep 2 06:00:00 PDT 1997') }
let(:timezone) { 'America/New_York' }
let(:rrule) { RRule::Rule.new(rule, dtstart: dtstart, tzid: timezone) }

context 'every day' do
let(:rule) { 'RRULE:FREQ=DAILY;INTERVAL=1' }

it { expect(rrule.humanize).to eq 'every day' }
end

context 'every day at 1' do
let(:rule) { 'RRULE:FREQ=DAILY;INTERVAL=1;BYHOUR=1' }

it { expect(rrule.humanize).to eq 'every day at 1' }
end

context 'every day weekly' do
let(:rule) { 'RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR,SA,SU' }

it { expect(rrule.humanize).to eq 'every day' }
end

context 'every week on Tuesday, Thursday' do
let(:rule) { 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,TH' }

it { expect(rrule.humanize).to eq 'every week on Tuesday, Thursday' }
end

context 'every 2 weeks on Tuesday, Thursday' do
let(:rule) { 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,TH' }

it { expect(rrule.humanize).to eq 'every 2 weeks on Tuesday, Thursday' }
end

context 'every month on the last Friday for 7 times' do
let(:rule) { 'FREQ=MONTHLY;BYDAY=-1FR;COUNT=7' }

it { expect(rrule.humanize).to eq 'every month on the last Friday for 7 times' }
end

context 'every month on the first Monday and last Friday for 7 times' do
let(:rule) { 'FREQ=MONTHLY;BYDAY=1MO,-1FR;COUNT=7' }

it { expect(rrule.humanize).to eq 'every month on the 1st Monday and last Friday for 7 times' }
end
end
end