diff --git a/.travis.yml b/.travis.yml index 60d161b9..e7c40249 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,6 @@ script: "bundle exec rspec spec" +before_install: + - gem install bundler notifications: email: - john.crepezzi@gmail.com @@ -10,3 +12,4 @@ rvm: - 1.9.3 - 2.0.0 - 2.1 + - 2.2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a7f6298..3e2b503b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.14.0 / 2016-02-23 + +* Added span option for occurrence methods + +## 0.13.1 / 2015-12-07 + +* Added i18n support! + ## 0.13.0 / 2015-05-26 * [FEATURE] from_ical! diff --git a/Gemfile b/Gemfile index fa75df15..cd8aa9e0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ source 'https://rubygems.org' -gemspec +gemspec \ No newline at end of file diff --git a/README.md b/README.md index 48d82fab..025f34ce 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,9 @@ Imagine you want: You would write: ```ruby +schedule = IceCube::Schedule.new schedule.add_recurrence_rule( - Rule.yearly.day_of_month(13).day(:friday).month_of_year(:october) + IceCube::Rule.yearly.day_of_month(13).day(:friday).month_of_year(:october) ) ``` @@ -42,11 +43,11 @@ With ice_cube, you can specify (in increasing order of precedence): * Recurrence Times - To specifically include in a schedule * Exception Times - To specifically exclude from a schedule -Example: Specifying a recurrence with an exception time: +Example: Specifying a recurrence with an exception time ```ruby schedule = IceCube::Schedule.new(now = Time.now) do |s| - s.add_recurrence_rule(Rule.daily.count(3)) + s.add_recurrence_rule(IceCube::Rule.daily.count(3)) s.add_exception_time(now + 1.day) end @@ -84,16 +85,19 @@ schedule.remaining_occurrences # for terminating schedules schedule.previous_occurrence(from_time) schedule.previous_occurrences(3, from_time) +# or include prior occurrences with a duration overlapping from_time +schedule.next_occurrences(3, from_time, :spans => true) +schedule.occurrences_between(from_time, to_time, :spans => true) # or give the schedule a duration and ask if occurring_at? schedule = IceCube::Schedule.new(now, :duration => 3600) -schedule.add_recurrence_rule Rule.daily +schedule.add_recurrence_rule IceCube::Rule.daily schedule.occurring_at?(now + 1800) # true schedule.occurring_between?(t1, t2) -# using end_time also sets the duration +# using end_time also sets the duration schedule = IceCube::Schedule.new(start = Time.now, :end_time => start + 3600) -schedule.add_recurrence_rule Rule.daily +schedule.add_recurrence_rule IceCube::Rule.daily schedule.occurring_at?(start + 3599) # true schedule.occurring_at?(start + 3600) # false @@ -170,36 +174,36 @@ There are many types of recurrence rules that can be added to a schedule: ```ruby # every day -schedule.add_recurrence_rule Rule.daily +schedule.add_recurrence_rule IceCube::Rule.daily # every third day -schedule.add_recurrence_rule Rule.daily(3) +schedule.add_recurrence_rule IceCube::Rule.daily(3) ``` ### Weekly ```ruby # every week -schedule.add_recurrence_rule Rule.weekly +schedule.add_recurrence_rule IceCube::Rule.weekly # every other week on monday and tuesday -schedule.add_recurrence_rule Rule.weekly(2).day(:monday, :tuesday) +schedule.add_recurrence_rule IceCube::Rule.weekly(2).day(:monday, :tuesday) # for programmatic convenience (same as above) -schedule.add_recurrence_rule Rule.weekly(2).day(1, 2) +schedule.add_recurrence_rule IceCube::Rule.weekly(2).day(1, 2) # specifying a weekly interval with a different first weekday (defaults to Sunday) -schedule.add_recurrence_rule Rule.weekly(1, :monday) +schedule.add_recurrence_rule IceCube::Rule.weekly(1, :monday) ``` ### Monthly (by day of month) ```ruby # every month on the first and last days of the month -schedule.add_recurrence_rule Rule.monthly.day_of_month(1, -1) +schedule.add_recurrence_rule IceCube::Rule.monthly.day_of_month(1, -1) # every other month on the 15th of the month -schedule.add_recurrence_rule Rule.monthly(2).day_of_month(15) +schedule.add_recurrence_rule IceCube::Rule.monthly(2).day_of_month(15) ``` Monthly rules will skip months that are too short for the specified day of @@ -209,69 +213,69 @@ month (e.g. no occurrences in February for `day_of_month(31)`). ```ruby # every month on the first and last tuesdays of the month -schedule.add_recurrence_rule Rule.monthly.day_of_week(:tuesday => [1, -1]) +schedule.add_recurrence_rule IceCube::Rule.monthly.day_of_week(:tuesday => [1, -1]) # every other month on the first monday and last tuesday -schedule.add_recurrence_rule Rule.monthly(2).day_of_week( +schedule.add_recurrence_rule IceCube::Rule.monthly(2).day_of_week( :monday => [1], :tuesday => [-1] ) # for programmatic convenience (same as above) -schedule.add_recurrence_rule Rule.monthly(2).day_of_week(1 => [1], 2 => [-1]) +schedule.add_recurrence_rule IceCube::Rule.monthly(2).day_of_week(1 => [1], 2 => [-1]) ``` ### Yearly (by day of year) ```ruby # every year on the 100th days from the beginning and end of the year -schedule.add_recurrence_rule Rule.yearly.day_of_year(100, -100) +schedule.add_recurrence_rule IceCube::Rule.yearly.day_of_year(100, -100) # every fourth year on new year's eve -schedule.add_recurrence_rule Rule.yearly(4).day_of_year(-1) +schedule.add_recurrence_rule IceCube::Rule.yearly(4).day_of_year(-1) ``` ### Yearly (by month of year) ```ruby # every year on the same day as start_time but in january and february -schedule.add_recurrence_rule Rule.yearly.month_of_year(:january, :februrary) +schedule.add_recurrence_rule IceCube::Rule.yearly.month_of_year(:january, :februrary) # every third year in march -schedule.add_recurrence_rule Rule.yearly(3).month_of_year(:march) +schedule.add_recurrence_rule IceCube::Rule.yearly(3).month_of_year(:march) # for programatic convenience (same as above) -schedule.add_recurrence_rule Rule.yearly(3).month_of_year(3) +schedule.add_recurrence_rule IceCube::Rule.yearly(3).month_of_year(3) ``` ### Hourly (by hour of day) ```ruby # every hour on the same minute and second as start date -schedule.add_recurrence_rule Rule.hourly +schedule.add_recurrence_rule IceCube::Rule.hourly # every other hour, on mondays -schedule.add_recurrence_rule Rule.hourly(2).day(:monday) +schedule.add_recurrence_rule IceCube::Rule.hourly(2).day(:monday) ``` ### Minutely (every N minutes) ```ruby # every 10 minutes -schedule.add_recurrence_rule Rule.minutely(10) +schedule.add_recurrence_rule IceCube::Rule.minutely(10) # every hour and a half, on the last tuesday of the month -schedule.add_recurrence_rule Rule.minutely(90).day_of_week(:tuesday => [-1]) +schedule.add_recurrence_rule IceCube::Rule.minutely(90).day_of_week(:tuesday => [-1]) ``` ### Secondly (every N seconds) ```ruby # every second -schedule.add_recurrence_rule Rule.secondly +schedule.add_recurrence_rule IceCube::Rule.secondly # every 15 seconds between 12:00 - 12:59 -schedule.add_recurrence_rule Rule.secondly(15).hour_of_day(12) +schedule.add_recurrence_rule IceCube::Rule.secondly(15).hour_of_day(12) ``` --- diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 00000000..b1ad9023 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,178 @@ +en: + ice_cube: + pieces_connector: ' / ' + not: 'not %{target}' + not_on: 'not on %{target}' + date: + formats: + default: '%B %-d, %Y' + month_names: + - + - January + - February + - March + - April + - May + - June + - July + - August + - September + - October + - November + - December + day_names: + - Sunday + - Monday + - Tuesday + - Wednesday + - Thursday + - Friday + - Saturday + times: + other: '%{count} times' + one: '%{count} time' + until: 'until %{date}' + days_of_week: '%{segments} %{day}' + days_of_month: + other: '%{segments} days of the month' + one: '%{segments} day of the month' + days_of_year: + other: '%{segments} days of the year' + one: '%{segments} day of the year' + at_hours_of_the_day: + other: on the %{segments} hours of the day + one: on the %{segments} hour of the day + on_minutes_of_hour: + other: on the %{segments} minutes of the hour + one: on the %{segments} minute of the hour + at_seconds_of_minute: + other: at the %{segments} seconds + one: at the %{segments} second + on_seconds_of_minute: + other: on the %{segments} seconds of the minute + one: on the %{segments} second of the minute + each_second: + one: Secondly + other: Every %{count} seconds + each_minute: + one: Minutely + other: Every %{count} minutes + each_hour: + one: Hourly + other: Every %{count} hours + each_day: + one: Daily + other: Every %{count} days + each_week: + one: Weekly + other: Every %{count} weeks + each_month: + one: Monthly + other: Every %{count} months + each_year: + one: Yearly + other: Every %{count} years + 'on': on the %{sentence} + in: 'in %{target}' + integer: + negative: '%{ordinal} to last' + literal_ordinals: + -1: last + -2: 2nd to last + ordinal: '%{number}%{ordinal}' + ordinals: + default: th + 1: st + 2: nd + 3: rd + 11: th + 12: th + 13: th + on_weekends: on Weekends + on_weekdays: on Weekdays + days_on: + - Sundays + - Mondays + - Tuesdays + - Wednesdays + - Thursdays + - Fridays + - Saturdays + on_days: on %{days} + array: + last_word_connector: ', and ' + two_words_connector: ' and ' + words_connector: ', ' + string: + format: + day: '%{rest} %{current}' + day_of_week: '%{rest} %{current}' + day_of_month: '%{rest} %{current}' + day_of_year: '%{rest} %{current}' + hour_of_day: '%{rest} %{current}' + minute_of_hour: '%{rest} %{current}' + until: '%{rest} %{current}' + count: '%{rest} %{current}' + default: '%{rest} %{current}' + + date: + abbr_day_names: + - Sun + - Mon + - Tue + - Wed + - Thu + - Fri + - Sat + abbr_month_names: + - + - Jan + - Feb + - Mar + - Apr + - May + - Jun + - Jul + - Aug + - Sep + - Oct + - Nov + - Dec + day_names: + - Sunday + - Monday + - Tuesday + - Wednesday + - Thursday + - Friday + - Saturday + formats: + default: "%Y-%m-%d" + long: "%B %d, %Y" + short: "%b %d" + month_names: + - + - January + - February + - March + - April + - May + - June + - July + - August + - September + - October + - November + - December + order: + - :year + - :month + - :day + + time: + am: am + formats: + default: "%a, %d %b %Y %H:%M:%S %z" + long: "%B %d, %Y %H:%M" + short: "%d %b %H:%M" + pm: pm diff --git a/config/locales/es.yml b/config/locales/es.yml new file mode 100644 index 00000000..aebd3f01 --- /dev/null +++ b/config/locales/es.yml @@ -0,0 +1,176 @@ +es: + ice_cube: + pieces_connector: ', ' + not: 'excepto %{target}' + not_on: 'excepto el %{target}' + date: + formats: + default: '%-d de %B de %Y' + month_names: + - + - Enero + - Febrero + - Marzo + - Abril + - Mayo + - Junio + - Julio + - Agosto + - Septiembre + - Octubre + - Noviembre + - Diciembre + day_names: + - Domingo + - Lunes + - Martes + - Miércoles + - Jueves + - Viernes + - Sábado + times: + other: '%{count} veces' + one: '%{count} vez' + until: 'hasta el %{date}' + days_of_week: '%{segments} %{day}' + days_of_month: + other: 'los días %{segments} del mes' + one: 'el día %{segments} del mes' + days_of_year: + one: 'el día %{segments}' + other: 'los días %{segments}' + at_hours_of_the_day: + one: 'en la hora %{segments}' + other: 'en las horas %{segments}' + on_minutes_of_hour: + one: 'en el minuto %{segments}' + other: 'en los minutos %{segments}' + at_seconds_of_minute: + one: 'en el segundo %{segments}' + other: 'en los segundos %{segments}' + on_seconds_of_minute: + one: 'en el segundo %{segments} del minuto' + other: 'en los segundos %{segments} del minuto' + on_days: '%{days}' + each_second: + one: Cada segundo + other: 'Cada %{count} segundos' + each_minute: + one: Cada minuto + other: 'Cada %{count} minutos' + each_hour: + one: Cada hora + other: 'Cada %{count} horas' + each_day: + one: Diariamente + other: 'Cada %{count} días' + each_week: + one: Semanalmente + other: 'Cada %{count} semanas' + each_month: + one: Mensualmente + other: 'Cada %{count} meses' + each_year: + one: Anualmente + other: 'Cada %{count} años' + 'on': 'en %{sentence}' + in: 'en %{target}' + integer: + negative: '%{ordinal} por la cola' + literal_ordinals: + -1: el último + -2: el penúltimo + ordinal: '%{number}%{ordinal}' + ordinals: + default: º + 1: º + 2: º + 3: º + 11: º + 12: º + 13: º + on_weekends: en fin de semana + on_weekdays: en días laborables + days_on: + - los domingos + - los lunes + - los martes + - los miércoles + - los jueves + - los viernes + - los sábados + on_days: '%{days}' + array: + last_word_connector: ' y ' + two_words_connector: ' y ' + words_connector: ', ' + string: + format: + day: '%{rest} %{current}' + day_of_week: '%{rest} %{current}' + day_of_month: '%{rest} %{current}' + day_of_year: '%{rest} %{current}' + hour_of_day: '%{rest} %{current}' + minute_of_hour: '%{rest} %{current}' + until: '%{rest} %{current}' + count: '%{rest} %{current}' + default: '%{rest} %{current}' + + date: + day_names: + - Domingo + - Lunes + - Martes + - Miércoles + - Jueves + - Viernes + - Sábado + abbr_day_names: + - Dom + - Lun + - Mar + - Mie + - Jue + - Vie + - Sab + # Don't forget the nil at the beginning; there's no such thing as a 0th month + month_names: + - + - Enero + - Febrero + - Marzo + - Abril + - Mayo + - Junio + - Julio + - Agosto + - Septiembre + - Octubre + - Noviembre + - Diciembre + abbr_month_names: + - + - Ene + - Feb + - Mar + - Abr + - May + - Jun + - Jul + - Ago + - Sep + - Oct + - Nov + - Dic + formats: + default: '%d/%m/%Y' + long: '%d de %B de %Y' + short: '%d de %B' + + time: + formats: + default: "%A, %d de %B de %Y %H:%M:%S %z" + short: "%d de %b %H:%M" + long: "%d de %B de %Y a las %H:%M" + am: "am" + pm: "pm" diff --git a/config/locales/ja.yml b/config/locales/ja.yml new file mode 100644 index 00000000..a113af1a --- /dev/null +++ b/config/locales/ja.yml @@ -0,0 +1,107 @@ +ja: + ice_cube: + pieces_connector: ' / ' + not: '%{target}以外' + not_on: '%{target}以外' + date: + formats: + default: '%Y年%m月%d日' + month_names: + - + - 1月 + - 2月 + - 3月 + - 4月 + - 5月 + - 6月 + - 7月 + - 8月 + - 9月 + - 10月 + - 11月 + - 12月 + day_names: + - 日曜 + - 月曜 + - 火曜 + - 水曜 + - 木曜 + - 金曜 + - 土曜 + times: + other: '%{count}回' + one: '%{count}回' + until: '%{date}まで' + days_of_week: '%{segments}%{day}' + days_of_month: + other: '%{segments}日' + one: '%{segments}日' + days_of_year: + other: '%{segments}日' + one: '%{segments}日' + at_hours_of_the_day: + other: '%{segments}時' + one: '%{segments}時' + on_minutes_of_hour: + other: '%{segments}分' + one: '%{segments}分' + on_seconds_of_minute: + other: '%{segments}秒' + one: '%{segments}秒' + each_second: + one: 毎秒 + other: '%{count}秒ごと' + each_minute: + one: 毎分 + other: '%{count}分ごと' + each_hour: + one: 毎時間 + other: '%{count}時間ごと' + each_day: + one: 毎日 + other: '%{count}日ごと' + each_week: + one: 毎週 + other: '%{count}週間ごと' + each_month: + one: 毎月 + other: '%{count}ヶ月ごと' + each_year: + one: 毎年 + other: '%{count}年ごと' + 'on': '%{sentence}' + in: '%{target}' + integer: + negative: '最終%{ordinal}' + literal_ordinals: + -1: 最終 + -2: 最後から2番目の + ordinal: '%{ordinal}%{number}' + ordinals: + default: '' + on_weekends: 週末 + on_weekdays: 平日 + days_on: + - 日曜 + - 月曜 + - 火曜 + - 水曜 + - 木曜 + - 金曜 + - 土曜 + on_days: '%{days}' + array: + last_word_connector: '、' + two_words_connector: '、' + words_connector: '、' + string: + format: + day: '%{rest}%{current}' + day_of_week: '%{rest}%{current}' + day_of_month: '%{rest}%{current}' + day_of_year: '%{rest}%{current}' + hour_of_day: '%{rest}%{current}' + minute_of_hour: '%{rest}%{current}' + until: '%{current}%{rest}' + count: '%{rest}%{current}' + default: '%{rest}%{current}' diff --git a/ice_cube.gemspec b/ice_cube.gemspec index 9854163d..195a6de6 100644 --- a/ice_cube.gemspec +++ b/ice_cube.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.version = IceCube::VERSION s.platform = Gem::Platform::RUBY - s.files = Dir['lib/**/*.rb'] + s.files = Dir['lib/**/*.rb', 'config/**/*.yml'] s.test_files = Dir.glob('spec/*.rb') s.require_paths = ['lib'] s.has_rdoc = true @@ -21,4 +21,5 @@ Gem::Specification.new do |s| s.add_development_dependency('rspec', '~> 2.12.0') s.add_development_dependency('activesupport', '>= 3.0.0') s.add_development_dependency('tzinfo') + s.add_development_dependency('i18n') end diff --git a/lib/ice_cube.rb b/lib/ice_cube.rb index 4375ee53..cd1d4015 100644 --- a/lib/ice_cube.rb +++ b/lib/ice_cube.rb @@ -1,5 +1,8 @@ require 'date' require 'ice_cube/deprecated' +require 'ice_cube/i18n' + +IceCube::I18n.detect_backend! module IceCube @@ -69,7 +72,7 @@ module Validations # Defines the format used by IceCube when printing out Schedule#to_s. # Defaults to '%B %e, %Y' def self.to_s_time_format - @to_s_time_format ||= '%B %e, %Y' + IceCube::I18n.t("ice_cube.date.formats.default") end # Sets the format used by IceCube when printing out Schedule#to_s. @@ -80,7 +83,7 @@ def self.to_s_time_format=(format) # Retain backwards compatibility for schedules exported from older versions # This represents the version number, 11 = 0.11, 1.0 will be 100 def self.compatibility - @compatibility ||= 11 + @compatibility ||= IceCube::VERSION.scan(/\d+/)[0..1].join.to_i end def self.compatibility=(version) diff --git a/lib/ice_cube/builders/ical_builder.rb b/lib/ice_cube/builders/ical_builder.rb index d6592a6a..2ba4e646 100644 --- a/lib/ice_cube/builders/ical_builder.rb +++ b/lib/ice_cube/builders/ical_builder.rb @@ -32,15 +32,15 @@ def to_s def self.ical_utc_format(time) time = time.dup.utc - "#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time + IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ') # utc time end def self.ical_format(time, force_utc) time = time.dup.utc if force_utc if time.utc? - ":#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time + ":#{IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ')}" # utc time else - ";TZID=#{time.strftime('%Z:%Y%m%dT%H%M%S')}" # local time specified + ";TZID=#{IceCube::I18n.l(time, format: '%Z:%Y%m%dT%H%M%S')}" # local time specified end end diff --git a/lib/ice_cube/builders/string_builder.rb b/lib/ice_cube/builders/string_builder.rb index de4585d7..dda38dcc 100644 --- a/lib/ice_cube/builders/string_builder.rb +++ b/lib/ice_cube/builders/string_builder.rb @@ -13,14 +13,18 @@ def piece(type, prefix = nil, suffix = nil) end def to_s - @types.each_with_object(@base || '') do |(type, segments), str| + string = @base || '' + @types.each do |type, segments| if f = self.class.formatter(type) - str << ' ' << f.call(segments) + current = f.call(segments) else next if segments.empty? - str << ' ' << self.class.sentence(segments) + current = self.class.sentence(segments) end + f = IceCube::I18n.t('ice_cube.string.format')[type] ? type : 'default' + string = IceCube::I18n.t("ice_cube.string.format.#{f}", rest: string, current: current) end + string end def self.formatter(type) @@ -34,27 +38,33 @@ def self.register_formatter(type, &formatter) module Helpers - NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'] - SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' } - # influenced by ActiveSupport's to_sentence def sentence(array) case array.length when 0 ; '' when 1 ; array[0].to_s - when 2 ; "#{array[0]} and #{array[1]}" - else ; "#{array[0...-1].join(', ')}, and #{array[-1]}" + when 2 ; "#{array[0]}#{IceCube::I18n.t('ice_cube.array.two_words_connector')}#{array[1]}" + else ; "#{array[0...-1].join(IceCube::I18n.t('ice_cube.array.words_connector'))}#{IceCube::I18n.t('ice_cube.array.last_word_connector')}#{array[-1]}" end end def nice_number(number) - return 'last' if number == -1 - suffix = SPECIAL_SUFFIX[number] || NUMBER_SUFFIX[number.abs % 10] - if number < -1 - number.abs.to_s << suffix << ' to last' - else - number.to_s << suffix - end + literal_ordinal(number) || ordinalize(number) + end + + def ordinalize(number) + IceCube::I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number)) + end + + def literal_ordinal(number) + IceCube::I18n.t("ice_cube.integer.literal_ordinals")[number] + end + + def ordinal(number) + ord = IceCube::I18n.t("ice_cube.integer.ordinals")[number] || + IceCube::I18n.t("ice_cube.integer.ordinals")[number % 10] || + IceCube::I18n.t('ice_cube.integer.ordinals')[:default] + number >= 0 ? ord : IceCube::I18n.t("ice_cube.integer.negative", ordinal: ord) end end diff --git a/lib/ice_cube/i18n.rb b/lib/ice_cube/i18n.rb new file mode 100644 index 00000000..a9c090d9 --- /dev/null +++ b/lib/ice_cube/i18n.rb @@ -0,0 +1,24 @@ +module IceCube + module I18n + def self.t(*args) + backend.t(*args) + end + + def self.l(*args) + backend.l(*args) + end + + def self.backend + @backend + end + + def self.detect_backend! + require 'i18n' + ::I18n.load_path += Dir[File.expand_path('../../../config/locales/*{rb,yml}', __FILE__)] + @backend = ::I18n + rescue LoadError + require 'ice_cube/null_i18n' + @backend = NullI18n + end + end +end diff --git a/lib/ice_cube/null_i18n.rb b/lib/ice_cube/null_i18n.rb new file mode 100644 index 00000000..1dd37fcf --- /dev/null +++ b/lib/ice_cube/null_i18n.rb @@ -0,0 +1,28 @@ +require 'yaml' + +module IceCube + module NullI18n + def self.t(key, options = {}) + base = key.to_s.split('.').reduce(config) { |hash, current_key| hash[current_key] } + + base = base[options[:count] == 1 ? "one" : "other"] if options[:count] + + if base.is_a?(Hash) + return base.each_with_object({}) do |(key, value), hash| + hash[key.is_a?(String) ? key.to_sym : key] = value + end + end + + options.reduce(base) { |result, (find, replace)| result.gsub("%{#{find}}", "#{replace}") } + end + + def self.l(date_or_time, options = {}) + return date_or_time.strftime(options[:format]) if options[:format] + date_or_time.strftime(t('ice_cube.date.formats.default')) + end + + def self.config + @config ||= YAML.load(File.read(File.join(File.dirname(__FILE__), '..', '..', 'config', 'locales', 'en.yml')))['en'] + end + end +end diff --git a/lib/ice_cube/parsers/ical_parser.rb b/lib/ice_cube/parsers/ical_parser.rb index 6f795131..437efac9 100644 --- a/lib/ice_cube/parsers/ical_parser.rb +++ b/lib/ice_cube/parsers/ical_parser.rb @@ -18,7 +18,8 @@ def self.schedule_from_ical(ical_string, options = {}) when 'DURATION' data[:duration] # FIXME when 'RRULE' - data[:rrules] = [rule_from_ical(value)] + data[:rrules] ||= [] + data[:rrules] += [rule_from_ical(value)] end end Schedule.from_hash data diff --git a/lib/ice_cube/schedule.rb b/lib/ice_cube/schedule.rb index 73303ff3..85036e4a 100644 --- a/lib/ice_cube/schedule.rb +++ b/lib/ice_cube/schedule.rb @@ -166,15 +166,15 @@ def each_occurrence(&block) end # The next n occurrences after now - def next_occurrences(num, from = nil) + def next_occurrences(num, from = nil, options = {}) from = TimeUtil.match_zone(from, start_time) || TimeUtil.now(start_time) - enumerate_occurrences(from + 1, nil).take(num) + enumerate_occurrences(from + 1, nil, options).take(num) end # The next occurrence after now (overridable) - def next_occurrence(from = nil) + def next_occurrence(from = nil, options = {}) from = TimeUtil.match_zone(from, start_time) || TimeUtil.now(start_time) - enumerate_occurrences(from + 1, nil).next + enumerate_occurrences(from + 1, nil, options).next rescue StopIteration nil end @@ -195,26 +195,26 @@ def previous_occurrences(num, from) end # The remaining occurrences (same requirements as all_occurrences) - def remaining_occurrences(from = nil) + def remaining_occurrences(from = nil, options = {}) require_terminating_rules from ||= TimeUtil.now(@start_time) - enumerate_occurrences(from).to_a + enumerate_occurrences(from, nil, options).to_a end # Returns an enumerator for all remaining occurrences - def remaining_occurrences_enumerator(from = nil) + def remaining_occurrences_enumerator(from = nil, options = {}) from ||= TimeUtil.now(@start_time) - enumerate_occurrences(from) + enumerate_occurrences(from, nil, options) end # Occurrences between two times - def occurrences_between(begin_time, closing_time) - enumerate_occurrences(begin_time, closing_time).to_a + def occurrences_between(begin_time, closing_time, options = {}) + enumerate_occurrences(begin_time, closing_time, options).to_a end # Return a boolean indicating if an occurrence falls between two times - def occurs_between?(begin_time, closing_time) - enumerate_occurrences(begin_time, closing_time).next + def occurs_between?(begin_time, closing_time, options = {}) + enumerate_occurrences(begin_time, closing_time, options).next true rescue StopIteration false @@ -226,9 +226,7 @@ def occurs_between?(begin_time, closing_time) # occurrences at the end of the range since none of their duration # intersects the range. def occurring_between?(opening_time, closing_time) - opening_time = opening_time - duration - closing_time = closing_time - 1 if duration > 0 - occurs_between?(opening_time, closing_time) + occurs_between?(opening_time, closing_time, :spans => true) end # Return a boolean indicating if an occurrence falls on a certain date @@ -313,11 +311,14 @@ def last(n = nil) def to_s pieces = [] rd = recurrence_times_with_start_time - extimes - pieces.concat rd.sort.map { |t| t.strftime(IceCube.to_s_time_format) } + pieces.concat rd.sort.map { |t| IceCube::I18n.l(t, format: IceCube.to_s_time_format) } pieces.concat rrules.map { |t| t.to_s } - pieces.concat exrules.map { |t| "not #{t.to_s}" } - pieces.concat extimes.sort.map { |t| "not on #{t.strftime(IceCube.to_s_time_format)}" } - pieces.join(' / ') + pieces.concat exrules.map { |t| IceCube::I18n.t('ice_cube.not', target: t.to_s) } + pieces.concat extimes.sort.map { |t| + target = IceCube::I18n.l(t, format: IceCube.to_s_time_format) + IceCube::I18n.t('ice_cube.not_on', target: target) + } + pieces.join(IceCube::I18n.t('ice_cube.pieces_connector')) end # Serialize this schedule to_ical @@ -404,25 +405,30 @@ def reset # Find all of the occurrences for the schedule between opening_time # and closing_time # Iteration is unrolled in pairs to skip duplicate times in end of DST - def enumerate_occurrences(opening_time, closing_time = nil, &block) + def enumerate_occurrences(opening_time, closing_time = nil, options = {}, &block) opening_time = TimeUtil.match_zone(opening_time, start_time) closing_time = TimeUtil.match_zone(closing_time, start_time) opening_time += start_time.subsec - opening_time.subsec rescue 0 opening_time = start_time if opening_time < start_time + spans = options[:spans] == true && duration != 0 Enumerator.new do |yielder| reset - t1 = full_required? ? start_time : realign(opening_time) + t1 = full_required? ? start_time : realign((spans ? opening_time - duration : opening_time)) loop do break unless (t0 = next_time(t1, closing_time)) break if closing_time && t0 > closing_time - yielder << (block_given? ? block.call(t0) : t0) if t0 >= opening_time + if (spans ? (t0.end_time > opening_time) : (t0 >= opening_time)) + yielder << (block_given? ? block.call(t0) : t0) + end break unless (t1 = next_time(t0 + 1, closing_time)) break if closing_time && t1 > closing_time if TimeUtil.same_clock?(t0, t1) && recurrence_rules.any?(&:dst_adjust?) wind_back_dst next (t1 += 1) end - yielder << (block_given? ? block.call(t1) : t1) if t1 >= opening_time + if (spans ? (t1.end_time > opening_time) : (t1 >= opening_time)) + yielder << (block_given? ? block.call(t1) : t1) + end next (t1 += 1) end end diff --git a/lib/ice_cube/time_util.rb b/lib/ice_cube/time_util.rb index 618c1b3d..bbb5bbb1 100644 --- a/lib/ice_cube/time_util.rb +++ b/lib/ice_cube/time_util.rb @@ -126,7 +126,7 @@ def self.end_of_date(date, reference=Time.now) # Convert a symbol to a numeric month def self.sym_to_month(sym) MONTHS.fetch(sym) do |k| - return wday = sym.to_i if MONTHS.values.any? { |i| i.to_s == sym.to_s } + MONTHS.values.detect { |i| i.to_s == k.to_s } or raise ArgumentError, "Expecting Fixnum or Symbol value for month. " \ "No such month: #{k.inspect}" end @@ -136,7 +136,7 @@ def self.sym_to_month(sym) # Convert a symbol to a wday number def self.sym_to_wday(sym) DAYS.fetch(sym) do |k| - return sym.to_i if DAYS.values.any? { |i| i.to_s == sym.to_s } + DAYS.values.detect { |i| i.to_s == k.to_s } or raise ArgumentError, "Expecting Fixnum or Symbol value for weekday. " \ "No such weekday: #{k.inspect}" end diff --git a/lib/ice_cube/validated_rule.rb b/lib/ice_cube/validated_rule.rb index c3ff916b..6dfae137 100644 --- a/lib/ice_cube/validated_rule.rb +++ b/lib/ice_cube/validated_rule.rb @@ -140,21 +140,19 @@ def find_acceptable_time_before(boundary) true end + # Returns true if all validations for the current rule match + # otherwise false and shifts to the first (largest) unmatched offset + # def validation_accepts_or_updates_time?(validations_for_type) - res = validated_results(validations_for_type) - return true if res.any? { |r| r.nil? || r == 0 } - return nil if res.all? { |r| r == true } - res.reject! { |r| r == true } + res = validations_for_type.each_with_object([]) do |validation, offsets| + r = validation.validate(@time, @schedule) + return true if r.nil? || r == 0 + offsets << r + end shift_time_by_validation(res, validations_for_type.first) false end - def validated_results(validations_for_type) - validations_for_type.map do |validation| - validation.validate(@time, @schedule) - end - end - def shift_time_by_validation(res, validation) return unless (interval = res.min) wrapper = TimeUtil::TimeWrapper.new(@time, validation.dst_adjust?) diff --git a/lib/ice_cube/validations/count.rb b/lib/ice_cube/validations/count.rb index 61099433..be22fd9e 100644 --- a/lib/ice_cube/validations/count.rb +++ b/lib/ice_cube/validations/count.rb @@ -51,7 +51,7 @@ def build_ical(builder) StringBuilder.register_formatter(:count) do |segments| count = segments.first - "#{count} #{count == 1 ? 'time' : 'times'}" + IceCube::I18n.t('ice_cube.times', count: count) end end diff --git a/lib/ice_cube/validations/daily_interval.rb b/lib/ice_cube/validations/daily_interval.rb index edf8ce72..635e77f1 100644 --- a/lib/ice_cube/validations/daily_interval.rb +++ b/lib/ice_cube/validations/daily_interval.rb @@ -35,7 +35,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Daily' : "Every #{interval} days" + builder.base = IceCube::I18n.t('ice_cube.each_day', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/day.rb b/lib/ice_cube/validations/day.rb index c29396f0..4c6bc56e 100644 --- a/lib/ice_cube/validations/day.rb +++ b/lib/ice_cube/validations/day.rb @@ -54,12 +54,13 @@ def build_ical(builder) validation_days.sort! # pick the right shortening, if applicable if validation_days == [0, 6] - 'on Weekends' + IceCube::I18n.t('ice_cube.on_weekends') elsif validation_days == (1..5).to_a - 'on Weekdays' + IceCube::I18n.t('ice_cube.on_weekdays') else - segments = validation_days.map { |d| "#{Date::DAYNAMES[d]}s" } - "on #{StringBuilder.sentence(segments)}" + day_names = ->(d){ "#{IceCube::I18n.t("ice_cube.days_on")[d]}" } + segments = validation_days.map(&day_names) + IceCube::I18n.t('ice_cube.on_days', days: StringBuilder.sentence(segments)) end end diff --git a/lib/ice_cube/validations/day_of_month.rb b/lib/ice_cube/validations/day_of_month.rb index ef2cd2ad..3acccd7a 100644 --- a/lib/ice_cube/validations/day_of_month.rb +++ b/lib/ice_cube/validations/day_of_month.rb @@ -43,9 +43,9 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_month) do |entries| - str = "on the #{StringBuilder.sentence(entries)} " - str << (entries.size == 1 ? 'day of the month' : 'days of the month') - str + sentence = StringBuilder.sentence(entries) + str = IceCube::I18n.t('ice_cube.days_of_month', count: entries.size, segments: sentence) + IceCube::I18n.t('ice_cube.on', sentence: str) end end diff --git a/lib/ice_cube/validations/day_of_week.rb b/lib/ice_cube/validations/day_of_week.rb index de40040e..a35ba258 100644 --- a/lib/ice_cube/validations/day_of_week.rb +++ b/lib/ice_cube/validations/day_of_week.rb @@ -45,7 +45,11 @@ def validate(step_time, schedule) end def build_s(builder) - builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}" + builder.piece(:day_of_week) << IceCube::I18n.t( + 'ice_cube.days_of_week', + segments: StringBuilder.nice_number(occ), + day: IceCube::I18n.t('ice_cube.date.day_names')[day] + ) end def build_hash(builder) @@ -62,7 +66,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_week) do |segments| - 'on the ' + segments.join(' and ') + sentence = segments.join(IceCube::I18n.t('ice_cube.array.two_words_connector')) + IceCube::I18n.t('ice_cube.on', sentence: sentence) end end diff --git a/lib/ice_cube/validations/day_of_year.rb b/lib/ice_cube/validations/day_of_year.rb index b68c980b..e4eba5e8 100644 --- a/lib/ice_cube/validations/day_of_year.rb +++ b/lib/ice_cube/validations/day_of_year.rb @@ -49,9 +49,9 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_year) do |entries| - str = "on the #{StringBuilder.sentence(entries)} " - str << (entries.size == 1 ? 'day of the year' : 'days of the year') - str + str = StringBuilder.sentence(entries) + sentence = IceCube::I18n.t('ice_cube.days_of_year', count: entries.size, segments: str) + IceCube::I18n.t('ice_cube.on', sentence: sentence) end end diff --git a/lib/ice_cube/validations/hour_of_day.rb b/lib/ice_cube/validations/hour_of_day.rb index a171722b..e5258006 100644 --- a/lib/ice_cube/validations/hour_of_day.rb +++ b/lib/ice_cube/validations/hour_of_day.rb @@ -44,8 +44,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:hour_of_day) do |segments| - str = "on the #{StringBuilder.sentence(segments)} " - str << (segments.size == 1 ? 'hour of the day' : 'hours of the day') + str = StringBuilder.sentence(segments) + IceCube::I18n.t('ice_cube.at_hours_of_the_day', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/hourly_interval.rb b/lib/ice_cube/validations/hourly_interval.rb index ce2484c4..789b82c1 100644 --- a/lib/ice_cube/validations/hourly_interval.rb +++ b/lib/ice_cube/validations/hourly_interval.rb @@ -35,7 +35,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Hourly' : "Every #{interval} hours" + builder.base = IceCube::I18n.t("ice_cube.each_hour", count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/minute_of_hour.rb b/lib/ice_cube/validations/minute_of_hour.rb index bf1e26f9..dcc70c6f 100644 --- a/lib/ice_cube/validations/minute_of_hour.rb +++ b/lib/ice_cube/validations/minute_of_hour.rb @@ -43,8 +43,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:minute_of_hour) do |segments| - str = "on the #{StringBuilder.sentence(segments)} " - str << (segments.size == 1 ? 'minute of the hour' : 'minutes of the hour') + str = StringBuilder.sentence(segments) + IceCube::I18n.t('ice_cube.on_minutes_of_hour', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/minutely_interval.rb b/lib/ice_cube/validations/minutely_interval.rb index eb03f366..63c8894b 100644 --- a/lib/ice_cube/validations/minutely_interval.rb +++ b/lib/ice_cube/validations/minutely_interval.rb @@ -35,7 +35,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Minutely' : "Every #{interval} minutes" + builder.base = IceCube::I18n.t('ice_cube.each_minute', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/month_of_year.rb b/lib/ice_cube/validations/month_of_year.rb index 5c9c0f98..d7431f2f 100644 --- a/lib/ice_cube/validations/month_of_year.rb +++ b/lib/ice_cube/validations/month_of_year.rb @@ -32,7 +32,7 @@ def dst_adjust? end def build_s(builder) - builder.piece(:month_of_year) << Date::MONTHNAMES[month] + builder.piece(:month_of_year) << IceCube::I18n.t("ice_cube.date.month_names")[month] end def build_hash(builder) @@ -44,7 +44,7 @@ def build_ical(builder) end StringBuilder.register_formatter(:month_of_year) do |segments| - "in #{StringBuilder.sentence(segments)}" + IceCube::I18n.t("ice_cube.in", target: StringBuilder.sentence(segments)) end end diff --git a/lib/ice_cube/validations/monthly_interval.rb b/lib/ice_cube/validations/monthly_interval.rb index 61fde0c5..3939d4a9 100644 --- a/lib/ice_cube/validations/monthly_interval.rb +++ b/lib/ice_cube/validations/monthly_interval.rb @@ -34,7 +34,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Monthly' : "Every #{interval} months" + builder.base = IceCube::I18n.t('ice_cube.each_month', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/second_of_minute.rb b/lib/ice_cube/validations/second_of_minute.rb index 6985623c..63b9a818 100644 --- a/lib/ice_cube/validations/second_of_minute.rb +++ b/lib/ice_cube/validations/second_of_minute.rb @@ -43,8 +43,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:second_of_minute) do |segments| - str = "on the #{StringBuilder.sentence(segments)} " - str << (segments.size == 1 ? 'second of the minute' : 'seconds of the minute') + str = StringBuilder.sentence(segments) + IceCube::I18n.t('ice_cube.on_seconds_of_minute', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/secondly_interval.rb b/lib/ice_cube/validations/secondly_interval.rb index 4e79b56e..a8a3d77d 100644 --- a/lib/ice_cube/validations/secondly_interval.rb +++ b/lib/ice_cube/validations/secondly_interval.rb @@ -32,7 +32,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Secondly' : "Every #{interval} seconds" + builder.base = IceCube::I18n.t("ice_cube.each_second", count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/until.rb b/lib/ice_cube/validations/until.rb index d834e431..748f2bdd 100644 --- a/lib/ice_cube/validations/until.rb +++ b/lib/ice_cube/validations/until.rb @@ -38,7 +38,8 @@ def validate(step_time, schedule) end def build_s(builder) - builder.piece(:until) << "until #{time.strftime(IceCube.to_s_time_format)}" + date = IceCube::I18n.l(time, format: IceCube.to_s_time_format) + builder.piece(:until) << IceCube::I18n.t('ice_cube.until', date: date) end def build_hash(builder) diff --git a/lib/ice_cube/validations/weekly_interval.rb b/lib/ice_cube/validations/weekly_interval.rb index 5bdf33be..50e7a6cb 100644 --- a/lib/ice_cube/validations/weekly_interval.rb +++ b/lib/ice_cube/validations/weekly_interval.rb @@ -44,7 +44,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Weekly' : "Every #{interval} weeks" + builder.base = IceCube::I18n.t('ice_cube.each_week', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/yearly_interval.rb b/lib/ice_cube/validations/yearly_interval.rb index 9daab221..3fa01673 100644 --- a/lib/ice_cube/validations/yearly_interval.rb +++ b/lib/ice_cube/validations/yearly_interval.rb @@ -32,7 +32,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Yearly' : "Every #{interval} years" + builder.base = IceCube::I18n.t('ice_cube.each_year', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/version.rb b/lib/ice_cube/version.rb index 95885de8..9c59ceea 100644 --- a/lib/ice_cube/version.rb +++ b/lib/ice_cube/version.rb @@ -1,5 +1,5 @@ module IceCube - VERSION = '0.13.0' + VERSION = '0.14.0' end diff --git a/spec/examples/from_ical_spec.rb b/spec/examples/from_ical_spec.rb index f1fec335..3694bdc2 100644 --- a/spec/examples/from_ical_spec.rb +++ b/spec/examples/from_ical_spec.rb @@ -102,14 +102,14 @@ module IceCube RRULE:FREQ=WEEKLY;BYDAY=TH;UNTIL=20130531T100000Z ICAL - ical_string_with_time_zones = <<-ICAL.gsub(/^\s*/,'') + ical_string_with_time_zones = <<-ICAL.gsub(/^\s*/,'') DTSTART;TZID=America/Denver:20130731T143000 DTEND:20130731T153000 RRULE:FREQ=WEEKLY EXDATE;TZID=America/Chicago:20130823T143000 ICAL - ical_string_with_multiple_exdates = <<-ICAL.gsub(/^\s*/, '') + ical_string_with_multiple_exdates = <<-ICAL.gsub(/^\s*/, '') DTSTART;TZID=America/Denver:20130731T143000 DTEND;TZID=America/Denver:20130731T153000 RRULE:FREQ=WEEKLY;UNTIL=20140730T203000Z;BYDAY=MO,WE,FR @@ -118,6 +118,11 @@ module IceCube EXDATE;TZID=America/Denver:20130807T143000 ICAL + ical_string_with_multiple_rules = <<-ICAL.gsub(/^\s*/, '' ) + DTSTART;TZID=CDT:20151005T195541 + RRULE:FREQ=WEEKLY;BYDAY=MO,TU + RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=SU;BYDAY=FR + ICAL def sorted_ical(ical) ical.split(/\n/).sort.map { |field| @@ -132,19 +137,23 @@ def sorted_ical(ical) it "loads an ICAL string" do expect(IceCube::Schedule.from_ical(ical_string)).to be_a(IceCube::Schedule) end + describe "parsing time zones" do it "sets the time zone of the start time" do schedule = IceCube::Schedule.from_ical(ical_string_with_time_zones) expect(schedule.start_time.time_zone).to eq ActiveSupport::TimeZone.new("America/Denver") end + it "uses the system time if a time zone is not explicity provided" do schedule = IceCube::Schedule.from_ical(ical_string_with_time_zones) expect(schedule.end_time).not_to respond_to :time_zone end + it "sets the time zone of the exception times" do schedule = IceCube::Schedule.from_ical(ical_string_with_time_zones) expect(schedule.exception_times[0].time_zone).to eq ActiveSupport::TimeZone.new("America/Chicago") end + it "adding the offset doesnt also change the time" do schedule = IceCube::Schedule.from_ical(ical_string_with_time_zones) expect(schedule.exception_times[0].hour).to eq 14 @@ -387,6 +396,13 @@ def sorted_ical(ical) schedule.exception_times.count.should == 3 end end + + describe 'multiple rules' do + it 'handles multiple recurrence rules' do + schedule = IceCube::Schedule.from_ical ical_string_with_multiple_rules + schedule.recurrence_rules.count.should == 2 + end + end end end diff --git a/spec/examples/hourly_rule_spec.rb b/spec/examples/hourly_rule_spec.rb index 77cd10be..199ed514 100644 --- a/spec/examples/hourly_rule_spec.rb +++ b/spec/examples/hourly_rule_spec.rb @@ -56,7 +56,7 @@ module IceCube schedule = double(start_time: t0 = Time.now) rule = Rule.hourly(7) rule.interval(5) - rule.next_time(t0 + 1, schedule, nil).should == t0 + 5.hours + rule.next_time(t0 + 1, schedule, nil).should == t0 + 5 * ONE_HOUR end it 'should produce the correct days for @interval = 3' do diff --git a/spec/examples/occurrence_spec.rb b/spec/examples/occurrence_spec.rb index e46f29ca..7498a476 100644 --- a/spec/examples/occurrence_spec.rb +++ b/spec/examples/occurrence_spec.rb @@ -28,7 +28,7 @@ end it "accepts a format option to comply with ActiveSupport" do - require 'active_support/core_ext/time' + # require 'active_support/core_ext/time' time_now = Time.current occurrence = Occurrence.new(time_now) diff --git a/spec/examples/schedule_spec.rb b/spec/examples/schedule_spec.rb index ea49c951..0add833d 100644 --- a/spec/examples/schedule_spec.rb +++ b/spec/examples/schedule_spec.rb @@ -1,4 +1,5 @@ require File.dirname(__FILE__) + '/../spec_helper' +require 'benchmark' describe IceCube::Schedule do @@ -451,14 +452,91 @@ end + describe :spans do + + it 'should find occurrence in past with duration beyond the start time' do + t0 = Time.utc(2015, 10, 1, 15, 31) + schedule = IceCube::Schedule.new(t0, :duration => 2 * IceCube::ONE_HOUR) + schedule.add_recurrence_rule IceCube::Rule.daily + next_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, :spans => true) + next_occ.should == t0 + end + + it 'should include occurrence in past with duration beyond the start time' do + t0 = Time.utc(2015, 10, 1, 15, 31) + schedule = IceCube::Schedule.new(t0, :duration => 2 * IceCube::ONE_HOUR) + schedule.add_recurrence_rule IceCube::Rule.daily.count(2) + occs = schedule.next_occurrences(10, t0 + IceCube::ONE_HOUR, :spans => true) + occs.should == [t0, t0 + IceCube::ONE_DAY] + end + + it 'should allow duration span on remaining_occurrences' do + t0 = Time.utc(2015, 10, 1, 00, 00) + schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_DAY) + schedule.add_recurrence_rule IceCube::Rule.daily.count(3) + occs = schedule.remaining_occurrences(t0 + IceCube::ONE_DAY + IceCube::ONE_HOUR, :spans => true) + occs.should == [t0 + IceCube::ONE_DAY, t0 + 2 * IceCube::ONE_DAY] + end + + it 'should include occurrences with duration spanning the requested start time' do + t0 = Time.utc(2015, 10, 1, 15, 31) + schedule = IceCube::Schedule.new(t0, :duration => 30 * IceCube::ONE_DAY) + long_event = schedule.remaining_occurrences_enumerator(t0 + IceCube::ONE_DAY, :spans => true).take(1) + long_event.should == [t0] + end + + it 'should find occurrences between including previous one with duration spanning start' do + t0 = Time.utc(2015, 10, 1, 10, 00) + schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR) + schedule.add_recurrence_rule IceCube::Rule.hourly.count(10) + occs = schedule.occurrences_between(t0 + IceCube::ONE_HOUR + 1, t0 + 3 * IceCube::ONE_HOUR + 1, :spans => true) + occs.length.should == 3 + end + + it 'should include long occurrences starting before and ending after' do + t0 = Time.utc(2015, 10, 1, 00, 00) + schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_DAY) + occs = schedule.occurrences_between(t0 + IceCube::ONE_HOUR, t0 + IceCube::ONE_DAY - IceCube::ONE_HOUR, :spans => true) + occs.should == [t0] + end + + it 'should not find occurrence with duration ending on start time' do + t0 = Time.utc(2015, 10, 1, 12, 00) + schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR) + schedule.occurs_between?(t0 + IceCube::ONE_HOUR, t0 + 2 * IceCube::ONE_HOUR, :spans => true).should be_false + end + + it 'should quickly fetch a future time from a recurring schedule' do + t0 = Time.utc(2000, 10, 1, 00, 00) + t1 = Time.utc(2015, 10, 1, 12, 00) + schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR - 1) + schedule.add_recurrence_rule IceCube::Rule.hourly + occ = nil + timing = Benchmark.realtime do + occ = schedule.remaining_occurrences_enumerator(t1, :spans => true).take(1) + end + timing.should < 0.1 + occ.should == [t1] + end + + it 'should not include occurrence ending on start time' do + t0 = Time.utc(2015, 10, 1, 10, 00) + schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR / 2) + schedule.add_recurrence_rule IceCube::Rule.minutely(30).count(6) + third_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, :spans => true) + third_occ.should == t0 + IceCube::ONE_HOUR + end + + end + describe :previous_occurrence do it 'returns the previous occurrence for a time in the schedule' do t0 = Time.utc(2013, 5, 18, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily - previous = schedule.previous_occurrence(t0 + 2 * ONE_DAY) - previous.should == t0 + ONE_DAY + previous = schedule.previous_occurrence(t0 + 2 * IceCube::ONE_DAY) + previous.should == t0 + IceCube::ONE_DAY end it 'returns nil given the start time' do @@ -485,16 +563,16 @@ t0 = Time.utc(2013, 5, 18, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily - previous = schedule.previous_occurrences(2, t0 + 3 * ONE_DAY) - previous.should == [t0 + ONE_DAY, t0 + 2 * ONE_DAY] + previous = schedule.previous_occurrences(2, t0 + 3 * IceCube::ONE_DAY) + previous.should == [t0 + IceCube::ONE_DAY, t0 + 2 * IceCube::ONE_DAY] end it 'limits the returned occurrences to a given count' do t0 = Time.utc(2013, 5, 18, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily - previous = schedule.previous_occurrences(999, t0 + 2 * ONE_DAY) - previous.should == [t0, t0 + ONE_DAY] + previous = schedule.previous_occurrences(999, t0 + 2 * IceCube::ONE_DAY) + previous.should == [t0, t0 + IceCube::ONE_DAY] end it 'returns empty array given the start time' do @@ -531,7 +609,7 @@ t1 = Time.utc(2013, 5, 31, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily.until(t1 + 1) - schedule.last(2).should == [t1 - ONE_DAY, t1] + schedule.last(2).should == [t1 - IceCube::ONE_DAY, t1] end it 'raises an error for a non-terminating schedule' do diff --git a/spec/examples/to_s_en_spec.rb b/spec/examples/to_s_en_spec.rb new file mode 100644 index 00000000..b6ca73f6 --- /dev/null +++ b/spec/examples/to_s_en_spec.rb @@ -0,0 +1,224 @@ +require File.dirname(__FILE__) + '/../spec_helper' +require 'i18n' +require 'ice_cube/null_i18n' + +describe IceCube::Schedule, 'to_s' do + + shared_examples "to_s in English" do + + it 'should represent its start time by default' do + t0 = Time.local(2013, 2, 14) + IceCube::Schedule.new(t0).to_s.should == 'February 14, 2013' + end + + it 'should have a useful base to_s representation for a secondly rule' do + IceCube::Rule.secondly.to_s.should == 'Secondly' + IceCube::Rule.secondly(2).to_s.should == 'Every 2 seconds' + end + + it 'should have a useful base to_s representation for a minutely rule' do + IceCube::Rule.minutely.to_s.should == 'Minutely' + IceCube::Rule.minutely(2).to_s.should == 'Every 2 minutes' + end + + it 'should have a useful base to_s representation for a hourly rule' do + IceCube::Rule.hourly.to_s.should == 'Hourly' + IceCube::Rule.hourly(2).to_s.should == 'Every 2 hours' + end + + it 'should have a useful base to_s representation for a daily rule' do + IceCube::Rule.daily.to_s.should == 'Daily' + IceCube::Rule.daily(2).to_s.should == 'Every 2 days' + end + + it 'should have a useful base to_s representation for a weekly rule' do + IceCube::Rule.weekly.to_s.should == 'Weekly' + IceCube::Rule.weekly(2).to_s.should == 'Every 2 weeks' + end + + it 'should have a useful base to_s representation for a monthly rule' do + IceCube::Rule.monthly.to_s.should == 'Monthly' + IceCube::Rule.monthly(2).to_s.should == 'Every 2 months' + end + + it 'should have a useful base to_s representation for a yearly rule' do + IceCube::Rule.yearly.to_s.should == 'Yearly' + IceCube::Rule.yearly(2).to_s.should == 'Every 2 years' + end + + it 'should work with various sentence types properly' do + IceCube::Rule.weekly.to_s.should == 'Weekly' + IceCube::Rule.weekly.day(:monday).to_s.should == 'Weekly on Mondays' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'Weekly on Mondays and Tuesdays' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'Weekly on Mondays, Tuesdays, and Wednesdays' + end + + it 'should show saturday and sunday as weekends' do + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'Weekly on Weekends' + end + + it 'should not show saturday and sunday as weekends when other days are present also' do + IceCube::Rule.weekly.day(:sunday, :monday, :saturday).to_s.should == + 'Weekly on Sundays, Mondays, and Saturdays' + end + + it 'should reorganize days to be in order' do + IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == + 'Weekly on Mondays and Tuesdays' + end + + it 'should show weekdays as such' do + IceCube::Rule.weekly.day( + :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Weekly on Weekdays' + end + + it 'should not show weekdays as such when a weekend day is present' do + IceCube::Rule.weekly.day( + :sunday, :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Weekly on Sundays, Mondays, Tuesdays, Wednesdays, Thursdays, and Fridays' + end + + it 'should show start time for an empty schedule' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.to_s.should == "March 20, 2010" + end + + it 'should work with a single date' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == "March 20, 2010" + end + + it 'should work with additional dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 21) + schedule.to_s.should == 'March 20, 2010 / March 21, 2010' + end + + it 'should order dates that are out of order' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 19) + schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + end + + it 'should remove duplicated start time' do + schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 20) + schedule.add_recurrence_time t0 + schedule.to_s.should == 'March 20, 2010' + end + + it 'should remove duplicate rtimes' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 19) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + end + + it 'should work with rules and dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 19) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.to_s.should == 'March 20, 2010 / Weekly' + end + + it 'should work with rules and times and exception times' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_exception_time Time.local(2010, 3, 20) # ignored + schedule.add_exception_time Time.local(2010, 3, 21) + schedule.to_s.should == 'Weekly / not on March 20, 2010 / not on March 21, 2010' + end + + it 'should work with a single rrule' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly.day_of_week(:monday => [1]) + schedule.to_s.should == schedule.rrules[0].to_s + end + + it 'should be able to say the last Thursday of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-1]).to_s + rule_str.should == 'Monthly on the last Thursday' + end + + it 'should be able to say what months of the year something happens' do + rule_str = IceCube::Rule.yearly.month_of_year(:june, :july).to_s + rule_str.should == 'Yearly in June and July' + end + + it 'should be able to say the second to last monday of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-2]).to_s + rule_str.should == 'Monthly on the 2nd to last Thursday' + end + + it 'should join the first and last weekdays of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [1, -1]).to_s + rule_str.should == 'Monthly on the 1st Thursday and last Thursday' + end + + it 'should be able to say the days of the month something happens' do + rule_str = IceCube::Rule.monthly.day_of_month(1, 15, 30).to_s + rule_str.should == 'Monthly on the 1st, 15th, and 30th days of the month' + end + + it 'should be able to say what day of the year something happens' do + rule_str = IceCube::Rule.yearly.day_of_year(30).to_s + rule_str.should == 'Yearly on the 30th day of the year' + end + + it 'should be able to say what hour of the day something happens' do + rule_str = IceCube::Rule.daily.hour_of_day(6, 12).to_s + rule_str.should == 'Daily on the 6th and 12th hours of the day' + end + + it 'should be able to say what minute of an hour something happens - with special suffix minutes' do + rule_str = IceCube::Rule.hourly.minute_of_hour(10, 11, 12, 13, 14, 15).to_s + rule_str.should == 'Hourly on the 10th, 11th, 12th, 13th, 14th, and 15th minutes of the hour' + end + + it 'should be able to say what seconds of the minute something happens' do + rule_str = IceCube::Rule.minutely.second_of_minute(10, 11).to_s + rule_str.should == 'Minutely on the 10th and 11th seconds of the minute' + end + + it 'should be able to reflect until dates' do + schedule = IceCube::Schedule.new(Time.now) + schedule.rrule IceCube::Rule.weekly.until(Time.local(2012, 2, 3)) + schedule.to_s.should == 'Weekly until February 3, 2012' + end + + it 'should be able to reflect count' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(1) + schedule.to_s.should == 'Weekly 1 time' + end + + it 'should be able to reflect count (proper pluralization)' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(2) + schedule.to_s.should == 'Weekly 2 times' + end + + end + + context "without I18n" do + before { IceCube::I18n.stub(:backend) { IceCube::NullI18n } } + + it_behaves_like "to_s in English" + end + + context "with I18n" do + before(:each) { I18n.locale = :en } + + it "uses I18n" do + IceCube::I18n.backend.should == I18n + end + + it_behaves_like "to_s in English" + end + +end diff --git a/spec/examples/to_s_es_spec.rb b/spec/examples/to_s_es_spec.rb new file mode 100644 index 00000000..dadaa87e --- /dev/null +++ b/spec/examples/to_s_es_spec.rb @@ -0,0 +1,209 @@ +# encoding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe IceCube::Schedule, 'to_s' do + before :each do + I18n.locale = :es + end + + after :all do + I18n.locale = :en + end + + it 'should represent its start time by default' do + t0 = Time.local(2013, 2, 14) + IceCube::Schedule.new(t0).to_s.should == '14 de Febrero de 2013' + end + + it 'should have a useful base to_s representation for a secondly rule' do + IceCube::Rule.secondly.to_s.should == 'Cada segundo' + IceCube::Rule.secondly(2).to_s.should == 'Cada 2 segundos' + end + + it 'should have a useful base to_s representation for a minutely rule' do + IceCube::Rule.minutely.to_s.should == 'Cada minuto' + IceCube::Rule.minutely(2).to_s.should == 'Cada 2 minutos' + end + + it 'should have a useful base to_s representation for a hourly rule' do + IceCube::Rule.hourly.to_s.should == 'Cada hora' + IceCube::Rule.hourly(2).to_s.should == 'Cada 2 horas' + end + + it 'should have a useful base to_s representation for a daily rule' do + IceCube::Rule.daily.to_s.should == 'Diariamente' + IceCube::Rule.daily(2).to_s.should == 'Cada 2 días' + end + + it 'should have a useful base to_s representation for a weekly rule' do + IceCube::Rule.weekly.to_s.should == 'Semanalmente' + IceCube::Rule.weekly(2).to_s.should == 'Cada 2 semanas' + end + + it 'should have a useful base to_s representation for a monthly rule' do + IceCube::Rule.monthly.to_s.should == 'Mensualmente' + IceCube::Rule.monthly(2).to_s.should == 'Cada 2 meses' + end + + it 'should have a useful base to_s representation for a yearly rule' do + IceCube::Rule.yearly.to_s.should == 'Anualmente' + IceCube::Rule.yearly(2).to_s.should == 'Cada 2 años' + end + + it 'should work with various sentence types properly' do + IceCube::Rule.weekly.to_s.should == 'Semanalmente' + IceCube::Rule.weekly.day(:monday).to_s.should == 'Semanalmente los lunes' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'Semanalmente los lunes y los martes' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'Semanalmente los lunes, los martes y los miércoles' + end + + it 'should show saturday and sunday as weekends' do + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'Semanalmente en fin de semana' + end + + it 'should not show saturday and sunday as weekends when other days are present also' do + IceCube::Rule.weekly.day(:sunday, :monday, :saturday).to_s.should == + 'Semanalmente los domingos, los lunes y los sábados' + end + + it 'should reorganize days to be in order' do + IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == + 'Semanalmente los lunes y los martes' + end + + it 'should show weekdays as such' do + IceCube::Rule.weekly.day( + :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Semanalmente en días laborables' + end + + it 'should not show weekdays as such when a weekend day is present' do + IceCube::Rule.weekly.day( + :sunday, :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Semanalmente los domingos, los lunes, los martes, los miércoles, los jueves y los viernes' + end + + it 'should work with a single date' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == "20 de Marzo de 2010" + end + + it 'should work with additional dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 21) + schedule.to_s.should == '20 de Marzo de 2010, 21 de Marzo de 2010' + end + + it 'should order dates that are out of order' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 19) + schedule.to_s.should == '19 de Marzo de 2010, 20 de Marzo de 2010' + end + + it 'should remove duplicated start time' do + schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 20) + schedule.add_recurrence_time t0 + schedule.to_s.should == '20 de Marzo de 2010' + end + + it 'should remove duplicate rtimes' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == '20 de Marzo de 2010' + end + + it 'should work with rules and dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.to_s.should == '20 de Marzo de 2010, Semanalmente' + end + + it 'should work with rules and dates and exdates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_exception_date Time.local(2010, 3, 20) # ignored + schedule.add_exception_date Time.local(2010, 3, 21) + # TODO: this text should be improved to add sentence connector + schedule.to_s.should == 'Semanalmente, excepto el 20 de Marzo de 2010, excepto el 21 de Marzo de 2010' + end + + it 'should work with a single rrule' do + pending 'remove dependency' + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly.day_of_week(:monday => [1]) + schedule.to_s.should == schedule.rrules[0].to_s + end + + it 'should be able to say the last monday of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-1]).to_s + rule_str.should == 'Mensualmente en el último Jueves' + end + + it 'should be able to say what months of the year something happens' do + rule_str = IceCube::Rule.yearly.month_of_year(:june, :july).to_s + rule_str.should == 'Anualmente en Junio y Julio' + end + + it 'should be able to say the second to last monday of the month' do + pending 'penultimo' + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-2]).to_s + rule_str.should == 'Mensualmente del segundo al último Jueves del mes' + end + + it 'should be able to say the days of the month something happens' do + rule_str = IceCube::Rule.monthly.day_of_month(1, 15, 30).to_s + rule_str.should == 'Mensualmente en los días 1º, 15º y 30º del mes' + end + + it 'should be able to say what day of the year something happens' do + rule_str = IceCube::Rule.yearly.day_of_year(30).to_s + rule_str.should == 'Anualmente en el día 30º' + end + + it 'should be able to say what hour of the day something happens' do + rule_str = IceCube::Rule.daily.hour_of_day(6, 12).to_s + rule_str.should == 'Diariamente en las horas 6º y 12º' + end + + it 'should be able to say what minute of an hour something happens - with special suffix minutes' do + rule_str = IceCube::Rule.hourly.minute_of_hour(10, 11, 12, 13, 14, 15).to_s + rule_str.should == 'Cada hora en los minutos 10º, 11º, 12º, 13º, 14º y 15º' + end + + it 'should be able to say what seconds of the minute something happens' do + rule_str = IceCube::Rule.minutely.second_of_minute(10, 11).to_s + rule_str.should == 'Cada minuto en los segundos 10º y 11º del minuto' + end + + it 'should be able to reflect until dates' do + schedule = IceCube::Schedule.new(Time.now) + schedule.rrule IceCube::Rule.weekly.until(Time.local(2012, 2, 3)) + schedule.to_s.should == 'Semanalmente hasta el 3 de Febrero de 2012' + end + + it 'should be able to reflect count' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(1) + schedule.to_s.should == 'Semanalmente 1 vez' + end + + it 'should be able to reflect count (proper pluralization)' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(2) + schedule.to_s.should == 'Semanalmente 2 veces' + end + + # it 'should work when an end_time is set' do + # schedule = IceCube::Schedule.new(Time.local(2012, 8, 31), :end_time => Time.local(2012, 10, 31)) + # schedule.add_recurrence_rule IceCube::Rule.daily.count(2) + # schedule.to_s.should == 'Diariamente 2 veces, hasta el 31 de Octubre 2012' + # end + +end diff --git a/spec/examples/to_s_spec.rb b/spec/examples/to_s_ja_spec.rb similarity index 68% rename from spec/examples/to_s_spec.rb rename to spec/examples/to_s_ja_spec.rb index fef6e531..011213e3 100644 --- a/spec/examples/to_s_spec.rb +++ b/spec/examples/to_s_ja_spec.rb @@ -1,124 +1,133 @@ +# encoding: utf-8 require File.dirname(__FILE__) + '/../spec_helper' describe IceCube::Schedule, 'to_s' do + before :each do + I18n.locale = :ja + end + + after :all do + I18n.locale = :en + end + it 'should represent its start time by default' do t0 = Time.local(2013, 2, 14) - IceCube::Schedule.new(t0).to_s.should == 'February 14, 2013' + IceCube::Schedule.new(t0).to_s.should == '2013年02月14日' end it 'should have a useful base to_s representation for a secondly rule' do - IceCube::Rule.secondly.to_s.should == 'Secondly' - IceCube::Rule.secondly(2).to_s.should == 'Every 2 seconds' + IceCube::Rule.secondly.to_s.should == '毎秒' + IceCube::Rule.secondly(2).to_s.should == '2秒ごと' end it 'should have a useful base to_s representation for a minutely rule' do - IceCube::Rule.minutely.to_s.should == 'Minutely' - IceCube::Rule.minutely(2).to_s.should == 'Every 2 minutes' + IceCube::Rule.minutely.to_s.should == '毎分' + IceCube::Rule.minutely(2).to_s.should == '2分ごと' end it 'should have a useful base to_s representation for a hourly rule' do - IceCube::Rule.hourly.to_s.should == 'Hourly' - IceCube::Rule.hourly(2).to_s.should == 'Every 2 hours' + IceCube::Rule.hourly.to_s.should == '毎時間' + IceCube::Rule.hourly(2).to_s.should == '2時間ごと' end it 'should have a useful base to_s representation for a daily rule' do - IceCube::Rule.daily.to_s.should == 'Daily' - IceCube::Rule.daily(2).to_s.should == 'Every 2 days' + IceCube::Rule.daily.to_s.should == '毎日' + IceCube::Rule.daily(2).to_s.should == '2日ごと' end it 'should have a useful base to_s representation for a weekly rule' do - IceCube::Rule.weekly.to_s.should == 'Weekly' - IceCube::Rule.weekly(2).to_s.should == 'Every 2 weeks' + IceCube::Rule.weekly.to_s.should == '毎週' + IceCube::Rule.weekly(2).to_s.should == '2週間ごと' end it 'should have a useful base to_s representation for a monthly rule' do - IceCube::Rule.monthly.to_s.should == 'Monthly' - IceCube::Rule.monthly(2).to_s.should == 'Every 2 months' + IceCube::Rule.monthly.to_s.should == '毎月' + IceCube::Rule.monthly(2).to_s.should == '2ヶ月ごと' end it 'should have a useful base to_s representation for a yearly rule' do - IceCube::Rule.yearly.to_s.should == 'Yearly' - IceCube::Rule.yearly(2).to_s.should == 'Every 2 years' + IceCube::Rule.yearly.to_s.should == '毎年' + IceCube::Rule.yearly(2).to_s.should == '2年ごと' end it 'should work with various sentence types properly' do - IceCube::Rule.weekly.to_s.should == 'Weekly' - IceCube::Rule.weekly.day(:monday).to_s.should == 'Weekly on Mondays' - IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'Weekly on Mondays and Tuesdays' - IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'Weekly on Mondays, Tuesdays, and Wednesdays' + IceCube::Rule.weekly.to_s.should == '毎週' + IceCube::Rule.weekly.day(:monday).to_s.should == '毎週月曜' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == '毎週月曜、火曜' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == '毎週月曜、火曜、水曜' end it 'should show saturday and sunday as weekends' do - IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'Weekly on Weekends' + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == '毎週週末' end it 'should not show saturday and sunday as weekends when other days are present also' do IceCube::Rule.weekly.day(:sunday, :monday, :saturday).to_s.should == - 'Weekly on Sundays, Mondays, and Saturdays' + '毎週日曜、月曜、土曜' end it 'should reorganize days to be in order' do IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == - 'Weekly on Mondays and Tuesdays' + '毎週月曜、火曜' end it 'should show weekdays as such' do IceCube::Rule.weekly.day( :monday, :tuesday, :wednesday, :thursday, :friday - ).to_s.should == 'Weekly on Weekdays' + ).to_s.should == '毎週平日' end it 'should not show weekdays as such when a weekend day is present' do IceCube::Rule.weekly.day( :sunday, :monday, :tuesday, :wednesday, :thursday, :friday - ).to_s.should == 'Weekly on Sundays, Mondays, Tuesdays, Wednesdays, Thursdays, and Fridays' + ).to_s.should == '毎週日曜、月曜、火曜、水曜、木曜、金曜' end it 'should show start time for an empty schedule' do schedule = IceCube::Schedule.new Time.local(2010, 3, 20) - schedule.to_s.should == "March 20, 2010" + schedule.to_s.should == "2010年03月20日" end it 'should work with a single date' do schedule = IceCube::Schedule.new Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 20) - schedule.to_s.should == "March 20, 2010" + schedule.to_s.should == "2010年03月20日" end it 'should work with additional dates' do schedule = IceCube::Schedule.new Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 21) - schedule.to_s.should == 'March 20, 2010 / March 21, 2010' + schedule.to_s.should == '2010年03月20日 / 2010年03月21日' end it 'should order dates that are out of order' do schedule = IceCube::Schedule.new(t0 = Time.local(2010, 3, 20)) schedule.add_recurrence_time t1 = Time.local(2010, 3, 19) - schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + schedule.to_s.should == '2010年03月19日 / 2010年03月20日' end it 'should remove duplicated start time' do schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 20) schedule.add_recurrence_time t0 - schedule.to_s.should == 'March 20, 2010' + schedule.to_s.should == '2010年03月20日' end it 'should remove duplicate rtimes' do schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 19) schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 20) - schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + schedule.to_s.should == '2010年03月19日 / 2010年03月20日' end it 'should work with rules and dates' do schedule = IceCube::Schedule.new Time.local(2010, 3, 19) schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_recurrence_rule IceCube::Rule.weekly - schedule.to_s.should == 'March 20, 2010 / Weekly' + schedule.to_s.should == '2010年03月20日 / 毎週' end it 'should work with rules and times and exception times' do @@ -127,7 +136,7 @@ schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_exception_time Time.local(2010, 3, 20) # ignored schedule.add_exception_time Time.local(2010, 3, 21) - schedule.to_s.should == 'Weekly / not on March 20, 2010 / not on March 21, 2010' + schedule.to_s.should == '毎週 / 2010年03月20日以外 / 2010年03月21日以外' end it 'should work with a single rrule' do @@ -138,65 +147,65 @@ it 'should be able to say the last Thursday of the month' do rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-1]).to_s - rule_str.should == 'Monthly on the last Thursday' + rule_str.should == '毎月最終木曜' end it 'should be able to say what months of the year something happens' do rule_str = IceCube::Rule.yearly.month_of_year(:june, :july).to_s - rule_str.should == 'Yearly in June and July' + rule_str.should == '毎年6月、7月' end it 'should be able to say the second to last monday of the month' do rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-2]).to_s - rule_str.should == 'Monthly on the 2nd to last Thursday' + rule_str.should == '毎月最後から2番目の木曜' end it 'should join the first and last weekdays of the month' do rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [1, -1]).to_s - rule_str.should == 'Monthly on the 1st Thursday and last Thursday' + rule_str.should == '毎月1木曜、最終木曜' end it 'should be able to say the days of the month something happens' do rule_str = IceCube::Rule.monthly.day_of_month(1, 15, 30).to_s - rule_str.should == 'Monthly on the 1st, 15th, and 30th days of the month' + rule_str.should == '毎月1、15、30日' end it 'should be able to say what day of the year something happens' do rule_str = IceCube::Rule.yearly.day_of_year(30).to_s - rule_str.should == 'Yearly on the 30th day of the year' + rule_str.should == '毎年30日' end it 'should be able to say what hour of the day something happens' do rule_str = IceCube::Rule.daily.hour_of_day(6, 12).to_s - rule_str.should == 'Daily on the 6th and 12th hours of the day' + rule_str.should == '毎日6、12時' end it 'should be able to say what minute of an hour something happens - with special suffix minutes' do rule_str = IceCube::Rule.hourly.minute_of_hour(10, 11, 12, 13, 14, 15).to_s - rule_str.should == 'Hourly on the 10th, 11th, 12th, 13th, 14th, and 15th minutes of the hour' + rule_str.should == '毎時間10、11、12、13、14、15分' end it 'should be able to say what seconds of the minute something happens' do rule_str = IceCube::Rule.minutely.second_of_minute(10, 11).to_s - rule_str.should == 'Minutely on the 10th and 11th seconds of the minute' + rule_str.should == '毎分10、11秒' end it 'should be able to reflect until dates' do schedule = IceCube::Schedule.new(Time.now) schedule.rrule IceCube::Rule.weekly.until(Time.local(2012, 2, 3)) - schedule.to_s.should == 'Weekly until February 3, 2012' + schedule.to_s.should == '2012年02月03日まで毎週' end it 'should be able to reflect count' do schedule = IceCube::Schedule.new(Time.now) schedule.add_recurrence_rule IceCube::Rule.weekly.count(1) - schedule.to_s.should == 'Weekly 1 time' + schedule.to_s.should == '毎週1回' end it 'should be able to reflect count (proper pluralization)' do schedule = IceCube::Schedule.new(Time.now) schedule.add_recurrence_rule IceCube::Rule.weekly.count(2) - schedule.to_s.should == 'Weekly 2 times' + schedule.to_s.should == '毎週2回' end end diff --git a/spec/examples/yearly_rule_spec.rb b/spec/examples/yearly_rule_spec.rb index d7cbcdb6..52438c83 100644 --- a/spec/examples/yearly_rule_spec.rb +++ b/spec/examples/yearly_rule_spec.rb @@ -2,12 +2,12 @@ describe IceCube::YearlyRule, 'interval validation' do it 'converts a string integer to an actual int when using the interval method' do - rule = Rule.yearly.interval("2") + rule = IceCube::Rule.yearly.interval("2") rule.validations_for(:interval).first.interval.should == 2 end it 'converts a string integer to an actual int when using the initializer' do - rule = Rule.yearly("3") + rule = IceCube::Rule.yearly("3") rule.validations_for(:interval).first.interval.should == 3 end