monthra 0.1.0 → 0.1.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
  SHA1:
3
- metadata.gz: 79f9ae5a25b04063d065b65051723f3fb3c6089e
4
- data.tar.gz: eb5bf8ab4909feaf28f58db683d9fc526cd8a14b
3
+ metadata.gz: 45b36b845fabde171c235d4bcd98632a5e211442
4
+ data.tar.gz: a9d8c944ccd9c056b8dd29c794c4ee4859ee708b
5
5
  SHA512:
6
- metadata.gz: 5626175dc02710b9ec19d2aa5ecd61fdbb585be0bd1d5695255118c3f27f5d90ec7e0cbcf24854fcfa8931096337aa1439f8268328d7dc33c77c0262b6d68dd1
7
- data.tar.gz: c37f7ea73922de51e647d12541b0ba0f908609d709039af0bb00d6d7cd7c8706ab25bac74b49d816b1fc1d2fb2a7df240563566a98de3ef65452365048c9f1c8
6
+ metadata.gz: 3e4302f0388ea41bb927bf6fcacdd550b6401ba5b1603d156f7b66bf2bbdd545cb671eba5ff174bc70ae7b0fc1e570a8b2c32bb964839fedf2711417eb8bbd97
7
+ data.tar.gz: 5ece4f757bd79d160de0ae5f7abb8d6e43f42820476645ddbff32739dfb97cc1a50838d3066eea3f42598fe86d6165ba515639028690a29bb86049ede16b58cb
@@ -1,5 +1,10 @@
1
- require "monthra/version"
1
+ require 'date'
2
+ require 'time'
3
+
4
+ require 'monthra/monkey_patches.rb'
5
+ require 'monthra/version'
2
6
  require 'monthra/month'
7
+ require 'monthra/month_range'
3
8
 
4
9
  module Monthra
5
10
  # Your code goes here...
@@ -0,0 +1,13 @@
1
+ # Decided not to use refinements due to backwards compatibility issues with Jruby 1.7. Sorry.
2
+
3
+ class Date
4
+ def to_monthra_month
5
+ Monthra::Month.at(self)
6
+ end
7
+ end
8
+
9
+ class Time
10
+ def to_monthra_month
11
+ Monthra::Month.at(self)
12
+ end
13
+ end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Monthra
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-13 00:00:00.000000000 Z
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