ice_cube 0.5.3 → 0.6.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.
data/lib/ice_cube/rule.rb CHANGED
@@ -25,9 +25,9 @@ module IceCube
25
25
  end
26
26
  rule
27
27
  end
28
-
29
- def to_yaml
30
- to_hash.to_yaml
28
+
29
+ def to_yaml(options = {})
30
+ to_hash.to_yaml(options)
31
31
  end
32
32
 
33
33
  def self.from_yaml(str)
@@ -134,6 +134,7 @@ module IceCube
134
134
  representation = ''
135
135
  representation = (@interval == 1 ? singular : plural)
136
136
  representation << @validation_types.values.map { |v| ' ' + v.send(:to_s) }.join()
137
+ representation << " #{occurrence_count} #{@occurrence_count == 1 ? 'time' : 'times'}" if @occurrence_count
137
138
  representation
138
139
  end
139
140
 
@@ -1,13 +1,12 @@
1
1
  module IceCube
2
2
 
3
3
  class RuleOccurrence
4
-
5
- include Comparable
6
-
7
- #allow to be compared to dates
8
- def <=>(other)
9
- to_time <=> other
10
- end
4
+
5
+ # allow to be compared to dates (awesome but not used anymore)
6
+ # include Comparable
7
+ # def <=>(other)
8
+ # to_time <=> other
9
+ # end
11
10
 
12
11
  def to_time
13
12
  @date
@@ -26,6 +25,17 @@ module IceCube
26
25
  find_occurrences { |roc| roc > end_date }
27
26
  end
28
27
 
28
+ # Break after the first occurrence after now
29
+ def next_occurrence(from)
30
+ found_one = false
31
+ find_occurrences do |roc|
32
+ find = roc > from
33
+ success = found_one
34
+ found_one = find
35
+ success
36
+ end
37
+ end
38
+
29
39
  def first(n)
30
40
  count = 0
31
41
  find_occurrences { |roc| count += 1; count > n }
@@ -45,7 +55,7 @@ module IceCube
45
55
  begin
46
56
  return nil if @end_time && date > @end_time
47
57
  return nil if @rule.until_date && date > @rule.until_date # until check
48
- next unless @rule.in_interval?(date, @start_date)
58
+ next unless @rule.in_interval?(date, @start_date)
49
59
  return nil if yield(date)
50
60
  return RuleOccurrence.new(@rule, @start_date, @end_time, date, @index + 1)
51
61
  end while date = @rule.next_suggestion(date)
@@ -33,25 +33,25 @@ module IceCube
33
33
  end
34
34
 
35
35
  # Convert the schedule to yaml, reverse of Schedule.from_yaml
36
- def to_yaml
36
+ def to_yaml(options = {})
37
37
  hash = to_hash
38
- hash[:start_date] = TimeUtil.serializable_time(hash[:start_date])
39
- hash[:rdates] = hash[:rdates].map { |t| TimeUtil.serializable_time(t) }
40
- hash[:exdates] = hash[:exdates].map { |t| TimeUtil.serializable_time(t) }
41
- hash[:end_time] = TimeUtil.serializable_time(hash[:end_time])
42
- hash.to_yaml
38
+ hash[:start_date] = TimeUtil.serialize_time(hash[:start_date])
39
+ hash[:rdates] = hash[:rdates].map { |t| TimeUtil.serialize_time(t) }
40
+ hash[:exdates] = hash[:exdates].map { |t| TimeUtil.serialize_time(t) }
41
+ hash[:end_time] = TimeUtil.serialize_time(hash[:end_time])
42
+ hash.to_yaml(options)
43
43
  end
44
44
 
45
45
  # Create a schedule from a hash created by instance.to_hash
46
46
  def self.from_hash(hash)
47
47
  options = {}
48
48
  options[:duration] = hash[:duration] if hash.has_key?(:duration)
49
- options[:end_time] = hash[:end_time] if hash.has_key?(:end_time)
50
- schedule = Schedule.new(hash[:start_date], options)
49
+ options[:end_time] = TimeUtil.deserialize_time(hash[:end_time]) if hash.has_key?(:end_time)
50
+ schedule = Schedule.new(TimeUtil.deserialize_time(hash[:start_date]), options)
51
51
  hash[:rrules].each { |rr| schedule.add_recurrence_rule Rule.from_hash(rr) }
52
52
  hash[:exrules].each { |ex| schedule.add_exception_rule Rule.from_hash(ex) }
53
- hash[:rdates].each { |rd| schedule.add_recurrence_date rd }
54
- hash[:exdates].each { |ed| schedule.add_exception_date ed }
53
+ hash[:rdates].each { |rd| schedule.add_recurrence_date TimeUtil.deserialize_time(rd) }
54
+ hash[:exdates].each { |ed| schedule.add_exception_date TimeUtil.deserialize_time(ed) }
55
55
  schedule
56
56
  end
57
57
 
@@ -105,10 +105,10 @@ module IceCube
105
105
  end
106
106
 
107
107
  # Determine whether a given date appears in the times returned by the schedule
108
- # Required activeSupport
109
108
  def occurs_on?(date)
110
- time = date.to_time
111
- occurrences_between(time.beginning_of_day, time.end_of_day).any?
109
+ return active_support_occurs_on?(date) if date.respond_to?(:to_time)
110
+ time_format = @start_date.utc? ? :utc : :local
111
+ self.occurrences_between(Time.send(time_format, date.year, date.month, date.day, 0, 0, 0), Time.send(time_format, date.year, date.month, date.day, 23, 59, 59)).any?
112
112
  end
113
113
 
114
114
  # Return all possible occurrences
@@ -124,6 +124,18 @@ module IceCube
124
124
  find_occurrences { |head| head.upto(end_date) }
125
125
  end
126
126
 
127
+ # Find remaining occurrences
128
+ def remaining_occurrences(from = Time.now)
129
+ raise ArgumentError.new('Schedule must have an end_time to use remaining_occurrences') unless @end_time
130
+ occurrences_between(from, @end_time)
131
+ end
132
+
133
+ # Find next scheduled occurrence
134
+ def next_occurrence(from = Time.now)
135
+ nexts = find_occurrences { |head| head.next_occurrence(from) }
136
+ nexts.empty? ? nil : (nexts.last if nexts.last > from)
137
+ end
138
+
127
139
  # Retrieve the first (n) occurrences of the schedule. May return less than
128
140
  # n results, if the rules end before n results are reached.
129
141
  def first(n = nil)
@@ -165,7 +177,7 @@ module IceCube
165
177
  # adjust to the propert end date
166
178
  end_time = @end_time if @end_time && @end_time < end_time
167
179
  # collect the occurrences
168
- include_dates = SortedSet.new(@rdates)
180
+ include_dates, exclude_dates = SortedSet.new(@rdates), Set.new(@exdates)
169
181
  @rrule_occurrence_heads.each do |rrule_occurrence_head|
170
182
  include_dates.merge(rrule_occurrence_head.between(begin_time, end_time))
171
183
  end
@@ -173,7 +185,7 @@ module IceCube
173
185
  exclude_dates.merge(exrule_occurrence_head.between(begin_time, end_time))
174
186
  end
175
187
  # reject all of the ones outside of the range
176
- include_dates.reject! { |date| (@exdates && @exdates.include?(date)) || date < begin_time || date > end_time }
188
+ include_dates.reject! { |date| exclude_dates.include?(date) || date < begin_time || date > end_time }
177
189
  include_dates.to_a
178
190
  end
179
191
 
@@ -184,7 +196,12 @@ module IceCube
184
196
 
185
197
 
186
198
  private
187
-
199
+
200
+ def active_support_occurs_on?(date)
201
+ time = date.to_time
202
+ occurrences_between(time.beginning_of_day, time.end_of_day).any?
203
+ end
204
+
188
205
  # tell if, from a list of rule_occurrence heads, a certain time is occurring
189
206
  def any_occurring_at?(what, time)
190
207
  return false if @start_time && time < @start_time
@@ -5,19 +5,21 @@ module IceCube
5
5
  LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
6
6
  CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
7
7
 
8
- # this method exists because ActiveSupport will serialize
9
- # TimeWithZone's in collections in UTC time instead of
10
- # their local time. if +time+ is a TimeWithZone, we move
11
- # it to a DateTime
12
- # Note: When converting to datetime, you microseconds get set to 0
13
- def self.serializable_time(time)
14
- if time.respond_to?(:to_datetime)
15
- time.to_datetime
16
- else
8
+ def self.serialize_time(time)
9
+ if time.is_a?(ActiveSupport::TimeWithZone)
10
+ { :time => time, :zone => time.time_zone }
11
+ elsif time.is_a?(Time)
17
12
  time
18
13
  end
19
14
  end
20
15
 
16
+ def self.deserialize_time(time_or_hash)
17
+ return time_or_hash if time_or_hash.is_a?(Time) # for backward-compat
18
+ if time_or_hash.is_a?(Hash)
19
+ time_or_hash[:time].in_time_zone(time_or_hash[:zone])
20
+ end
21
+ end
22
+
21
23
  # TODO can we improve this more?
22
24
  def self.date_in_n_months(date, month_distance)
23
25
 
@@ -3,14 +3,15 @@ module IceCube
3
3
  class Validation
4
4
 
5
5
  NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
6
-
7
- def adjust(goal, date)
6
+ SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }
7
+
8
+ def self.adjust(goal, date)
8
9
  return goal if goal.utc_offset == date.utc_offset
9
10
  goal - goal.utc_offset + date.utc_offset
10
11
  end
11
12
 
12
13
  # influences by ActiveSupport's to_sentence
13
- def sentence(array)
14
+ def self.sentence(array)
14
15
  case array.length
15
16
  when 0 ; ''
16
17
  when 1 ; array[0].to_s
@@ -19,23 +20,25 @@ module IceCube
19
20
  end
20
21
  end
21
22
 
22
- def nice_numbers(array)
23
+ def self.nice_numbers(array)
23
24
  array.sort!
24
- sentence array.map { |d| nice_number(d) }
25
+ self.sentence array.map { |d| nice_number(d) }
25
26
  end
26
27
 
27
28
  private
28
29
 
29
- def nice_number(number)
30
+ def self.nice_number(number)
30
31
  if number == -1
31
32
  'last'
32
33
  elsif number < -1
33
- number.abs.to_s << NUMBER_SUFFIX[number.abs % 10] << ' to last'
34
+ suffix = SPECIAL_SUFFIX.include?(number) ? SPECIAL_SUFFIX[number] : NUMBER_SUFFIX[number.abs % 10]
35
+ number.abs.to_s << suffix << ' to last'
34
36
  else
35
- number.to_s << NUMBER_SUFFIX[number % 10]
37
+ suffix = SPECIAL_SUFFIX.include?(number) ? SPECIAL_SUFFIX[number] : NUMBER_SUFFIX[number.abs % 10]
38
+ number.to_s << suffix
36
39
  end
37
40
  end
38
41
 
39
42
  end
40
43
 
41
- end
44
+ end
@@ -21,12 +21,12 @@ module IceCube
21
21
  days.compact!
22
22
  # go to the closest distance away, the start of that day
23
23
  goal = date + days.min * IceCube::ONE_DAY
24
- adjust(goal, date)
24
+ self.class.adjust(goal, date)
25
25
  end
26
26
 
27
27
  def to_s
28
28
  days_dup = (@days - @rule.validations[:day_of_week].keys if @rule.validations[:day_of_week]) || @days # don't list twice
29
- 'on ' << sentence(days_dup.map { |d| Date::DAYNAMES[d] + 's' }) unless days_dup.empty?
29
+ 'on ' << self.class.sentence(days_dup.map { |d| Date::DAYNAMES[d] + 's' }) unless days_dup.empty?
30
30
  end
31
31
 
32
32
  def to_ical
@@ -36,4 +36,4 @@ module IceCube
36
36
 
37
37
  end
38
38
 
39
- end
39
+ end
@@ -34,11 +34,11 @@ module IceCube
34
34
  return nil if distances.empty?
35
35
  # return the start of the proper day
36
36
  goal = date + distances.min * IceCube::ONE_DAY
37
- adjust(goal, date)
37
+ self.class.adjust(goal, date)
38
38
  end
39
39
 
40
40
  def to_s
41
- 'on the ' << nice_numbers(@days_of_month) << (@days_of_month.size == 1 ? ' day' : ' days') << ' of the month' unless @days_of_month.empty?
41
+ 'on the ' << self.class.nice_numbers(@days_of_month) << (@days_of_month.size == 1 ? ' day' : ' days') << ' of the month' unless @days_of_month.empty?
42
42
  end
43
43
 
44
44
  def to_ical
@@ -26,7 +26,7 @@ module IceCube
26
26
  return nil if !@days_of_week || @days_of_week.empty?
27
27
  goal = date
28
28
  while goal += IceCube::ONE_DAY
29
- return adjust(goal, date) if validate(goal)
29
+ return self.class.adjust(goal, date) if validate(goal)
30
30
  end
31
31
  end
32
32
 
@@ -34,8 +34,8 @@ module IceCube
34
34
  representation = ''
35
35
  representation << 'on the '
36
36
  representation << @days_of_week.map do |day, occ|
37
- nice_numbers(occ) << ' ' << Date::DAYNAMES[day] << (occ.size != 1 ? 's' : '') unless @days_of_week.empty?
38
- end.join(' and the ')
37
+ self.class.nice_numbers(occ) << ' ' << Date::DAYNAMES[day] << (occ.size != 1 ? 's' : '') unless @days_of_week.empty?
38
+ end.join(' when it is the ')
39
39
  representation
40
40
  end
41
41
 
@@ -33,11 +33,11 @@ module IceCube
33
33
  return nil if distances.empty?
34
34
  # return the start of the proper day
35
35
  goal = date + distances.min * IceCube::ONE_DAY
36
- adjust(goal, date)
36
+ self.class.adjust(goal, date)
37
37
  end
38
38
 
39
39
  def to_s
40
- 'on the ' << nice_numbers(@days_of_year) << (@days_of_year.size == 1 ? ' day' : ' days') << ' of the year' unless @days_of_year.empty?
40
+ 'on the ' << self.class.nice_numbers(@days_of_year) << (@days_of_year.size == 1 ? ' day' : ' days') << ' of the year' unless @days_of_year.empty?
41
41
  end
42
42
 
43
43
  def to_ical
@@ -22,11 +22,11 @@ module IceCube
22
22
  # go to the closest distance away, the start of that hour
23
23
  closest_hour = hours.min
24
24
  goal = date + IceCube::ONE_HOUR * closest_hour
25
- adjust(goal, date)
25
+ self.class.adjust(goal, date)
26
26
  end
27
27
 
28
28
  def to_s
29
- 'on the ' << nice_numbers(@hours_of_day) << (@hours_of_day.size == 1 ? ' hour' : ' hours') << ' of the day' unless @hours_of_day.empty?
29
+ 'on the ' << self.class.nice_numbers(@hours_of_day) << (@hours_of_day.size == 1 ? ' hour' : ' hours') << ' of the day' unless @hours_of_day.empty?
30
30
  end
31
31
 
32
32
  def to_ical
@@ -22,11 +22,11 @@ module IceCube
22
22
  # go to the closest distance away, the beginning of that minute
23
23
  closest_minute = minutes.min
24
24
  goal = date + closest_minute * IceCube::ONE_MINUTE
25
- adjust(goal, date)
25
+ self.class.adjust(goal, date)
26
26
  end
27
27
 
28
28
  def to_s
29
- 'on the ' << nice_numbers(@minutes_of_hour) << (@minutes_of_hour.size == 1 ? ' minute' : ' minutes') << ' of the hour' unless @minutes_of_hour.empty?
29
+ 'on the ' << self.class.nice_numbers(@minutes_of_hour) << (@minutes_of_hour.size == 1 ? ' minute' : ' minutes') << ' of the hour' unless @minutes_of_hour.empty?
30
30
  end
31
31
 
32
32
  def to_ical
@@ -22,11 +22,11 @@ module IceCube
22
22
  # go to the closest distance away
23
23
  goal = date
24
24
  months.min.times { goal += TimeUtil.days_in_month(goal) * IceCube::ONE_DAY }
25
- adjust(goal, date)
25
+ self.class.adjust(goal, date)
26
26
  end
27
27
 
28
28
  def to_s
29
- 'in ' << @months_of_year.map { |m| Date::MONTHNAMES[m] }.join(', ') unless @months_of_year.empty?
29
+ 'in ' << self.class.sentence(@months_of_year.map { |m| Date::MONTHNAMES[m] }) unless @months_of_year.empty?
30
30
  end
31
31
 
32
32
  def to_ical
@@ -35,4 +35,4 @@ module IceCube
35
35
 
36
36
  end
37
37
 
38
- end
38
+ end
@@ -22,11 +22,11 @@ module IceCube
22
22
  # go to the closest distance away
23
23
  closest_second = seconds.min
24
24
  goal = date + closest_second
25
- adjust(goal, date)
25
+ self.class.adjust(goal, date)
26
26
  end
27
27
 
28
28
  def to_s
29
- 'on the ' << nice_numbers(@seconds_of_minute) << (@seconds_of_minute.size == 1 ? ' second' : ' seconds') << ' of the minute' unless @seconds_of_minute.empty?
29
+ 'on the ' << self.class.nice_numbers(@seconds_of_minute) << (@seconds_of_minute.size == 1 ? ' second' : ' seconds') << ' of the minute' unless @seconds_of_minute.empty?
30
30
  end
31
31
 
32
32
  def to_ical
@@ -1,5 +1,5 @@
1
1
  module IceCube
2
2
 
3
- VERSION = '0.5.3'
3
+ VERSION = '0.6.0'
4
4
 
5
5
  end
data/lib/ice_cube.rb CHANGED
@@ -37,7 +37,7 @@ module IceCube
37
37
  IceCube::ONE_HOUR = 60 * 60
38
38
  IceCube::ONE_MINUTE = 60
39
39
  IceCube::ONE_SECOND = 1
40
-
40
+
41
41
  ICAL_DAYS = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA']
42
42
  DAYS = { :sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6 }
43
43
  MONTHS = { :january => 1, :february => 2, :march => 3, :april => 4, :may => 5, :june => 6, :july => 7, :august => 8,
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ice_cube
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 3
10
- version: 0.5.3
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Crepezzi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-25 00:00:00 -04:00
18
+ date: 2010-10-18 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency