montrose 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a88cd103fc68290cf832c8e41ec2e8a1ca4d4a87
4
- data.tar.gz: c74c51dad1844316155c1b138b0b34f548d27dfd
3
+ metadata.gz: 860d9ff5e15a0ab0bf99a0882c367d79629ebc8f
4
+ data.tar.gz: 4366bbc4218a7f42f53f2b71e4cf42a12c2b53b4
5
5
  SHA512:
6
- metadata.gz: 3d186764038aaeb902fb809225a5e26779831e55a3ed0cc11998a6395821c9ab4e34af9d0152f43558f660c39d02ad9e2eff82630e24d56986e1958d0ff51de6
7
- data.tar.gz: 4c939c9c4e6a416f20fb24b718d6fab5026b17650f72e5769b03abaa40ecc10d002a6a8210cd0ec6ae44c832d41020a222cf5e248597ede1754d830e8587d92b
6
+ metadata.gz: a970bd59b217242a61e1c6dd485b8577537c60b0ed0f56dcaf2d274f52ec1f781ba239f8269a11eb3ff8df55c8dacb6a606e223883e3c2977ffb44e354cbbe38
7
+ data.tar.gz: 04c563a8c68abd4480232873c9629b802fccfae66b25b477bbb82ed19ae855bb3665ecef2914497c5ec6f2579ef43cc9cc7c7875bb88060ff5e05ee74171e557
@@ -1,3 +1,10 @@
1
+ ### 0.4.0 - (2016-04-20)
2
+
3
+ * enhancements
4
+ * Respect configured time zone by using `Time.current` from `ActiveSupport`
5
+ * Adds `Montrose::Recurrence#to_json` method
6
+ * Additional tests for utils methods (by @AlexWheeler)
7
+
1
8
  ### 0.3.0 - (2016-02-19)
2
9
 
3
10
  * enhancements
data/README.md CHANGED
@@ -29,10 +29,11 @@ Or install it yourself as:
29
29
 
30
30
  ## Why
31
31
 
32
- Dealing with recurring events is hard. `Montrose` provides a simple interface for specifying and enumerating recurring events as `Time` objects.
32
+ Dealing with recurring events is hard. `Montrose` provides a simple interface for specifying and enumerating recurring events as `Time` objects for Ruby applications.
33
33
 
34
34
  More specifically, this project intends to:
35
35
 
36
+ * model recurring events in Ruby
36
37
  * embrace Ruby idioms
37
38
  * support Ruby 2.1+
38
39
  * be reasonably performant
@@ -42,11 +43,11 @@ More specifically, this project intends to:
42
43
  What `Montrose` doesn't do:
43
44
 
44
45
  * support all calendaring use cases under the sun
45
- * schedule recurring jobs for you. See instead [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler), [sidekiq-cron](https://github.com/ondrejbartas/sidekiq-cron), [sidetiq](https://github.com/tobiassvn/sidetiq), [whenever](https://github.com/javan/whenever)
46
+ * schedule recurring jobs for your Rails app. Use one of these instead: [cron](https://en.wikipedia.org/wiki/Cron), [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler), [sidekiq-cron](https://github.com/ondrejbartas/sidekiq-cron), [sidetiq](https://github.com/tobiassvn/sidetiq), [whenever](https://github.com/javan/whenever)
46
47
 
47
48
  ## Concepts
48
49
 
49
- Montrose allows you to easily create "recurrence" objects through chaining:
50
+ Montrose allows you to easily create "recurrence objects" through chaining:
50
51
 
51
52
  ```ruby
52
53
  # Every Monday at 10:30am
@@ -181,8 +182,8 @@ Montrose.daily(interval: 2)
181
182
  Montrose.every(10.days, total: 5)
182
183
 
183
184
  # everyday in January for 3 years
184
- starts = Time.now.beginning_of_year
185
- ends = Time.now.end_of_year + 2.years
185
+ starts = Time.current.beginning_of_year
186
+ ends = Time.current.end_of_year + 2.years
186
187
  Montrose.daily(month: :january, between: starts...ends)
187
188
 
188
189
  # weekly for 10 occurrences
@@ -356,7 +357,7 @@ Montrose.daily(except: [Date.today, "2017-01-31"])
356
357
  # Chaining
357
358
  Montrose.weekly.starting(3.weeks.from_now).on(:friday)
358
359
  Montrose.every(:day).at("4:05pm")
359
- Montrose.yearly.between(Time.now..10.years.from_now)
360
+ Montrose.yearly.between(Time.current..10.years.from_now)
360
361
 
361
362
  # Enumerating events
362
363
  r = Montrose.every(:month, mday: 31, until: "January 1, 2017")
@@ -6,11 +6,6 @@ module Montrose
6
6
  @default_until = nil
7
7
  @default_every = nil
8
8
 
9
- MAX_HOURS_IN_DAY = 24
10
- MAX_DAYS_IN_YEAR = 366
11
- MAX_WEEKS_IN_YEAR = 53
12
- MAX_DAYS_IN_MONTH = 31
13
-
14
9
  class << self
15
10
  def new(options = {})
16
11
  return options if options.is_a?(self)
@@ -36,7 +31,7 @@ module Montrose
36
31
  def default_until
37
32
  case @default_until
38
33
  when String
39
- Time.parse(@default_until)
34
+ ::Montrose::Utils.parse_time(@default_until)
40
35
  when Proc
41
36
  @default_until.call
42
37
  else
@@ -51,11 +46,11 @@ module Montrose
51
46
  def default_starts
52
47
  case @default_starts
53
48
  when String
54
- Time.parse(@default_starts)
49
+ ::Montrose::Utils.parse_time(@default_starts)
55
50
  when Proc
56
51
  @default_starts.call
57
52
  when nil
58
- Time.now
53
+ ::Montrose::Utils.current_time
59
54
  else
60
55
  @default_starts
61
56
  end
@@ -261,19 +256,19 @@ module Montrose
261
256
  end
262
257
 
263
258
  def assert_hour(hour)
264
- assert_range_includes(1..MAX_HOURS_IN_DAY, hour)
259
+ assert_range_includes(1..::Montrose::Utils::MAX_HOURS_IN_DAY, hour)
265
260
  end
266
261
 
267
262
  def assert_mday(mday)
268
- assert_range_includes(1..MAX_DAYS_IN_MONTH, mday, :absolute)
263
+ assert_range_includes(1..::Montrose::Utils::MAX_DAYS_IN_MONTH, mday, :absolute)
269
264
  end
270
265
 
271
266
  def assert_yday(yday)
272
- assert_range_includes(1..MAX_DAYS_IN_YEAR, yday, :absolute)
267
+ assert_range_includes(1..::Montrose::Utils::MAX_DAYS_IN_YEAR, yday, :absolute)
273
268
  end
274
269
 
275
270
  def assert_week(week)
276
- assert_range_includes(1..MAX_WEEKS_IN_YEAR, week, :absolute)
271
+ assert_range_includes(1..::Montrose::Utils::MAX_WEEKS_IN_YEAR, week, :absolute)
277
272
  end
278
273
 
279
274
  def decompose_on_arg(arg)
@@ -62,6 +62,15 @@ module Montrose
62
62
  end
63
63
  alias to_h to_hash
64
64
 
65
+ # Returns json string of options used to create
66
+ # the recurrence
67
+ #
68
+ # @return [String] json of recurrence options
69
+ #
70
+ def to_json
71
+ to_hash.to_json
72
+ end
73
+
65
74
  def inspect
66
75
  "#<#{self.class}:#{object_id.to_s(16)} #{to_h.inspect}>"
67
76
  end
@@ -23,7 +23,7 @@ module Montrose
23
23
 
24
24
  # matches days specified at negative numbers
25
25
  def included_from_end_of_month?(time)
26
- month_days = Time.days_in_month(time.month, time.year) # given by activesupport
26
+ month_days = ::Montrose::Utils.days_in_month(time.month, time.year) # given by activesupport
27
27
  @days.any? { |d| month_days + d + 1 == time.mday }
28
28
  end
29
29
  end
@@ -22,16 +22,9 @@ module Montrose
22
22
  private
23
23
 
24
24
  def included_from_end_of_month?(time)
25
- year_days = days_in_year(time.year) # given by activesupport
25
+ year_days = ::Montrose::Utils.days_in_year(time.year) # given by activesupport
26
26
  @days.any? { |d| year_days + d + 1 == time.yday }
27
27
  end
28
-
29
- # Returns the number of days in the given year.
30
- # If no year is specified, it will use the current year.
31
- # https://github.com/rails/rails/pull/22244
32
- def days_in_year(year)
33
- Time.days_in_month(2, year) + 337
34
- end
35
28
  end
36
29
  end
37
30
  end
@@ -47,15 +47,7 @@ module Montrose
47
47
  end
48
48
 
49
49
  def total_days
50
- days_in_month(@time)
51
- end
52
-
53
- private
54
-
55
- # Get the days in the month for +time
56
- def days_in_month(time)
57
- date = Date.new(time.year, time.month, 1)
58
- ((date >> 1) - date).to_i
50
+ ::Montrose::Utils.days_in_month(@time.month, @time.year)
59
51
  end
60
52
  end
61
53
  end
@@ -2,15 +2,20 @@ module Montrose
2
2
  module Utils
3
3
  module_function
4
4
 
5
- MONTHS = Date::MONTHNAMES
6
- DAYS = Date::DAYNAMES
5
+ MONTHS = ::Date::MONTHNAMES
6
+ DAYS = ::Date::DAYNAMES
7
+
8
+ MAX_HOURS_IN_DAY = 24
9
+ MAX_DAYS_IN_YEAR = 366
10
+ MAX_WEEKS_IN_YEAR = 53
11
+ MAX_DAYS_IN_MONTH = 31
7
12
 
8
13
  def as_time(time)
9
14
  return nil unless time
10
15
 
11
16
  case
12
17
  when time.is_a?(String)
13
- Time.parse(time)
18
+ parse_time(time)
14
19
  when time.respond_to?(:to_time)
15
20
  time.to_time
16
21
  else
@@ -22,6 +27,14 @@ module Montrose
22
27
  as_time(time).to_date
23
28
  end
24
29
 
30
+ def parse_time(*args)
31
+ ::Time.zone.nil? ? ::Time.parse(*args) : ::Time.zone.parse(*args)
32
+ end
33
+
34
+ def current_time
35
+ ::Time.current
36
+ end
37
+
25
38
  def month_number(name)
26
39
  case name
27
40
  when Symbol, String
@@ -51,5 +64,17 @@ module Montrose
51
64
  day_number(name) or fail ConfigurationError,
52
65
  "Did not recognize day #{name}, must be one of #{DAYS.inspect}"
53
66
  end
67
+
68
+ def days_in_month(month, year = current_time.year)
69
+ date = ::Date.new(year, month, 1)
70
+ ((date >> 1) - date).to_i
71
+ end
72
+
73
+ # Returns the number of days in the given year.
74
+ # If no year is specified, it will use the current year.
75
+ # https://github.com/rails/rails/pull/22244
76
+ def days_in_year(year)
77
+ ::Montrose::Utils.days_in_month(2, year) + 337
78
+ end
54
79
  end
55
80
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Montrose
3
- VERSION = "0.3.0".freeze
3
+ VERSION = "0.4.0".freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: montrose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Kaffenberger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-20 00:00:00.000000000 Z
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport