ice_cube 0.11.0 → 0.12.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 (46) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ice_cube/builders/string_builder.rb +15 -23
  3. data/lib/ice_cube/deprecated.rb +33 -23
  4. data/lib/ice_cube/enumerator.rb +1 -3
  5. data/lib/ice_cube/errors/count_exceeded.rb +1 -1
  6. data/lib/ice_cube/errors/until_exceeded.rb +1 -1
  7. data/lib/ice_cube/flexible_hash.rb +23 -7
  8. data/lib/ice_cube/formatters/string.rb +155 -0
  9. data/lib/ice_cube/hash_input.rb +71 -0
  10. data/lib/ice_cube/occurrence.rb +2 -1
  11. data/lib/ice_cube/parsers/hash_parser.rb +87 -0
  12. data/lib/ice_cube/parsers/yaml_parser.rb +21 -0
  13. data/lib/ice_cube/rule.rb +5 -7
  14. data/lib/ice_cube/rules/daily_rule.rb +1 -1
  15. data/lib/ice_cube/rules/hourly_rule.rb +1 -1
  16. data/lib/ice_cube/rules/minutely_rule.rb +1 -1
  17. data/lib/ice_cube/rules/monthly_rule.rb +1 -1
  18. data/lib/ice_cube/rules/secondly_rule.rb +1 -1
  19. data/lib/ice_cube/rules/yearly_rule.rb +1 -1
  20. data/lib/ice_cube/schedule.rb +76 -64
  21. data/lib/ice_cube/string_helpers.rb +10 -0
  22. data/lib/ice_cube/time_util.rb +24 -8
  23. data/lib/ice_cube/validated_rule.rb +13 -6
  24. data/lib/ice_cube/validations/count.rb +8 -6
  25. data/lib/ice_cube/validations/daily_interval.rb +17 -18
  26. data/lib/ice_cube/validations/day.rb +8 -4
  27. data/lib/ice_cube/validations/day_of_month.rb +12 -10
  28. data/lib/ice_cube/validations/day_of_week.rb +28 -26
  29. data/lib/ice_cube/validations/day_of_year.rb +15 -12
  30. data/lib/ice_cube/validations/hour_of_day.rb +13 -11
  31. data/lib/ice_cube/validations/hourly_interval.rb +18 -21
  32. data/lib/ice_cube/validations/lock.rb +35 -14
  33. data/lib/ice_cube/validations/minute_of_hour.rb +13 -11
  34. data/lib/ice_cube/validations/minutely_interval.rb +16 -19
  35. data/lib/ice_cube/validations/month_of_year.rb +8 -4
  36. data/lib/ice_cube/validations/monthly_interval.rb +19 -17
  37. data/lib/ice_cube/validations/schedule_lock.rb +12 -11
  38. data/lib/ice_cube/validations/second_of_minute.rb +9 -5
  39. data/lib/ice_cube/validations/secondly_interval.rb +13 -16
  40. data/lib/ice_cube/validations/until.rb +16 -12
  41. data/lib/ice_cube/validations/weekly_interval.rb +24 -23
  42. data/lib/ice_cube/validations/yearly_interval.rb +14 -11
  43. data/lib/ice_cube/version.rb +1 -1
  44. data/lib/ice_cube.rb +13 -11
  45. data/spec/spec_helper.rb +2 -0
  46. metadata +24 -34
@@ -17,12 +17,6 @@ module IceCube
17
17
 
18
18
  attr_reader :day
19
19
 
20
- StringBuilder.register_formatter(:day_of_year) do |entries|
21
- str = "on the #{StringBuilder.sentence(entries)} "
22
- str << (entries.size == 1 ? 'day of the year' : 'days of the year')
23
- str
24
- end
25
-
26
20
  def initialize(day)
27
21
  @day = day
28
22
  end
@@ -31,6 +25,17 @@ module IceCube
31
25
  :day
32
26
  end
33
27
 
28
+ def dst_adjust?
29
+ true
30
+ end
31
+
32
+ def validate(step_time, schedule)
33
+ days_in_year = TimeUtil.days_in_year(step_time)
34
+ yday = day < 0 ? day + days_in_year : day
35
+ offset = yday - step_time.yday
36
+ offset >= 0 ? offset : offset + days_in_year
37
+ end
38
+
34
39
  def build_s(builder)
35
40
  builder.piece(:day_of_year) << StringBuilder.nice_number(day)
36
41
  end
@@ -43,12 +48,10 @@ module IceCube
43
48
  builder['BYYEARDAY'] << day
44
49
  end
45
50
 
46
- def validate(time, schedule)
47
- days_in_year = TimeUtil.days_in_year(time)
48
- the_day = day < 0 ? day + days_in_year : day
49
- # compute the diff
50
- diff = the_day - time.yday
51
- diff >= 0 ? diff : diff + days_in_year
51
+ StringBuilder.register_formatter(:day_of_year) do |entries|
52
+ str = "on the #{StringBuilder.sentence(entries)} "
53
+ str << (entries.size == 1 ? 'day of the year' : 'days of the year')
54
+ str
52
55
  end
53
56
 
54
57
  end
@@ -2,8 +2,6 @@ module IceCube
2
2
 
3
3
  module Validations::HourOfDay
4
4
 
5
- include Validations::Lock
6
-
7
5
  # Add hour of day validations
8
6
  def hour_of_day(*hours)
9
7
  hours.flatten.each do |hour|
@@ -20,11 +18,6 @@ module IceCube
20
18
 
21
19
  include Validations::Lock
22
20
 
23
- StringBuilder.register_formatter(:hour_of_day) do |segments|
24
- str = "on the #{StringBuilder.sentence(segments)} "
25
- str << (segments.size == 1 ? 'hour of the day' : 'hours of the day')
26
- end
27
-
28
21
  attr_reader :hour
29
22
  alias :value :hour
30
23
 
@@ -32,14 +25,18 @@ module IceCube
32
25
  @hour = hour
33
26
  end
34
27
 
35
- def build_s(builder)
36
- builder.piece(:hour_of_day) << StringBuilder.nice_number(hour)
37
- end
38
-
39
28
  def type
40
29
  :hour
41
30
  end
42
31
 
32
+ def dst_adjust?
33
+ true
34
+ end
35
+
36
+ def build_s(builder)
37
+ builder.piece(:hour_of_day) << StringBuilder.nice_number(hour)
38
+ end
39
+
43
40
  def build_hash(builder)
44
41
  builder.validations_array(:hour_of_day) << hour
45
42
  end
@@ -48,6 +45,11 @@ module IceCube
48
45
  builder['BYHOUR'] << hour
49
46
  end
50
47
 
48
+ StringBuilder.register_formatter(:hour_of_day) do |segments|
49
+ str = "on the #{StringBuilder.sentence(segments)} "
50
+ str << (segments.size == 1 ? 'hour of the day' : 'hours of the day')
51
+ end
52
+
51
53
  end
52
54
 
53
55
  end
@@ -13,10 +13,27 @@ module IceCube
13
13
 
14
14
  attr_reader :interval
15
15
 
16
+ def initialize(interval)
17
+ @interval = interval
18
+ end
19
+
16
20
  def type
17
21
  :hour
18
22
  end
19
23
 
24
+ def dst_adjust?
25
+ false
26
+ end
27
+
28
+ def validate(step_time, schedule)
29
+ t0, t1 = schedule.start_time.to_i, step_time.to_i
30
+ sec = (t1 - t1 % ONE_HOUR) -
31
+ (t0 - t0 % ONE_HOUR)
32
+ hours = sec / ONE_HOUR
33
+ offset = (hours % interval).nonzero?
34
+ interval - offset if offset
35
+ end
36
+
20
37
  def build_s(builder)
21
38
  builder.base = interval == 1 ? 'Hourly' : "Every #{interval} hours"
22
39
  end
@@ -27,27 +44,7 @@ module IceCube
27
44
 
28
45
  def build_ical(builder)
29
46
  builder['FREQ'] << 'HOURLY'
30
- unless interval == 1
31
- builder['INTERVAL'] << interval
32
- end
33
- end
34
-
35
- def initialize(interval)
36
- @interval = interval
37
- end
38
-
39
- def dst_adjust?
40
- false
41
- end
42
-
43
- def validate(time, schedule)
44
- start_time = schedule.start_time
45
- sec = (time.to_i - time.to_i % ONE_HOUR) -
46
- (start_time.to_i - start_time.to_i % ONE_HOUR)
47
- hours = sec / ONE_HOUR
48
- unless hours % interval == 0
49
- interval - (hours % interval)
50
- end
47
+ builder['INTERVAL'] << interval unless interval == 1
51
48
  end
52
49
 
53
50
  end
@@ -1,34 +1,49 @@
1
1
  module IceCube
2
2
 
3
- # A validation mixin that will lock the +type field to
4
- # +value or +schedule.start_time.send(type) if value is nil
5
-
3
+ # This validation mixin is used by the various "fixed-time" (e.g. day,
4
+ # day_of_month, hour_of_day) Validation and ScheduleLock::Validation modules.
5
+ # It is not a standalone rule validation like the others.
6
+ #
7
+ # Given the including Validation's defined +type+ field, it will lock
8
+ # to the specified +value+ or else the corresponding time unit from the
9
+ # schedule's start_time
10
+ #
6
11
  module Validations::Lock
7
12
 
8
- INTERVALS = {:min => 60, :sec => 60, :month => 12, :wday => 7}
13
+ INTERVALS = {:min => 60, :sec => 60, :hour => 24, :month => 12, :wday => 7}
9
14
 
10
15
  def validate(time, schedule)
11
- return send(:"validate_#{type}_lock", time, schedule) unless INTERVALS[type]
12
- start = value || schedule.start_time.send(type)
13
- start = INTERVALS[type] + start if start < 0 # handle negative values
14
- start >= time.send(type) ? start - time.send(type) : INTERVALS[type] - time.send(type) + start
16
+ case type
17
+ when :day then validate_day_lock(time, schedule)
18
+ when :hour then validate_hour_lock(time, schedule)
19
+ else validate_interval_lock(time, schedule)
20
+ end
15
21
  end
16
22
 
17
23
  private
18
24
 
25
+ # Validate if the current time unit matches the same unit from the schedule
26
+ # start time, returning the difference to the interval
27
+ #
28
+ def validate_interval_lock(time, schedule)
29
+ t0 = starting_unit(schedule.start_time)
30
+ t1 = time.send(type)
31
+ t0 >= t1 ? t0 - t1 : INTERVALS[type] - t1 + t0
32
+ end
33
+
19
34
  # Lock the hour if explicitly set by hour_of_day, but allow for the nearest
20
35
  # hour during DST start to keep the correct interval.
21
36
  #
22
37
  def validate_hour_lock(time, schedule)
23
- hour = value || schedule.start_time.send(type)
24
- hour = 24 + hour if hour < 0
25
- if hour >= time.hour
26
- hour - time.hour
38
+ h0 = starting_unit(schedule.start_time)
39
+ h1 = time.hour
40
+ if h0 >= h1
41
+ h0 - h1
27
42
  else
28
43
  if dst_offset = TimeUtil.dst_change(time)
29
- hour - time.hour + dst_offset
44
+ h0 - h1 + dst_offset
30
45
  else
31
- 24 - time.hour + hour
46
+ 24 - h1 + h0
32
47
  end
33
48
  end
34
49
  end
@@ -69,6 +84,12 @@ module IceCube
69
84
  sleeps >= 0 ? sleeps : until_next_month
70
85
  end
71
86
 
87
+ def starting_unit(start_time)
88
+ start = value || start_time.send(type)
89
+ start += INTERVALS[type] while start < 0
90
+ start
91
+ end
92
+
72
93
  end
73
94
 
74
95
  end
@@ -2,8 +2,6 @@ module IceCube
2
2
 
3
3
  module Validations::MinuteOfHour
4
4
 
5
- include Validations::Lock
6
-
7
5
  def minute_of_hour(*minutes)
8
6
  minutes.flatten.each do |minute|
9
7
  unless minute.is_a?(Fixnum)
@@ -19,11 +17,6 @@ module IceCube
19
17
 
20
18
  include Validations::Lock
21
19
 
22
- StringBuilder.register_formatter(:minute_of_hour) do |segments|
23
- str = "on the #{StringBuilder.sentence(segments)} "
24
- str << (segments.size == 1 ? 'minute of the hour' : 'minutes of the hour')
25
- end
26
-
27
20
  attr_reader :minute
28
21
  alias :value :minute
29
22
 
@@ -31,14 +24,18 @@ module IceCube
31
24
  @minute = minute
32
25
  end
33
26
 
34
- def build_s(builder)
35
- builder.piece(:minute_of_hour) << StringBuilder.nice_number(minute)
36
- end
37
-
38
27
  def type
39
28
  :min
40
29
  end
41
30
 
31
+ def dst_adjust?
32
+ false
33
+ end
34
+
35
+ def build_s(builder)
36
+ builder.piece(:minute_of_hour) << StringBuilder.nice_number(minute)
37
+ end
38
+
42
39
  def build_hash(builder)
43
40
  builder.validations_array(:minute_of_hour) << minute
44
41
  end
@@ -47,6 +44,11 @@ module IceCube
47
44
  builder['BYMINUTE'] << minute
48
45
  end
49
46
 
47
+ StringBuilder.register_formatter(:minute_of_hour) do |segments|
48
+ str = "on the #{StringBuilder.sentence(segments)} "
49
+ str << (segments.size == 1 ? 'minute of the hour' : 'minutes of the hour')
50
+ end
51
+
50
52
  end
51
53
 
52
54
  end
@@ -13,6 +13,10 @@ module IceCube
13
13
 
14
14
  attr_reader :interval
15
15
 
16
+ def initialize(interval)
17
+ @interval = interval
18
+ end
19
+
16
20
  def type
17
21
  :min
18
22
  end
@@ -21,33 +25,26 @@ module IceCube
21
25
  false
22
26
  end
23
27
 
24
- def build_s(builder)
25
- builder.base = interval == 1 ? 'Minutely' : "Every #{interval} minutes"
28
+ def validate(step_time, schedule)
29
+ t0, t1 = schedule.start_time.to_i, step_time.to_i
30
+ sec = (t1 - t1 % ONE_MINUTE) -
31
+ (t0 - t0 % ONE_MINUTE)
32
+ minutes = sec / ONE_MINUTE
33
+ offset = (minutes % interval).nonzero?
34
+ interval - offset if offset
26
35
  end
27
36
 
28
- def build_ical(builder)
29
- builder['FREQ'] << 'MINUTELY'
30
- unless interval == 1
31
- builder['INTERVAL'] << interval
32
- end
37
+ def build_s(builder)
38
+ builder.base = interval == 1 ? 'Minutely' : "Every #{interval} minutes"
33
39
  end
34
40
 
35
41
  def build_hash(builder)
36
42
  builder[:interval] = interval
37
43
  end
38
44
 
39
- def initialize(interval)
40
- @interval = interval
41
- end
42
-
43
- def validate(time, schedule)
44
- start_time = schedule.start_time
45
- sec = (time.to_i - time.to_i % ONE_MINUTE) -
46
- (start_time.to_i - start_time.to_i % ONE_MINUTE)
47
- minutes = sec / ONE_MINUTE
48
- unless minutes % interval == 0
49
- interval - (minutes % interval)
50
- end
45
+ def build_ical(builder)
46
+ builder['FREQ'] << 'MINUTELY'
47
+ builder['INTERVAL'] << interval unless interval == 1
51
48
  end
52
49
 
53
50
  end
@@ -25,6 +25,14 @@ module IceCube
25
25
  @month = month
26
26
  end
27
27
 
28
+ def type
29
+ :month
30
+ end
31
+
32
+ def dst_adjust?
33
+ true
34
+ end
35
+
28
36
  def build_s(builder)
29
37
  builder.piece(:month_of_year) << Date::MONTHNAMES[month]
30
38
  end
@@ -37,10 +45,6 @@ module IceCube
37
45
  builder['BYMONTH'] << month
38
46
  end
39
47
 
40
- def type
41
- :month
42
- end
43
-
44
48
  StringBuilder.register_formatter(:month_of_year) do |segments|
45
49
  "in #{StringBuilder.sentence(segments)}"
46
50
  end
@@ -13,35 +13,37 @@ module IceCube
13
13
 
14
14
  attr_reader :interval
15
15
 
16
+ def initialize(interval)
17
+ @interval = interval
18
+ end
19
+
16
20
  def type
17
21
  :month
18
22
  end
19
23
 
20
- def build_s(builder)
21
- builder.base = interval == 1 ? 'Monthly' : "Every #{interval} months"
24
+ def dst_adjust?
25
+ true
22
26
  end
23
27
 
24
- def build_ical(builder)
25
- builder['FREQ'] << 'MONTHLY'
26
- unless interval == 1
27
- builder['INTERVAL'] << interval
28
- end
28
+ def validate(step_time, schedule)
29
+ t0, t1 = schedule.start_time, step_time
30
+ months = (t1.month - t0.month) +
31
+ (t1.year - t0.year) * 12
32
+ offset = (months % interval).nonzero?
33
+ interval - offset if offset
29
34
  end
30
35
 
31
- def build_hash(builder)
32
- builder[:interval] = interval
36
+ def build_s(builder)
37
+ builder.base = interval == 1 ? 'Monthly' : "Every #{interval} months"
33
38
  end
34
39
 
35
- def initialize(interval)
36
- @interval = interval
40
+ def build_hash(builder)
41
+ builder[:interval] = interval
37
42
  end
38
43
 
39
- def validate(time, schedule)
40
- start_time = schedule.start_time
41
- months_to_start = (time.month - start_time.month) + (time.year - start_time.year) * 12
42
- unless months_to_start % interval == 0
43
- interval - (months_to_start % interval)
44
- end
44
+ def build_ical(builder)
45
+ builder['FREQ'] << 'MONTHLY'
46
+ builder['INTERVAL'] << interval unless interval == 1
45
47
  end
46
48
 
47
49
  end
@@ -2,16 +2,16 @@ module IceCube
2
2
 
3
3
  module Validations::ScheduleLock
4
4
 
5
- # Lock the given times down the schedule's start_time for that position
5
+ # Lock the given time units to the units from schedule's +start_time+
6
6
  # These locks are all clobberable by other rules of the same #type
7
- # using clobber_base_validation
7
+ # using +clobber_base_validation+
8
+ #
8
9
  def schedule_lock(*types)
9
10
  types.each do |type|
10
11
  validations_for(:"base_#{type}") << Validation.new(type)
11
12
  end
12
13
  end
13
14
 
14
- # A validation used for locking time into a certain value
15
15
  class Validation
16
16
 
17
17
  include Validations::Lock
@@ -22,24 +22,25 @@ module IceCube
22
22
  @type = type
23
23
  end
24
24
 
25
- # no -op
26
- def build_s(builder)
25
+ def dst_adjust?
26
+ case @type
27
+ when :sec, :min then false
28
+ else true
29
+ end
27
30
  end
28
31
 
29
32
  # no -op
30
- def build_ical(builder)
33
+ def build_s(builder)
31
34
  end
32
35
 
33
36
  # no -op
34
37
  def build_hash(builder)
35
38
  end
36
39
 
37
- def dst_adjust?
38
- case @type
39
- when :sec, :min then false
40
- else true
41
- end
40
+ # no -op
41
+ def build_ical(builder)
42
42
  end
43
+
43
44
  end
44
45
 
45
46
  end
@@ -17,11 +17,6 @@ module IceCube
17
17
 
18
18
  include Validations::Lock
19
19
 
20
- StringBuilder.register_formatter(:second_of_minute) do |segments|
21
- str = "on the #{StringBuilder.sentence(segments)} "
22
- str << (segments.size == 1 ? 'second of the minute' : 'seconds of the minute')
23
- end
24
-
25
20
  attr_reader :second
26
21
  alias :value :second
27
22
 
@@ -33,6 +28,10 @@ module IceCube
33
28
  :sec
34
29
  end
35
30
 
31
+ def dst_adjust?
32
+ false
33
+ end
34
+
36
35
  def build_s(builder)
37
36
  builder.piece(:second_of_minute) << StringBuilder.nice_number(second)
38
37
  end
@@ -45,6 +44,11 @@ module IceCube
45
44
  builder['BYSECOND'] << second
46
45
  end
47
46
 
47
+ StringBuilder.register_formatter(:second_of_minute) do |segments|
48
+ str = "on the #{StringBuilder.sentence(segments)} "
49
+ str << (segments.size == 1 ? 'second of the minute' : 'seconds of the minute')
50
+ end
51
+
48
52
  end
49
53
 
50
54
  end
@@ -13,6 +13,10 @@ module IceCube
13
13
 
14
14
  attr_reader :interval
15
15
 
16
+ def initialize(interval)
17
+ @interval = interval
18
+ end
19
+
16
20
  def type
17
21
  :sec
18
22
  end
@@ -21,30 +25,23 @@ module IceCube
21
25
  false
22
26
  end
23
27
 
24
- def build_s(builder)
25
- builder.base = interval == 1 ? 'Secondly' : "Every #{interval} seconds"
28
+ def validate(step_time, schedule)
29
+ seconds = step_time.to_i - schedule.start_time.to_i
30
+ offset = (seconds % interval).nonzero?
31
+ interval - offset if offset
26
32
  end
27
33
 
28
- def build_ical(builder)
29
- builder['FREQ'] << 'SECONDLY'
30
- unless interval == 1
31
- builder['INTERVAL'] << interval
32
- end
34
+ def build_s(builder)
35
+ builder.base = interval == 1 ? 'Secondly' : "Every #{interval} seconds"
33
36
  end
34
37
 
35
38
  def build_hash(builder)
36
39
  builder[:interval] = interval
37
40
  end
38
41
 
39
- def initialize(interval)
40
- @interval = interval
41
- end
42
-
43
- def validate(time, schedule)
44
- seconds = time.to_i - schedule.start_time.to_i
45
- unless seconds % interval == 0
46
- interval - (seconds % interval)
47
- end
42
+ def build_ical(builder)
43
+ builder['FREQ'] << 'SECONDLY'
44
+ builder['INTERVAL'] << interval unless interval == 1
48
45
  end
49
46
 
50
47
  end
@@ -2,9 +2,9 @@ module IceCube
2
2
 
3
3
  module Validations::Until
4
4
 
5
- extend ::Deprecated
5
+ extend Deprecated
6
6
 
7
- # accessor
7
+ # Value reader for limit
8
8
  def until_time
9
9
  @until
10
10
  end
@@ -21,28 +21,32 @@ module IceCube
21
21
 
22
22
  attr_reader :time
23
23
 
24
- def type
25
- :dealbreaker
26
- end
27
-
28
24
  def initialize(time)
29
25
  @time = time
30
26
  end
31
27
 
32
- def build_ical(builder)
33
- builder['UNTIL'] << IcalBuilder.ical_utc_format(time)
28
+ def type
29
+ :limit
34
30
  end
35
31
 
36
- def build_hash(builder)
37
- builder[:until] = TimeUtil.serialize_time(time)
32
+ def dst_adjust?
33
+ false
34
+ end
35
+
36
+ def validate(step_time, schedule)
37
+ raise UntilExceeded if step_time > time
38
38
  end
39
39
 
40
40
  def build_s(builder)
41
41
  builder.piece(:until) << "until #{time.strftime(IceCube.to_s_time_format)}"
42
42
  end
43
43
 
44
- def validate(t, schedule)
45
- raise UntilExceeded if t > time
44
+ def build_hash(builder)
45
+ builder[:until] = TimeUtil.serialize_time(time)
46
+ end
47
+
48
+ def build_ical(builder)
49
+ builder['UNTIL'] << IcalBuilder.ical_utc_format(time)
46
50
  end
47
51
 
48
52
  end