monthra 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/monthra.rb +6 -1
- data/lib/monthra/monkey_patches.rb +13 -0
- data/lib/monthra/month.rb +58 -3
- data/lib/monthra/month_range.rb +69 -0
- data/lib/monthra/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45b36b845fabde171c235d4bcd98632a5e211442
|
4
|
+
data.tar.gz: a9d8c944ccd9c056b8dd29c794c4ee4859ee708b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e4302f0388ea41bb927bf6fcacdd550b6401ba5b1603d156f7b66bf2bbdd545cb671eba5ff174bc70ae7b0fc1e570a8b2c32bb964839fedf2711417eb8bbd97
|
7
|
+
data.tar.gz: 5ece4f757bd79d160de0ae5f7abb8d6e43f42820476645ddbff32739dfb97cc1a50838d3066eea3f42598fe86d6165ba515639028690a29bb86049ede16b58cb
|
data/lib/monthra.rb
CHANGED
data/lib/monthra/month.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'date'
|
2
|
-
require 'time'
|
3
|
-
|
4
1
|
module Monthra
|
5
2
|
class Month
|
6
3
|
include Comparable
|
@@ -41,6 +38,11 @@ module Monthra
|
|
41
38
|
end
|
42
39
|
end
|
43
40
|
|
41
|
+
# @return [Month] To Match the monkey patch in Date and Time
|
42
|
+
def to_monthra_month
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
44
46
|
# @return [Fixnum] The year
|
45
47
|
def year
|
46
48
|
@year
|
@@ -80,8 +82,61 @@ module Monthra
|
|
80
82
|
strfmonth("%Y-%m")
|
81
83
|
end
|
82
84
|
|
85
|
+
# @param [Integer or Month] offset How much to add to the current month.
|
86
|
+
# @return [Month]
|
87
|
+
def +(offset)
|
88
|
+
offsets = month_year_offset(offset)
|
89
|
+
|
90
|
+
new_year = year + offsets[:year]
|
91
|
+
new_month = month + offsets[:month]
|
92
|
+
|
93
|
+
if new_month > 12
|
94
|
+
new_month -= 12
|
95
|
+
new_year += 1
|
96
|
+
end
|
97
|
+
|
98
|
+
self.class.new(new_year, new_month)
|
99
|
+
end
|
100
|
+
|
101
|
+
# @param [Integer or Month] offset How much to subtract to the current month.
|
102
|
+
# @return [Month]
|
103
|
+
def -(offset)
|
104
|
+
offsets = month_year_offset(offset)
|
105
|
+
|
106
|
+
new_year = year - offsets[:year]
|
107
|
+
new_month = month - offsets[:month]
|
108
|
+
|
109
|
+
if new_month < 1
|
110
|
+
new_month += 12 # note there is no month 0
|
111
|
+
new_year -= 1
|
112
|
+
end
|
113
|
+
|
114
|
+
self.class.new(new_year, new_month)
|
115
|
+
end
|
116
|
+
|
83
117
|
private
|
84
118
|
|
119
|
+
# @param [Integer or Month] offset How much to add to the current month. If a month
|
120
|
+
# object is passed in, both the year and month values are considered. If an integer is passed
|
121
|
+
# in, it represents the number of months.
|
122
|
+
# @return [Hash] With :year and :month keys with offsets
|
123
|
+
def month_year_offset(offset)
|
124
|
+
if offset.is_a?(Integer)
|
125
|
+
year_offset = offset / 12
|
126
|
+
month_offset = offset % 12
|
127
|
+
elsif offset.is_a?(Month)
|
128
|
+
year_offset = offset.year
|
129
|
+
month_offset = offset.month
|
130
|
+
else
|
131
|
+
raise ArgumentError, "Unsupported data type, #{month_offset.class.name}"
|
132
|
+
end
|
133
|
+
|
134
|
+
return {
|
135
|
+
year: year_offset,
|
136
|
+
month: month_offset
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
85
140
|
# if month > 12 or < -12 or == 0, raise exception
|
86
141
|
# if the month < 0, then subtract that number from 12 and decrement the year
|
87
142
|
# otherwise, just set the month and year
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Monthra
|
2
|
+
class MonthRange
|
3
|
+
include Enumerable
|
4
|
+
attr_accessor :months
|
5
|
+
|
6
|
+
# Note that this method will return a range with <size> number of months.
|
7
|
+
# It will not return a range from <start month> to <start month + size>
|
8
|
+
# It will actually return a range from <start month> to <start month + size - 1> because
|
9
|
+
# that will return a range with <size> elements.
|
10
|
+
#
|
11
|
+
# @param [Month/Date/Time] start_month The beginning of the range. Needs to include a year and month
|
12
|
+
# method to match the interface.
|
13
|
+
# @param [Integer] size The Number of months in the range
|
14
|
+
def self.months_after_start(start_month, size)
|
15
|
+
start_month = Month.at(start_month)
|
16
|
+
# the end date is inclusive so it needs to end one month before <size> to get the correct
|
17
|
+
# number of months
|
18
|
+
end_month = start_month + (size - 1)
|
19
|
+
|
20
|
+
self.new(start_month, end_month)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param [Month/Date/Time] start_month The beginning of the range. Needs to include a year and
|
24
|
+
# month method to match the interface.
|
25
|
+
# @param [Month/Date/Time] end_month The end of the range. Needs to include a year and month
|
26
|
+
# method to match the interface.
|
27
|
+
def initialize(start_month, end_month)
|
28
|
+
start_month = Month.at(start_month)
|
29
|
+
end_month = Month.at(end_month)
|
30
|
+
|
31
|
+
if end_month < start_month
|
32
|
+
raise ArgumentError, "Start month must occur before the end month"
|
33
|
+
end
|
34
|
+
|
35
|
+
setup_months(start_month, end_month)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Iterates over the months to meet the conditions of Enumerable
|
39
|
+
def each
|
40
|
+
months.each do |m|
|
41
|
+
yield(m)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [Integer] Number of months in the range
|
46
|
+
def size
|
47
|
+
months.size
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Month] The last month in the range
|
51
|
+
def last
|
52
|
+
months.last
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# Creates the internal representation of the month range
|
58
|
+
#
|
59
|
+
# @param [Month] start_month
|
60
|
+
# @param [Month] end_month
|
61
|
+
def setup_months(start_month, end_month)
|
62
|
+
@months = []
|
63
|
+
while start_month <= end_month
|
64
|
+
@months << start_month
|
65
|
+
start_month += 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/monthra/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monthra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jeremiah.hemphill@gmail.com
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,7 +70,9 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/monthra.rb
|
73
|
+
- lib/monthra/monkey_patches.rb
|
73
74
|
- lib/monthra/month.rb
|
75
|
+
- lib/monthra/month_range.rb
|
74
76
|
- lib/monthra/version.rb
|
75
77
|
- monthra.gemspec
|
76
78
|
homepage: https://github.com/jeremiahishere/monthra
|