periods 0.0.3 → 0.0.4

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: b340e86339eee67b5f7915f6df4d6e809aba2107
4
- data.tar.gz: 0d613dce4f4898974d2953797e8cd9cd0b6b72b1
3
+ metadata.gz: 8bca4bb489addf1c13b97b9393036dea7c7a8750
4
+ data.tar.gz: 6f9b873e913d076b08d1ea25aa6d8ccc1a275c55
5
5
  SHA512:
6
- metadata.gz: f299f089e6a1c016005d0120b8784cb317bd7ed73061dd76906fb35eb1b4a2c81da882928608b974f1c54bbda33e9f9993a6c7f362754a4a77575b82e26d92d8
7
- data.tar.gz: adc4461e7f2d1e8b2cf112a0bb8baddf9e3732ded407f6ed66d933f9493cbfc2f936d9da25336eb449cfebe817d319c17fe676c25b7d80d6aa3589354707c3e2
6
+ metadata.gz: 0fd1215e00f8890bab21bda707d6904a43d05aaa8717c3885e9114a9b41cc13fc16e3d30324ea8ade7669c4af1ea192266d5efe17bee639a66ea21bffd022ac0
7
+ data.tar.gz: 47461d42951e77ff77f8c57dd185b66b96e0e67f8a69ff7364996490ae1d90cb70d7b95ed323c13cb8cf972c693eb01ac4fbe498f715f925028e5ef198a244f5
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --protected
2
+ --no-private
3
+ -
4
+ LICENSE
5
+ README.md
6
+ lib/**/*.rb
data/README.md CHANGED
@@ -35,14 +35,11 @@ Require `periods` in your app:
35
35
 
36
36
  A period is defined by a start and end date:
37
37
 
38
- period = Period.for(Date.new(2015,6,25), Date.new(2015,8,19))
39
- period.days # => 56
40
- period.next # => 20.08.2015 - 14.10.2015
41
- period.next.days # => 56
38
+ period = Period.new(Date.new(2015,6,25), Date.new(2015,8,19))
42
39
 
43
40
  The start and end date can be a string too:
44
41
 
45
- period = Period.for('25.06.2015', '19.08.2015')
42
+ period = Period.new('25.06.2015', '19.08.2015')
46
43
 
47
44
  All period models respond to the following interface:
48
45
 
@@ -55,42 +52,100 @@ All period models respond to the following interface:
55
52
 
56
53
  Special period models may define additional methods (see below).
57
54
 
55
+ #### days
56
+
57
+ period = Period.new('25.06.2015', '19.08.2015')
58
+ period.days # => 56
59
+
60
+ #### next
61
+
62
+ period = Period.new('25.06.2015', '19.08.2015')
63
+ period.next # => 20.08.2015 - 14.10.2015
64
+
65
+ #### previous
66
+
67
+ period = Period.new('25.06.2015', '19.08.2015')
68
+ period.previous # => 30.04.2015 - 24.06.2015
69
+
70
+ #### include?
71
+
72
+ Test whether a date or a period is included in the given period:
73
+
74
+ period = Period.new('25.06.2015', '19.08.2015')
75
+ period.include?(Date.new(2015,6,25)) # => true
76
+ period.include?('25.06.2015') # => true
77
+ period.include?('24.06.2015') # => false
78
+
79
+ period.include?(Period.new('25.06.2015', '19.08.2015')) # => true
80
+ period.include?(Period.new('26.06.2015', '19.08.2015')) # => true
81
+ period.include?(Period.new('24.06.2015', '19.08.2015')) # => false
82
+ period.include?(Period.new('25.06.2015', '20.08.2015')) # => false
83
+
58
84
  ### MonthlyPeriod
59
85
 
60
86
  If you need a constant monthly range use `MonthlyPeriod`:
61
87
 
62
- monthly = MonthPeriod.for('01.01.2015') # => 01.01.2015 - 31.01.2015
63
- monthly.next # => 01.02.2015 - 28.02.2015
64
-
65
88
  monthly = MonthPeriod.for('25.06.2015') # => 25.06.2015 - 24.07.2015
66
- monthly.next # => 25.07.2015 - 24.08.2015
67
- monthly.start_date # Date(25.06.2015)
68
- monthly.end_date # Date(24.07.2015)
69
-
89
+ monthly.next # => 25.07.2015 - 24.08.2015
90
+ monthly.previous # => 25.05.2015 - 24.06.2015
91
+
70
92
  ### Month
71
93
 
72
94
  If you need a calendar month starting at first day of month and ending at last day of month use `Month`:
73
95
 
74
- january = Month.for('01.01.2015') # => 01.01.2015 - 31.01.2015
75
- january.next # => 01.02.2015 - 28.02.2015
76
-
77
96
  june = MonthPeriod.for('25.06.2015') # => 01.06.2015 - 30.06.2015
78
- june.next # => 01.07.2015 - 31.07.2015
79
- june.month # => 6
80
- june.year # => 2015
97
+ june.next # => 01.07.2015 - 31.07.2015
98
+ june.previous # => 01.05.2015 - 31.05.2015
99
+ june.month # => 6
100
+ june.year # => 2015
81
101
 
82
102
  ### QuarterlyPeriod
83
103
 
104
+ period = QuarterlyPeriod.for('25.06.2015') # => 25.06.2015 - 24.09.2015
105
+
84
106
  ### Quarter
85
107
 
108
+ A quarter starts at first day of a month and ends at last day three months later:
109
+
110
+ quarter = Quarter.for('01.02.2015') # => 01.02.2015 - 30.04.2015
111
+ quarter.next # => 01.05.2015 - 31.07.2015
112
+ quarter.previous # => 01.11.2014 - 31.01.2015
113
+
114
+ quarter = Quarter.for('01.12.2015') # => 01.12.2015 - 29.02.2016
115
+ quarter.start_year # => 2015
116
+ quarter.end_year # => 2016
117
+ quarter.months # => [Month.for('01.12.2015'), Month.for('01.01.2016'), Month.for('01.02.2016')]
118
+
86
119
  ### HalfyearlyPeriod
87
120
 
121
+ A HalfyearlyPeriod starts at the given date and ends six month later:
122
+
123
+ period = HalfyearlyPeriod.for('25.06.2015') # => 25.06.2015 - 24.12.2015
124
+
88
125
  ### Halfyear
89
126
 
90
- ### Yearly
127
+ A Halfyear starts at the first day of a month and ends six month later:
128
+
129
+ halfyear = HalfyearlyPeriod.for('25.06.2015') # => 01.06.2015 - 30.11.2015
130
+ halfyear.next # => 25.12.2015 - 24.06.2016
131
+ halfyear.previous # => 25.12.2014 - 24.06.2015
132
+ halfyear.months # => [Month.for('01.06.2015'), ..., Month.for('01.11.2015')]
133
+
134
+ ### YearlyPeriod
135
+
136
+ A YearlyPeriod starts at the given date and ends one year later:
137
+
138
+ period = YearlyPeriod.for('25.06.2015') # => 25.06.2015 - 24.06.2016
91
139
 
92
140
  ### Year
93
141
 
142
+ A year starts at first day of a month and ends one year later:
143
+
144
+ year = Year.for('01.02.2015') # => 01.02.2015 - 31.01.2016
145
+ year.next # => 01.02.2016 - 31.01.2017
146
+ year.previous # => 01.02.2014 - 31.01.2015
147
+ year.months # => [Month.for('01.02.2015'), ..., Month.for('01.01.2016')]
148
+
94
149
  ### If constants already exist
95
150
 
96
151
  In case constants like `Month` already exist in your code stop requiring `periods` in your `Gemfile`:
@@ -121,6 +176,7 @@ Write your own classes:
121
176
 
122
177
  * Implement Week
123
178
  * Use Time not Date
179
+ * Optimize README
124
180
  * Comment Code
125
181
 
126
182
  ## Contributing
@@ -10,17 +10,54 @@ module Periods
10
10
  end
11
11
 
12
12
  module InstanceMethods
13
- attr_reader :start_date, :end_date
14
13
 
14
+ # Returns the start date of this period.
15
+ # @return [Date] the start date of this period.
16
+ attr_reader :start_date
17
+
18
+ # Returns the end date of this period.
19
+ # @return [Date] the end date of this period.
20
+ attr_reader :end_date
21
+
22
+ ##
23
+ # Initialize a new period with its start and end date.
24
+ #
25
+ # @example
26
+ #
27
+ # period = Period.new('10.04.2015', '20.05.2015')
28
+ #
15
29
  def initialize(start_date, end_date)
16
30
  @start_date = Date.parse(start_date.to_s)
17
31
  @end_date = Date.parse(end_date.to_s)
18
32
  end
19
33
 
34
+ ##
35
+ # Test whether the given period is equal to this one.
36
+ # A period is considered equal if it has the same start and end date.
37
+ #
38
+ # @return [Boolean] true if period is equal, false otherwise.
39
+ #
40
+ # @since 0.0.3
41
+ #
42
+ # @example
43
+ #
44
+ # period1 = Period.new('10.04.2015', '20.05.2015')
45
+ # period2 = Period.new('10.04.2015', '20.05.2015')
46
+ # period3 = Period.new('09.04.2015', '20.05.2015')
47
+ # period1 == period2 # => true
48
+ # period1 == period3 # => false
49
+ #
20
50
  def ==(period)
21
51
  start_date == period.start_date && end_date == period.end_date
22
52
  end
23
53
 
54
+ ##
55
+ # Test whether the given period is older, newer or equal to this one.
56
+ #
57
+ # @return [Fixnum] 1 if period is older, -1 if newer, 0 if equal.
58
+ #
59
+ # @since 0.0.3
60
+ #
24
61
  def <=>(period)
25
62
  if start_date > period.start_date
26
63
  1
@@ -31,18 +68,106 @@ module Periods
31
68
  end
32
69
  end
33
70
 
71
+ ##
72
+ # Returns next period.
73
+ #
74
+ # @return [Period] the next period.
75
+ #
76
+ # @since 0.0.3
77
+ #
78
+ # @see Periods::Modules::Period#previous
79
+ #
80
+ # @example
81
+ #
82
+ # period = Period.new('10.04.2015', '20.05.2015')
83
+ # period.next # => 21.05.2015 - 30.06.2015
84
+ #
34
85
  def next
35
86
  self.class.new(start_date + days, end_date + days)
36
87
  end
37
88
 
89
+ ##
90
+ # Returns previous period.
91
+ #
92
+ # @return [Period] the previous period.
93
+ #
94
+ # @since 0.0.3
95
+ #
96
+ # @see Periods::Modules::Period#next
97
+ #
98
+ # @example
99
+ #
100
+ # period = Period.new('10.04.2015', '20.05.2015')
101
+ # period.previous # => 28.02.2015 - 09.04.2015
102
+ #
38
103
  def previous
39
104
  self.class.new(start_date - days, end_date - days)
40
105
  end
41
106
 
107
+ ##
108
+ # Returns number of days in period.
109
+ #
110
+ # @return [Fixnum] the number of days in period.
111
+ #
112
+ # @since 0.0.3
113
+ #
114
+ # @example
115
+ #
116
+ # Period.new('10.04.2015', '20.05.2015').days # => 41
117
+ #
42
118
  def days
43
119
  end_date.yday - start_date.yday + 1
44
120
  end
45
121
 
122
+ ##
123
+ # Returns start year of period.
124
+ #
125
+ # @return [Fixnum] start year of period.
126
+ #
127
+ # @since 0.0.3
128
+ #
129
+ # @example
130
+ #
131
+ # Period.new('10.04.2015', '20.05.2016').start_year # => 2015
132
+ #
133
+ def start_year
134
+ start_date.year
135
+ end
136
+
137
+ ##
138
+ # Returns end year of period.
139
+ #
140
+ # @return [Fixnum] end year of period.
141
+ #
142
+ # @since 0.0.3
143
+ #
144
+ # @example
145
+ #
146
+ # Period.new('10.04.2015', '20.05.2016').end_year # => 2016
147
+ #
148
+ def end_year
149
+ end_date.year
150
+ end
151
+
152
+ ##
153
+ # Test whether the given date or period is included in this one.
154
+ #
155
+ # @return [Boolean] true if date or period is included, false otherwise.
156
+ #
157
+ # @since 0.0.3
158
+ #
159
+ # @example
160
+ #
161
+ # Period.new('10.04.2015', '20.05.2015').include?('10.04.2015') # => true
162
+ # Period.new('10.04.2015', '20.05.2015').include?('20.05.2015') # => true
163
+ # Period.new('10.04.2015', '20.05.2015').include?('09.04.2015') # => false
164
+ # Period.new('10.04.2015', '20.05.2015').include?('21.05.2015') # => false
165
+ #
166
+ # Period.new('10.04.2015', '20.05.2015').include?(Period.new('10.04.2015', '20.05.2015')) # => true
167
+ # Period.new('10.04.2015', '20.05.2015').include?(Period.new('15.04.2015', '15.05.2015')) # => true
168
+ # Period.new('10.04.2015', '20.05.2015').include?(Period.new('09.04.2015', '20.05.2015')) # => false
169
+ # Period.new('10.04.2015', '20.05.2015').include?(Period.new('10.04.2015', '21.05.2015')) # => false
170
+ #
46
171
  def include?(period)
47
172
  if period.is_a?(Date)
48
173
  start_date <= period && period <= end_date
@@ -56,6 +181,17 @@ module Periods
56
181
  end
57
182
  end
58
183
 
184
+ ##
185
+ # Return string representation of period.
186
+ #
187
+ # @return [String] the string representation of period.
188
+ #
189
+ # @since 0.0.3
190
+ #
191
+ # @example
192
+ #
193
+ # Period.new('10.04.2015', '20.05.2015').to_s # => '10.04.2015 - 20.05.2015'
194
+ #
59
195
  def to_s
60
196
  "#{start_date.strftime("%d.%m.%Y")} - #{end_date.strftime("%d.%m.%Y")}"
61
197
  end
@@ -35,14 +35,6 @@ module Periods
35
35
  Periods::Month.for(start_date.next_month(2)) ]
36
36
  end
37
37
 
38
- def start_year
39
- start_date.year
40
- end
41
-
42
- def end_year
43
- end_date.year
44
- end
45
-
46
38
  private
47
39
 
48
40
  def beginning_of_month(date)
@@ -47,10 +47,6 @@ module Periods
47
47
  (self.next.start_date - start_date).to_i
48
48
  end
49
49
 
50
- def year
51
- start_date.year
52
- end
53
-
54
50
  def months
55
51
  months = [Periods::Month.for(start_date)]
56
52
  1.upto(11) do |idx|
@@ -1,3 +1,3 @@
1
1
  module Periods
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -8,23 +8,23 @@ describe Halfyear do
8
8
  it_behaves_like "Lint Check"
9
9
 
10
10
  describe ".for" do
11
- it "returns month of given date included" do
12
- month = described_class.for('25.06.2015')
11
+ it "returns halfyear of given date included" do
12
+ halfyear = described_class.for('25.06.2015')
13
13
 
14
- expect(month.start_date).to eq Date('01.06.2015')
15
- expect(month.end_date).to eq Date('30.11.2015')
14
+ expect(halfyear.start_date).to eq Date('01.06.2015')
15
+ expect(halfyear.end_date).to eq Date('30.11.2015')
16
16
  end
17
17
  end
18
18
 
19
19
  describe "#next" do
20
- it "returns next month" do
20
+ it "returns next halfyear" do
21
21
  expect(described_class.for('25.06.2015').next).to eq described_class.for('01.12.2015')
22
22
  expect(described_class.for('01.07.2015').next).to eq described_class.for('01.01.2016')
23
23
  end
24
24
  end
25
25
 
26
26
  describe "#previous" do
27
- it "returns previous month" do
27
+ it "returns previous halfyear" do
28
28
  expect(described_class.for('25.06.2015').previous).to eq described_class.for('01.12.2014')
29
29
  expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.01.2015')
30
30
  end
@@ -17,14 +17,14 @@ describe HalfyearlyPeriod do
17
17
  end
18
18
 
19
19
  describe "#next" do
20
- it "returns next quarter" do
20
+ it "returns next period" do
21
21
  expect(described_class.for('25.06.2015').next).to eq described_class.for('25.12.2015')
22
22
  expect(described_class.for('01.07.2015').next).to eq described_class.for('01.01.2016')
23
23
  end
24
24
  end
25
25
 
26
26
  describe "#previous" do
27
- it "returns previous quarter" do
27
+ it "returns previous period" do
28
28
  expect(described_class.for('25.06.2015').previous).to eq described_class.for('25.12.2014')
29
29
  expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.01.2015')
30
30
  end
@@ -55,6 +55,20 @@ describe Period do
55
55
  end
56
56
  end
57
57
 
58
+ describe "#start_year" do
59
+ it "returns start year of quarter" do
60
+ expect(new_period("25.06.2015", "19.08.2015").start_year).to eq 2015
61
+ expect(new_period("25.06.2015", "20.05.2016").start_year).to eq 2015
62
+ end
63
+ end
64
+
65
+ describe "#end_year" do
66
+ it "returns end year of quarter" do
67
+ expect(new_period("25.06.2015", "19.08.2015").end_year).to eq 2015
68
+ expect(new_period("25.06.2015", "20.05.2016").end_year).to eq 2016
69
+ end
70
+ end
71
+
58
72
  describe "#include?" do
59
73
  context "date included" do
60
74
  it "returns true" do
@@ -101,17 +101,4 @@ describe Quarter do
101
101
  end
102
102
  end
103
103
 
104
- describe "#start_year" do
105
- it "returns start year of quarter" do
106
- expect(described_class.for('01.06.2015').start_year).to eq 2015
107
- expect(described_class.for('01.06.2016').start_year).to eq 2016
108
- end
109
- end
110
-
111
- describe "#end_year" do
112
- it "returns end year of quarter" do
113
- expect(described_class.for('01.06.2015').end_year).to eq 2015
114
- expect(described_class.for('01.12.2015').end_year).to eq 2016
115
- end
116
- end
117
104
  end
@@ -8,36 +8,28 @@ describe Year do
8
8
  it_behaves_like "Lint Check"
9
9
 
10
10
  describe ".for" do
11
- it "returns month of given date included" do
12
- month = described_class.for('25.06.2015')
11
+ it "returns year of given date included" do
12
+ year = described_class.for('25.06.2015')
13
13
 
14
- expect(month.start_date).to eq Date('01.06.2015')
15
- expect(month.end_date).to eq Date('31.05.2016')
14
+ expect(year.start_date).to eq Date('01.06.2015')
15
+ expect(year.end_date).to eq Date('31.05.2016')
16
16
  end
17
17
  end
18
18
 
19
19
  describe "#next" do
20
- it "returns next month" do
20
+ it "returns next year" do
21
21
  expect(described_class.for('25.06.2015').next).to eq described_class.for('01.06.2016')
22
22
  expect(described_class.for('01.07.2015').next).to eq described_class.for('01.07.2016')
23
23
  end
24
24
  end
25
25
 
26
26
  describe "#previous" do
27
- it "returns previous month" do
27
+ it "returns previous year" do
28
28
  expect(described_class.for('25.06.2015').previous).to eq described_class.for('01.06.2014')
29
29
  expect(described_class.for('01.06.2015').previous).to eq described_class.for('01.06.2014')
30
30
  end
31
31
  end
32
32
 
33
- describe "#year" do
34
- it "returns year number" do
35
- expect(described_class.for('01.01.2015').year).to eq 2015
36
- expect(described_class.for('01.12.2015').year).to eq 2015
37
- expect(described_class.for('01.01.2014').year).to eq 2014
38
- end
39
- end
40
-
41
33
  describe "#months" do
42
34
  it "returns months of year" do
43
35
  expect(described_class.for('01.06.2015').months).
@@ -17,14 +17,14 @@ describe YearlyPeriod do
17
17
  end
18
18
 
19
19
  describe "#next" do
20
- it "returns next quarter" do
20
+ it "returns next period" do
21
21
  expect(described_class.for('25.06.2015').next).to eq described_class.for('25.06.2016')
22
22
  expect(described_class.for('01.07.2015').next).to eq described_class.for('01.07.2016')
23
23
  end
24
24
  end
25
25
 
26
26
  describe "#previous" do
27
- it "returns previous quarter" do
27
+ it "returns previous period" do
28
28
  expect(described_class.for('25.06.2015').previous).to eq described_class.for('25.06.2014')
29
29
  expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.07.2014')
30
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: periods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Baustert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-14 00:00:00.000000000 Z
11
+ date: 2015-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,6 +62,7 @@ files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
64
  - ".travis.yml"
65
+ - ".yardopts"
65
66
  - Gemfile
66
67
  - LICENSE
67
68
  - README.md