ice_cube 0.6.14 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/lib/ice_cube.rb +63 -37
  2. data/lib/ice_cube/builders/hash_builder.rb +27 -0
  3. data/lib/ice_cube/builders/ical_builder.rb +59 -0
  4. data/lib/ice_cube/builders/string_builder.rb +74 -0
  5. data/lib/ice_cube/errors/count_exceeded.rb +7 -0
  6. data/lib/ice_cube/errors/until_exceeded.rb +7 -0
  7. data/lib/ice_cube/rule.rb +85 -147
  8. data/lib/ice_cube/rules/daily_rule.rb +5 -27
  9. data/lib/ice_cube/rules/hourly_rule.rb +6 -26
  10. data/lib/ice_cube/rules/minutely_rule.rb +5 -25
  11. data/lib/ice_cube/rules/monthly_rule.rb +6 -30
  12. data/lib/ice_cube/rules/secondly_rule.rb +5 -26
  13. data/lib/ice_cube/rules/weekly_rule.rb +5 -36
  14. data/lib/ice_cube/rules/yearly_rule.rb +8 -34
  15. data/lib/ice_cube/schedule.rb +257 -229
  16. data/lib/ice_cube/single_occurrence_rule.rb +28 -0
  17. data/lib/ice_cube/time_util.rb +202 -76
  18. data/lib/ice_cube/validated_rule.rb +107 -0
  19. data/lib/ice_cube/validations/count.rb +56 -0
  20. data/lib/ice_cube/validations/daily_interval.rb +51 -0
  21. data/lib/ice_cube/validations/day.rb +45 -31
  22. data/lib/ice_cube/validations/day_of_month.rb +44 -44
  23. data/lib/ice_cube/validations/day_of_week.rb +60 -47
  24. data/lib/ice_cube/validations/day_of_year.rb +48 -44
  25. data/lib/ice_cube/validations/hour_of_day.rb +42 -30
  26. data/lib/ice_cube/validations/hourly_interval.rb +50 -0
  27. data/lib/ice_cube/validations/lock.rb +47 -0
  28. data/lib/ice_cube/validations/minute_of_hour.rb +42 -31
  29. data/lib/ice_cube/validations/minutely_interval.rb +50 -0
  30. data/lib/ice_cube/validations/month_of_year.rb +39 -30
  31. data/lib/ice_cube/validations/monthly_interval.rb +47 -0
  32. data/lib/ice_cube/validations/schedule_lock.rb +41 -0
  33. data/lib/ice_cube/validations/second_of_minute.rb +39 -30
  34. data/lib/ice_cube/validations/secondly_interval.rb +50 -0
  35. data/lib/ice_cube/validations/until.rb +49 -0
  36. data/lib/ice_cube/validations/weekly_interval.rb +50 -0
  37. data/lib/ice_cube/validations/yearly_interval.rb +45 -0
  38. data/lib/ice_cube/version.rb +2 -2
  39. data/spec/spec_helper.rb +13 -0
  40. metadata +50 -9
  41. data/lib/ice_cube/rule_occurrence.rb +0 -94
  42. data/lib/ice_cube/validation.rb +0 -44
  43. data/lib/ice_cube/validation_types.rb +0 -137
@@ -1,44 +0,0 @@
1
- module IceCube
2
-
3
- class Validation
4
-
5
- NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
6
- SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }
7
-
8
- def self.adjust(goal, date)
9
- return goal if goal.utc_offset == date.utc_offset
10
- goal - goal.utc_offset + date.utc_offset
11
- end
12
-
13
- # influences by ActiveSupport's to_sentence
14
- def self.sentence(array)
15
- case array.length
16
- when 0 ; ''
17
- when 1 ; array[0].to_s
18
- when 2 ; "#{array[0]} and #{array[1]}"
19
- else ; "#{array[0...-1].join(', ')}, and #{array[-1]}"
20
- end
21
- end
22
-
23
- def self.nice_numbers(array)
24
- array.sort!
25
- self.sentence array.map { |d| nice_number(d) }
26
- end
27
-
28
- private
29
-
30
- def self.nice_number(number)
31
- if number == -1
32
- 'last'
33
- elsif number < -1
34
- suffix = SPECIAL_SUFFIX.include?(number) ? SPECIAL_SUFFIX[number] : NUMBER_SUFFIX[number.abs % 10]
35
- number.abs.to_s << suffix << ' to last'
36
- else
37
- suffix = SPECIAL_SUFFIX.include?(number) ? SPECIAL_SUFFIX[number] : NUMBER_SUFFIX[number.abs % 10]
38
- number.to_s << suffix
39
- end
40
- end
41
-
42
- end
43
-
44
- end
@@ -1,137 +0,0 @@
1
- module ValidationTypes
2
-
3
- def second_of_minute(*seconds)
4
- @validations[:second_of_minute] ||= []
5
- @validation_types[:second_of_minute] ||= IceCube::SecondOfMinuteValidation.new(self)
6
- seconds.each do |second|
7
- raise ArgumentError.new('Argument must be a valid second') unless second < 60 && second >= 0
8
- @validations[:second_of_minute] << second
9
- end
10
- # enforce uniqueness
11
- @validations[:second_of_minute].uniq!
12
- self
13
- end
14
-
15
- # Specify what days of the week this rule should occur on.
16
- # ie: Schedule.weekly.day_of_week(:monday) would create a rule that
17
- # occurs every monday.
18
- def day(*days)
19
- @validations[:day] ||= []
20
- @validation_types[:day] ||= IceCube::DayValidation.new(self)
21
- days.each do |day|
22
- if day.is_a?(Integer)
23
- # integer type argument
24
- raise ArgumentError.new('Argument must be a valid day of week (0-6)') unless day >= 0 && day <= 6
25
- @validations[:day] << day
26
- else
27
- # symbol type argument
28
- raise ArgumentError.new('Argument must be a valid day of the week') unless IceCube::DAYS.has_key?(day)
29
- @validations[:day] << IceCube::DAYS[day]
30
- end
31
- end
32
- # enforce uniqueness
33
- @validations[:day].uniq!
34
- self
35
- end
36
-
37
- # Specify what days of the year this rule applies to.
38
- # ie: Schedule.yearly(2).days_of_year(17, -1) would create a
39
- # rule which occurs every 17th and last day of every other year.
40
- # Note: you cannot combine month_of_year and day_of_year in the same rule.
41
- def day_of_year(*days)
42
- @validations[:day_of_year] ||= []
43
- @validation_types[:day_of_year] ||= IceCube::DayOfYearValidation.new(self)
44
- days.each do |day|
45
- raise ArgumentError.new('Argument must be a valid day') if day.abs > 366
46
- raise ArgumentError.new('Argument must be non-zero') if day == 0
47
- @validations[:day_of_year] << day
48
- end
49
- # enforce uniqueness
50
- @validations[:day_of_year].uniq!
51
- self
52
- end
53
-
54
- # Specify what months of the year this rule applies to.
55
- # ie: Schedule.yearly(2).month_of_year(:january, :march) would create a
56
- # rule which occurs every january and march, every other year
57
- # Note: you cannot combine day_of_year and month_of_year in the same rule.
58
- def month_of_year(*months)
59
- @validations[:month_of_year] ||= []
60
- @validation_types[:month_of_year] ||= IceCube::MonthOfYearValidation.new(self)
61
- months.each do |month|
62
- if month.is_a?(Integer)
63
- # integer type argument
64
- raise ArgumentError.new('Argument must be a valid month (1-12)') unless month >= 1 && month <= 12
65
- @validations[:month_of_year] << month
66
- else
67
- #symbol type argument
68
- raise ArgumentError.new('Argument must be a valid month') unless IceCube::MONTHS.has_key?(month)
69
- @validations[:month_of_year] << IceCube::MONTHS[month]
70
- end
71
- end
72
- # enforce uniqueness
73
- @validations[:month_of_year].uniq!
74
- self
75
- end
76
-
77
- # Specify the day(s) of the week that this rule should occur
78
- # on. ie: rule.day_of_week(:monday => [1, -1]) would mean
79
- # that this rule should occur on the first and last mondays of each month.
80
- def day_of_week(days)
81
- @validations[:day_of_week] ||= {}
82
- @validation_types[:day_of_week] ||= IceCube::DayOfWeekValidation.new(self)
83
- days.each do |day, occurrences|
84
- unless day.is_a?(Integer)
85
- raise ArgumentError.new('Argument must be a valid day of week') unless IceCube::DAYS.has_key?(day)
86
- day = IceCube::DAYS[day]
87
- end
88
- raise ArgumentError.new('Argument must be a valid day of week (0-6)') unless day >= 0 && day <= 6
89
- # add the day
90
- @validations[:day_of_week][day] ||= []
91
- @validations[:day_of_week][day].concat(occurrences)
92
- @validations[:day_of_week][day].uniq!
93
- end
94
- self
95
- end
96
-
97
- def hour_of_day(*hours)
98
- @validations[:hour_of_day] ||= []
99
- @validation_types[:hour_of_day] ||= IceCube::HourOfDayValidation.new(self)
100
- hours.each do |hour|
101
- raise ArgumentError.new('Argument must be a valid hour') unless hour < 24 && hour >= 0
102
- @validations[:hour_of_day] << hour
103
- end
104
- # enforce uniqueness
105
- @validations[:hour_of_day].uniq!
106
- self
107
- end
108
-
109
- # Specify the days of the month that this rule should
110
- # occur on. ie: rule.day_of_month(1, -1) would mean that
111
- # this rule should occur on the first and last day of every month.
112
- def day_of_month(*days)
113
- @validations[:day_of_month] ||= []
114
- @validation_types[:day_of_month] ||= IceCube::DayOfMonthValidation.new(self)
115
- days.each do |day|
116
- raise ArgumentError.new('Argument must be a valid date') if day.abs > 31
117
- raise ArgumentError.new('Argument must be non-zero') if day == 0
118
- @validations[:day_of_month] << day
119
- end
120
- # enforce uniqueness
121
- @validations[:day_of_month].uniq!
122
- self
123
- end
124
-
125
- def minute_of_hour(*minutes)
126
- @validations[:minute_of_hour] ||= []
127
- @validation_types[:minute_of_hour] ||= IceCube::MinuteOfHourValidation.new(self)
128
- minutes.each do |minute|
129
- raise ArgumentError.new('Argument must be a valid minute') unless minute < 60 && minute >= 0
130
- @validations[:minute_of_hour] << minute
131
- end
132
- # enforce uniqueness
133
- @validations[:minute_of_hour].uniq!
134
- self
135
- end
136
-
137
- end