ice_cube 0.3.9 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,6 +30,9 @@ module IceCube
30
30
  autoload :MonthOfYearValidation, 'ice_cube/validations/month_of_year'
31
31
  autoload :SecondOfMinuteValidation, 'ice_cube/validations/second_of_minute'
32
32
 
33
+ # if you're reading this code, you've just been iced
34
+ # http://brosicingbros.com/
35
+
33
36
  VERSION = '0.2.3'
34
37
 
35
38
  IceCube::ONE_DAY = 24 * 60 * 60
@@ -44,4 +47,4 @@ module IceCube
44
47
 
45
48
  include TimeUtil
46
49
 
47
- end
50
+ end
@@ -14,7 +14,7 @@ module IceCube
14
14
  end
15
15
 
16
16
  def all_occurrences
17
- raise ArgumentError.new("Rule must specify either an until date or a count to use 'all_occurrences'") unless @rule.occurrence_count || @rule.until_date
17
+ raise ArgumentError.new("Rule must specify either an until date or a count to use 'all_occurrences'") unless @rule.occurrence_count || @rule.until_date || @end_time
18
18
  find_occurrences { |roc| false }
19
19
  end
20
20
 
@@ -47,15 +47,16 @@ module IceCube
47
47
  end
48
48
  #walk through all of the successive dates, looking for the next occurrence (interval-valid), then return it.
49
49
  begin
50
+ return nil if @end_time && date > @end_time
50
51
  return nil if @rule.until_date && date > @rule.until_date # until check
51
- return RuleOccurrence.new(@rule, @start_date, date, @index + 1) if @rule.in_interval?(date, @start_date)
52
+ return RuleOccurrence.new(@rule, @start_date, @end_time, date, @index + 1) if @rule.in_interval?(date, @start_date)
52
53
  end while date = @rule.next_suggestion(date)
53
54
  end
54
55
 
55
56
  attr_reader :rule
56
57
 
57
58
  private
58
-
59
+
59
60
  def find_occurrences
60
61
  include_dates = []
61
62
  roc = self
@@ -68,11 +69,12 @@ module IceCube
68
69
  include_dates
69
70
  end
70
71
 
71
- def initialize(rule, start_date, date = nil, index = 0)
72
+ def initialize(rule, start_date, end_time, date = nil, index = 0)
72
73
  #set some variables
73
74
  @rule = rule
74
- @date = date
75
75
  @start_date = start_date
76
+ @end_time = end_time
77
+ @date = date
76
78
  @index = index
77
79
  end
78
80
 
@@ -2,11 +2,13 @@ module IceCube
2
2
 
3
3
  class DailyRule < Rule
4
4
 
5
+ # TODO repair
5
6
  # Determine whether this rule occurs on a give date.
6
7
  def in_interval?(date, start_date)
7
8
  #make sure we're in a proper interval
8
9
  day_count = ((date - start_date) / IceCube::ONE_DAY).to_i
9
10
  day_count % @interval == 0
11
+ true
10
12
  end
11
13
 
12
14
  def to_ical
@@ -7,6 +7,7 @@ module IceCube
7
7
  # and we're in a valid day of the week.
8
8
  def in_interval?(date, start_date)
9
9
  #make sure we're in the right interval
10
+ date = adjust(date, start_date)
10
11
  week_of_year = Date.civil(date.year, date.month, date.day).cweek
11
12
  week_of_year % @interval == 0
12
13
  end
@@ -2,7 +2,8 @@ module IceCube
2
2
 
3
3
  class Schedule
4
4
 
5
- attr_reader :rdates, :exdates, :start_date, :duration
5
+ attr_reader :rdates, :exdates, :start_date, :duration, :end_time
6
+ alias :start_time :start_date
6
7
 
7
8
  def initialize(start_date, options = {})
8
9
  @rrule_occurrence_heads = []
@@ -12,6 +13,8 @@ module IceCube
12
13
  @start_date = start_date
13
14
  raise ArgumentError.new('Duration cannot be negative') if options[:duration] && options[:duration] < 0
14
15
  @duration = options[:duration]
16
+ raise ArgumentError.new('Start time must be before end time') if options[:end_time] && options[:end_time] < @start_date
17
+ @end_time = options[:end_time]
15
18
  end
16
19
 
17
20
  # Convert the schedule to a hash, reverse of Schedule.from_hash
@@ -23,6 +26,7 @@ module IceCube
23
26
  hash[:rdates] = @rdates
24
27
  hash[:exdates] = @exdates
25
28
  hash[:duration] = @duration
29
+ hash[:end_time] = @end_time
26
30
  hash
27
31
  end
28
32
 
@@ -32,6 +36,7 @@ module IceCube
32
36
  hash[:start_date] = TimeUtil.serializable_time(hash[:start_date])
33
37
  hash[:rdates] = hash[:rdates].map { |t| TimeUtil.serializable_time(t) }
34
38
  hash[:exdates] = hash[:exdates].map { |t| TimeUtil.serializable_time(t) }
39
+ hash[:end_time] = TimeUtil.serializable_time(hash[:end_time])
35
40
  hash.to_yaml
36
41
  end
37
42
 
@@ -39,7 +44,7 @@ module IceCube
39
44
  def self.from_hash(hash)
40
45
  options = {}
41
46
  options[:duration] = hash[:duration] if hash.has_key?(:duration)
42
-
47
+ options[:end_time] = hash[:end_time] if hash.has_key?(:end_time)
43
48
  schedule = Schedule.new(hash[:start_date], options)
44
49
  hash[:rrules].each { |rr| schedule.add_recurrence_rule Rule.from_hash(rr) }
45
50
  hash[:exrules].each { |ex| schedule.add_exception_rule Rule.from_hash(ex) }
@@ -77,6 +82,9 @@ module IceCube
77
82
  representation << SEPARATOR unless representation.empty?
78
83
  representation << @exdates.uniq.sort.map { |d| 'not on ' << d.strftime(TIME_FORMAT) }.join(SEPARATOR)
79
84
  end
85
+ if @end_time
86
+ representation << "until #{end_time.strftime(TIME_FORMAT)}"
87
+ end
80
88
  representation
81
89
  end
82
90
 
@@ -89,14 +97,16 @@ module IceCube
89
97
 
90
98
  # Determine whether a given time adheres to the ruleset of this schedule.
91
99
  def occurs_at?(date)
100
+ return false if @end_time && date > @end_time
92
101
  dates = occurrences(date)
93
102
  dates.last == date
94
103
  end
95
104
 
96
105
  # Determine whether a given date appears in the times returned by the schedule
97
106
  # Required activeSupport
98
- def occurs_on?(date)
107
+ def occurs_on?(date)
99
108
  time = date.to_time
109
+ return false if @end_time && time > @end_time
100
110
  occurrences_between(time.beginning_of_day, time.end_of_day).any?
101
111
  end
102
112
 
@@ -109,6 +119,7 @@ module IceCube
109
119
 
110
120
  # Find all occurrences until a certain date
111
121
  def occurrences(end_date)
122
+ end_date = @end_time if @end_time && @end_time < end_date
112
123
  find_occurrences { |head| head.upto(end_date) }
113
124
  end
114
125
 
@@ -122,7 +133,7 @@ module IceCube
122
133
  # Add a rule of any type as an recurrence in this schedule
123
134
  def add_recurrence_rule(rule)
124
135
  raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
125
- @rrule_occurrence_heads << RuleOccurrence.new(rule, @start_date)
136
+ @rrule_occurrence_heads << RuleOccurrence.new(rule, @start_date, @end_time)
126
137
  end
127
138
 
128
139
  def rrules
@@ -132,7 +143,7 @@ module IceCube
132
143
  # Add a rule of any type as an exception to this schedule
133
144
  def add_exception_rule(rule)
134
145
  raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
135
- @exrule_occurrence_heads << RuleOccurrence.new(rule, @start_date)
146
+ @exrule_occurrence_heads << RuleOccurrence.new(rule, @start_date, @end_time)
136
147
  end
137
148
 
138
149
  def exrules
@@ -150,6 +161,9 @@ module IceCube
150
161
  end
151
162
 
152
163
  def occurrences_between(begin_time, end_time)
164
+ # adjust to the propert end date
165
+ end_time = @end_time if @end_time && @end_time < end_time
166
+ # collect the occurrences
153
167
  exclude_dates, include_dates = Set.new(@exdates), SortedSet.new(@rdates)
154
168
  @rrule_occurrence_heads.each do |rrule_occurrence_head|
155
169
  include_dates.merge(rrule_occurrence_head.between(begin_time, end_time))
@@ -166,7 +180,7 @@ module IceCube
166
180
 
167
181
  # tell if, from a list of rule_occurrence heads, a certain time is occurring
168
182
  def any_occurring_at?(what, time)
169
- return false if @start_time && itime < @start_time
183
+ return false if @start_time && time < @start_time
170
184
  what.any? do |occurrence_head|
171
185
  # time is less than now and duration is less than that distance
172
186
  possibilities = occurrence_head.between(@start_date, time)
@@ -1,5 +1,5 @@
1
1
  module IceCube
2
2
 
3
- VERSION = "0.3.9"
3
+ VERSION = "0.4"
4
4
 
5
5
  end
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 9
9
- version: 0.3.9
7
+ - 4
8
+ version: "0.4"
10
9
  platform: ruby
11
10
  authors:
12
11
  - John Crepezzi
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2010-06-01 00:00:00 -04:00
16
+ date: 2010-06-25 00:00:00 -04:00
18
17
  default_executable:
19
18
  dependencies:
20
19
  - !ruby/object:Gem::Dependency