duration-range 0.2.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: 31ca768c2bf4dc12f6de5f78ff2efcd8a08962c3b0c106583979c138ba688f7b
4
- data.tar.gz: 34a075181c3f19e13bc1148481d60540841c0df5212f47fc2da94f99b01a1cc9
3
+ metadata.gz: 2906de236492e14acd76597c419e63417d7caad207afecd8ddcbb0818614055a
4
+ data.tar.gz: 610a910d62c6b9d3eedc64445752b9bebe1526da96c40f38c02ebfa4ff574db8
5
5
  SHA512:
6
- metadata.gz: 06daafdabccf9ff4473041e1b072a48dd12fef80b22d3639625da2cd7270ed693c12308eec6cbb085e7336d8eaa0ec0b9a739775f277584b711b92ae347ba1e6
7
- data.tar.gz: c2998d5d3496143fb23655f0a552a8b6d1e97f547f51a294056b5513268c80e8f732c0e298a97a29e5a52c952f2673c105445d59b977ba5bfd372e80d81b38fb
6
+ metadata.gz: 0a7c3a32aad450e98d5ecf58eab08b085fea3f41df8c55b675a13f70f5bfbf1565d40e0a7d7efe885a9aa4b8c1dfb2fc851d65e726e0ecfe8575662344c5bfb7
7
+ data.tar.gz: 7357529ea96eb43b21cc96bd620a96ede03c06408b49fcce07cc9f1c685f6e3abfd398015d9bdc4ae2e3e82218c32d357595ee7e8628fa88b325c35c3eb0947f
data/README.md CHANGED
@@ -2,6 +2,34 @@
2
2
 
3
3
  duration range data generator
4
4
 
5
+ ## Features
6
+
7
+ generate Array / Hash of Date / Time
8
+
9
+ e.g.
10
+
11
+ ```
12
+ {:begin=>Mon, 19 Apr 2021, :end=>Sun, 25 Apr 2021} # week as Hash of Date
13
+ [Mon, 19 Apr 2021, Tue, 20 Apr 2021, ... Sun, 25 Apr 2021] # week as Array of Date
14
+ {:begin=>2021-04-01 00:00:00 UTC, :end=>2021-05-01 00:00:00 UTC} # month as Hash of Time
15
+ ```
16
+
17
+ available classes:
18
+
19
+ * DurationRange::Date ( default type Array )
20
+ * DurationRange::Time ( default type Hash )
21
+
22
+ available methods:
23
+
24
+ * this_week
25
+ * last_week
26
+ * next_week
27
+ * this_month
28
+ * last_month
29
+ * next_month
30
+ * month
31
+ * months
32
+
5
33
  ## Installation
6
34
 
7
35
  Add this line to your application's Gemfile:
@@ -31,6 +59,12 @@ DurationRange::Date.new.last_month(as: :hash)
31
59
 
32
60
  DurationRange::Time.new.last_month
33
61
  # => {:begin=>2021-04-01 00:00:00 UTC, :end=>2021-05-01 00:00:00 UTC}
62
+
63
+ DurationRange::Time.new.month('2021-04')
64
+ # => {:begin=>2021-04-01 00:00:00 UTC, :end=>2021-05-01 00:00:00 UTC}
65
+
66
+ DurationRange::Date.new.months('2021-04', '2021-06')
67
+ # => [Thu, 01 Apr 2021, Fri, 02 Apr 2021, ... Wed, 30 Jun 2021]
34
68
  ```
35
69
 
36
70
  ## Development
@@ -31,12 +31,46 @@ module DurationRange
31
31
  end
32
32
  end
33
33
 
34
+ #
35
+ # @param month [String]
36
+ # @param as [Symbol]
37
+ # @return [Object]
38
+ #
39
+ def month_as_date(month, as:)
40
+ this_month_as_date(as: as, reference_date: ::Date.parse(month + '-01'))
41
+ end
42
+
43
+ #
44
+ # @param months [Array]
45
+ # @param as [Symbol]
46
+ # @return [Object]
47
+ #
48
+ # :reek:NilCheck :reek:TooMenyStatements
49
+ def months_as_date(*months, as:)
50
+ min = nil
51
+ max = nil
52
+ months.each { |mon|
53
+ dates = month_as_date(mon, as: :hash)
54
+ begin_date = dates[:begin]
55
+ end_date = dates[:end]
56
+
57
+ if min.nil? || begin_date < min
58
+ min = begin_date
59
+ end
60
+ if max.nil? || max < end_date
61
+ max = end_date
62
+ end
63
+ }
64
+
65
+ tidy_with_date(as: as, begin_date: min, end_date: max)
66
+ end
67
+
34
68
  #
35
69
  # @param as [Symbol]
36
70
  # @param reference_date [Date]
37
71
  # @return [Object]
38
72
  #
39
- def this_month(as:, reference_date: today)
73
+ def this_month_as_date(as:, reference_date: today)
40
74
  begin_date = reference_date.at_beginning_of_month
41
75
  end_date = reference_date.at_end_of_month
42
76
 
@@ -6,13 +6,35 @@ module DurationRange
6
6
  class Date
7
7
  include DurationRange::CommonFunctions
8
8
 
9
+ #
10
+ # generate specific month duration (e.g. 2021-04)
11
+ #
12
+ # @param month [String]
13
+ # @param as [Symbol]
14
+ # @return [Object]
15
+ #
16
+ def month(month, as: :array)
17
+ month_as_date(month, as: as)
18
+ end
19
+
20
+ #
21
+ # longest duration from multiple months
22
+ #
23
+ # @param months [Array]
24
+ # @param as [Symbol]
25
+ # @return [Object]
26
+ #
27
+ def months(*months, as: :array)
28
+ months_as_date(*months, as: as)
29
+ end
30
+
9
31
  #
10
32
  # @param as [Symbol]
11
33
  # @param reference_date [Date]
12
34
  # @return [Object]
13
35
  #
14
36
  def this_month(as: :array, reference_date: today)
15
- super(as: as, reference_date: reference_date)
37
+ this_month_as_date(as: as, reference_date: reference_date)
16
38
  end
17
39
 
18
40
  #
@@ -45,13 +45,35 @@ module DurationRange
45
45
  with_localtime ? date.to_time : date.to_time(:utc)
46
46
  end
47
47
 
48
+ #
49
+ # @param month [String]
50
+ # @param as [Symbol]
51
+ # @return [Object]
52
+ #
53
+ def month(month, as: :hash)
54
+ dates = month_as_date(month, as: :hash)
55
+
56
+ tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
57
+ end
58
+
59
+ #
60
+ # @param months [Array]
61
+ # @param as [Symbol]
62
+ # @return [Object]
63
+ #
64
+ def months(*months, as: :hash)
65
+ dates = months_as_date(*months, as: :hash)
66
+
67
+ tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
68
+ end
69
+
48
70
  #
49
71
  # @param as [Symbol]
50
72
  # @param reference_date [Date]
51
73
  # @return [Object]
52
74
  #
53
75
  def this_month(as: :hash, reference_date: today)
54
- dates = super(as: :hash, reference_date: reference_date)
76
+ dates = this_month_as_date(as: :hash, reference_date: reference_date)
55
77
 
56
78
  tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
57
79
  end
@@ -1,3 +1,3 @@
1
1
  module DurationRange
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duration-range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - wtnabe