ice_cube 0.5.9 → 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_occurrence.rb +11 -0
- data/lib/ice_cube/schedule.rb +10 -9
- data/lib/ice_cube/time_util.rb +11 -9
- data/lib/ice_cube/version.rb +1 -1
- metadata +5 -5
@@ -25,6 +25,17 @@ module IceCube
|
|
25
25
|
find_occurrences { |roc| roc > end_date }
|
26
26
|
end
|
27
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
|
+
|
28
39
|
def first(n)
|
29
40
|
count = 0
|
30
41
|
find_occurrences { |roc| count += 1; count > n }
|
data/lib/ice_cube/schedule.rb
CHANGED
@@ -35,10 +35,10 @@ module IceCube
|
|
35
35
|
# Convert the schedule to yaml, reverse of Schedule.from_yaml
|
36
36
|
def to_yaml(options = {})
|
37
37
|
hash = to_hash
|
38
|
-
hash[:start_date] = TimeUtil.
|
39
|
-
hash[:rdates] = hash[:rdates].map { |t| TimeUtil.
|
40
|
-
hash[:exdates] = hash[:exdates].map { |t| TimeUtil.
|
41
|
-
hash[:end_time] = TimeUtil.
|
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
42
|
hash.to_yaml(options)
|
43
43
|
end
|
44
44
|
|
@@ -46,12 +46,12 @@ module IceCube
|
|
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
|
|
@@ -132,7 +132,8 @@ module IceCube
|
|
132
132
|
|
133
133
|
# Find next scheduled occurrence
|
134
134
|
def next_occurrence(from = Time.now)
|
135
|
-
|
135
|
+
nexts = find_occurrences { |head| head.next_occurrence(from) }
|
136
|
+
nexts.empty? ? nil : (nexts.last if nexts.last > from)
|
136
137
|
end
|
137
138
|
|
138
139
|
# Retrieve the first (n) occurrences of the schedule. May return less than
|
data/lib/ice_cube/time_util.rb
CHANGED
@@ -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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
|
data/lib/ice_cube/version.rb
CHANGED
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:
|
4
|
+
hash: 7
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
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-
|
18
|
+
date: 2010-10-18 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|