periods 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +134 -0
  8. data/Rakefile +7 -0
  9. data/lib/january.rb +22 -0
  10. data/lib/periods/classes.rb +11 -0
  11. data/lib/periods/constants.rb +16 -0
  12. data/lib/periods/date_calculator.rb +25 -0
  13. data/lib/periods/halfyear.rb +9 -0
  14. data/lib/periods/halfyearly_period.rb +8 -0
  15. data/lib/periods/modules/halfyear.rb +52 -0
  16. data/lib/periods/modules/halfyearly_period.rb +52 -0
  17. data/lib/periods/modules/month.rb +61 -0
  18. data/lib/periods/modules/monthly_period.rb +43 -0
  19. data/lib/periods/modules/period.rb +58 -0
  20. data/lib/periods/modules/quarter.rb +59 -0
  21. data/lib/periods/modules/quarterly_period.rb +55 -0
  22. data/lib/periods/modules/year.rb +85 -0
  23. data/lib/periods/modules/yearly_period.rb +57 -0
  24. data/lib/periods/modules.rb +12 -0
  25. data/lib/periods/month.rb +12 -0
  26. data/lib/periods/monthly_period.rb +12 -0
  27. data/lib/periods/period.rb +9 -0
  28. data/lib/periods/quarter.rb +9 -0
  29. data/lib/periods/quarterly_period.rb +8 -0
  30. data/lib/periods/version.rb +3 -0
  31. data/lib/periods/year.rb +9 -0
  32. data/lib/periods/yearly_period.rb +9 -0
  33. data/lib/periods.rb +4 -0
  34. data/periods.gemspec +24 -0
  35. data/spec/lib/halfyear_spec.rb +41 -0
  36. data/spec/lib/halfyearly_period_spec.rb +74 -0
  37. data/spec/lib/january_spec.rb +44 -0
  38. data/spec/lib/month_spec.rb +58 -0
  39. data/spec/lib/monthly_period_spec.rb +35 -0
  40. data/spec/lib/period_spec.rb +75 -0
  41. data/spec/lib/periods/date_calculator_spec.rb +24 -0
  42. data/spec/lib/quarter_spec.rb +117 -0
  43. data/spec/lib/quarterly_period_spec.rb +80 -0
  44. data/spec/lib/year_spec.rb +51 -0
  45. data/spec/lib/yearly_period_spec.rb +74 -0
  46. data/spec/lint_check.rb +24 -0
  47. data/spec/spec_helper.rb +79 -0
  48. metadata +145 -0
@@ -0,0 +1,85 @@
1
+ require 'periods/date_calculator'
2
+ require 'periods/modules/yearly_period'
3
+
4
+ module Periods
5
+ module Modules
6
+ module Year
7
+
8
+ def self.included(base)
9
+ base.class_eval do
10
+ include Periods::Modules::YearlyPeriod
11
+ include InstanceMethods
12
+ extend ClassMethods
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ ##
18
+ # '25.06.2015' => 01.06.2015 - '30.05.2016'
19
+ #
20
+ def for(date)
21
+ new(date)
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+
27
+ def initialize(date)
28
+ date = Date.parse(date.to_s)
29
+ super(beginning_of_month(date), end_of_month(beginning_of_month(date).next_year.prev_day))
30
+ end
31
+
32
+ ##
33
+ # 01.06.2015 => 01.06.2016
34
+ #
35
+ def next
36
+ self.class.for(start_date.next_year)
37
+ end
38
+
39
+ ##
40
+ # 01.06.2015 => 01.06.2014
41
+ #
42
+ def previous
43
+ self.class.for(start_date.prev_year)
44
+ end
45
+
46
+ def days
47
+ (self.next.start_date - start_date).to_i
48
+ end
49
+
50
+ def include?(period)
51
+ if period.is_a?(String) || period.is_a?(Date)
52
+ super
53
+ elsif period.is_a?(Period)
54
+ start_date <= period.start_date && period.end_date <= end_date
55
+ else
56
+ false
57
+ end
58
+ end
59
+
60
+ def year
61
+ start_date.year
62
+ end
63
+
64
+ def months
65
+ months = [Periods::Month.for(start_date)]
66
+ 1.upto(11) do |idx|
67
+ months << Periods::Month.for(start_date.next_month(idx))
68
+ end
69
+ months
70
+ end
71
+
72
+ private
73
+
74
+ def beginning_of_month(date)
75
+ Periods::DateCalculator.new(date).beginning_of_month
76
+ end
77
+
78
+ def end_of_month(date)
79
+ Periods::DateCalculator.new(date).end_of_month
80
+ end
81
+
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,57 @@
1
+ require 'periods/modules/period'
2
+
3
+ module Periods
4
+ module Modules
5
+ module YearlyPeriod
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+ include Periods::Modules::Period
10
+ include InstanceMethods
11
+ extend ClassMethods
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ ##
17
+ # '25.06.2015' => 25.06.2015 - '24.06.2016'
18
+ #
19
+ def for(date)
20
+ date = Date.parse(date.to_s)
21
+ new(date, date.next_year.prev_day)
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ ##
27
+ # 25.06.2015 => 25.06.2016
28
+ #
29
+ def next
30
+ self.class.for(start_date.next_year)
31
+ end
32
+
33
+ ##
34
+ # 25.06.2015 => 25.06.2014
35
+ #
36
+ def previous
37
+ self.class.for(start_date.prev_year)
38
+ end
39
+
40
+ def days
41
+ (self.next.start_date - start_date).to_i
42
+ end
43
+
44
+ def include?(period)
45
+ if period.is_a?(String) || period.is_a?(Date)
46
+ super
47
+ elsif period.is_a?(Period)
48
+ start_date <= period.start_date && period.end_date <= end_date
49
+ else
50
+ false
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ require 'periods/modules/period'
2
+ require 'periods/modules/month'
3
+ require 'periods/modules/monthly_period'
4
+ require 'periods/modules/quarter'
5
+ require 'periods/modules/quarterly_period'
6
+ require 'periods/modules/halfyear'
7
+ require 'periods/modules/halfyearly_period'
8
+ require 'periods/modules/year'
9
+ require 'periods/modules/yearly_period'
10
+
11
+
12
+
@@ -0,0 +1,12 @@
1
+ require 'periods/modules/month'
2
+
3
+ ##
4
+ # A month always starts at first day of month and ends at last day of month.
5
+ #
6
+ module Periods
7
+ class Month
8
+ include Periods::Modules::Month
9
+ end
10
+ end
11
+
12
+
@@ -0,0 +1,12 @@
1
+ require 'periods/modules/monthly_period'
2
+
3
+ ##
4
+ # A monthly period starts one a day X in month and ends on one day before X in the next month,
5
+ # e.g. from 25.06.2015 to 24.07.2015
6
+ #
7
+ module Periods
8
+ class MonthlyPeriod
9
+ include Periods::Modules::MonthlyPeriod
10
+ end
11
+ end
12
+
@@ -0,0 +1,9 @@
1
+ require 'periods/modules/period'
2
+
3
+ module Periods
4
+ class Period
5
+ include Periods::Modules::Period
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,9 @@
1
+ require 'periods/modules/quarter'
2
+
3
+ module Periods
4
+ class Quarter
5
+ include Periods::Modules::Quarter
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,8 @@
1
+ require 'periods/modules/quarterly_period'
2
+
3
+ module Periods
4
+ class QuarterlyPeriod
5
+ include Periods::Modules::QuarterlyPeriod
6
+ end
7
+ end
8
+
@@ -0,0 +1,3 @@
1
+ module Periods
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'periods/modules/year'
2
+
3
+ module Periods
4
+ class Year
5
+ include Periods::Modules::Year
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,9 @@
1
+ require 'periods/modules/yearly_period'
2
+
3
+ module Periods
4
+ class YearlyPeriod
5
+ include Periods::Modules::YearlyPeriod
6
+ end
7
+ end
8
+
9
+
data/lib/periods.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'periods/constants'
2
+
3
+ module Periods
4
+ end
data/periods.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'periods/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "periods"
8
+ spec.version = Periods::VERSION
9
+ spec.authors = ["Thomas Baustert"]
10
+ spec.email = ["business@thomasbaustert.de"]
11
+ spec.summary = %q{Simple period types like Week, Month, Quarter, Halfyear, Year.}
12
+ spec.description = %q{Simple period types like Week, Month, Quarter, Halfyear, Year.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'periods/constants'
3
+
4
+ describe Halfyear do
5
+
6
+ let(:subject) { described_class.for("1.1.2015") }
7
+
8
+ it_behaves_like "Lint Check"
9
+
10
+ describe ".for" do
11
+ it "returns month of given date included" do
12
+ month = described_class.for('25.06.2015')
13
+
14
+ expect(month.start_date).to eq date('01.06.2015')
15
+ expect(month.end_date).to eq date('30.11.2015')
16
+ end
17
+ end
18
+
19
+ describe "#next" do
20
+ it "returns next month" do
21
+ expect(described_class.for('25.06.2015').next).to eq described_class.for('01.12.2015')
22
+ expect(described_class.for('01.07.2015').next).to eq described_class.for('01.01.2016')
23
+ end
24
+ end
25
+
26
+ describe "#previous" do
27
+ it "returns previous month" do
28
+ expect(described_class.for('25.06.2015').previous).to eq described_class.for('01.12.2014')
29
+ expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.01.2015')
30
+ end
31
+ end
32
+
33
+ describe "#months" do
34
+ it "returns months of year" do
35
+ expect(described_class.for('01.06.2015').months).
36
+ to eq [ Month.for('01.06.2015'), Month.for('01.07.2015'), Month.for('01.08.2015'),
37
+ Month.for('01.09.2015'), Month.for('01.10.2015'), Month.for('01.11.2015')
38
+ ]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ require 'periods/constants'
3
+
4
+ describe HalfyearlyPeriod do
5
+
6
+ let(:subject) { described_class.for("1.1.2015") }
7
+
8
+ it_behaves_like "Lint Check"
9
+
10
+ describe ".for" do
11
+ it "returns halfyear of given date included" do
12
+ period = described_class.for('25.06.2015')
13
+
14
+ expect(period.start_date).to eq date('25.06.2015')
15
+ expect(period.end_date).to eq date('24.12.2015')
16
+ end
17
+ end
18
+
19
+ describe "#next" do
20
+ it "returns next quarter" do
21
+ expect(described_class.for('25.06.2015').next).to eq described_class.for('25.12.2015')
22
+ expect(described_class.for('01.07.2015').next).to eq described_class.for('01.01.2016')
23
+ end
24
+ end
25
+
26
+ describe "#previous" do
27
+ it "returns previous quarter" do
28
+ expect(described_class.for('25.06.2015').previous).to eq described_class.for('25.12.2014')
29
+ expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.01.2015')
30
+ end
31
+ end
32
+
33
+ describe "#days" do
34
+ it "returns days of period" do
35
+ expect(described_class.for("01.01.2015").days).to eq 181
36
+ expect(described_class.for("25.06.2015").days).to eq 183
37
+ expect(described_class.for("01.01.2016").days).to eq 182
38
+ expect(described_class.for("01.03.2016").days).to eq 184
39
+ end
40
+ end
41
+
42
+ describe "#include?" do
43
+ let(:period) { described_class.for('25.06.2015') }
44
+
45
+ context "date included" do
46
+ it "returns true" do
47
+ expect(period.include?('25.06.2015')).to be_truthy
48
+ expect(period.include?('24.12.2015')).to be_truthy
49
+ end
50
+ end
51
+
52
+ context "date not included" do
53
+ it "returns false" do
54
+ expect(period.include?('24.06.2015')).to be_falsey
55
+ expect(period.include?('25.12.2015')).to be_falsey
56
+ end
57
+ end
58
+
59
+ context "period included" do
60
+ it "returns true" do
61
+ expect(period.include?(Period.new('25.06.2015', '24.12.2015'))).to be_truthy
62
+ expect(period.include?(Period.new('26.06.2015', '24.12.2015'))).to be_truthy
63
+ expect(period.include?(Period.new('25.06.2015', '23.12.2015'))).to be_truthy
64
+ end
65
+ end
66
+
67
+ context "period not included" do
68
+ it "returns false" do
69
+ expect(period.include?(Period.new('24.06.2015', '24.12.2015'))).to be_falsey
70
+ expect(period.include?(Period.new('25.06.2015', '25.12.2015'))).to be_falsey
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require "january"
3
+
4
+ # TODO/05.03.15/04:15/tb Experimental
5
+ describe January do
6
+
7
+ let(:subject) { January.of(2015) }
8
+
9
+ it_behaves_like "Lint Check"
10
+
11
+ describe ".of" do
12
+ it "returns January of given year" do
13
+ expect(described_class.of(2014)).to eq Month.for('01.01.2014')
14
+ expect(described_class.of(2015)).to eq Month.for('01.01.2015')
15
+ end
16
+ end
17
+
18
+ describe "#next" do
19
+ it "returns next month" do
20
+ expect(described_class.of(2014).next).to eq described_class.of(2015)
21
+ expect(described_class.of(2015).next).to eq described_class.of(2016)
22
+ end
23
+ end
24
+
25
+ describe "#previous" do
26
+ it "returns previous month" do
27
+ expect(described_class.of(2014).previous).to eq described_class.of(2013)
28
+ expect(described_class.of(2015).previous).to eq described_class.of(2014)
29
+ end
30
+ end
31
+
32
+ describe "#month" do
33
+ it "returns month number" do
34
+ expect(described_class.of(2015).month).to eq 1
35
+ end
36
+ end
37
+
38
+ describe "#year" do
39
+ it "returns year number" do
40
+ expect(described_class.of(2015).year).to eq 2015
41
+ expect(described_class.of(2014).year).to eq 2014
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'periods/constants'
3
+
4
+ describe Month do
5
+
6
+ let(:subject) { described_class.for("1.1.2015") }
7
+
8
+ it_behaves_like "Lint Check"
9
+
10
+ describe ".for" do
11
+ it "returns month of given date included" do
12
+ month = described_class.for('25.06.2015')
13
+
14
+ expect(month.start_date).to eq date('01.06.2015')
15
+ expect(month.end_date).to eq date('30.06.2015')
16
+ end
17
+ end
18
+
19
+ describe "#next" do
20
+ it "returns next month" do
21
+ expect(described_class.for('25.06.2015').next).to eq described_class.for('01.07.2015')
22
+ expect(described_class.for('01.07.2015').next).to eq described_class.for('01.08.2015')
23
+ expect(described_class.for('01.12.2015').next).to eq described_class.for('01.01.2016')
24
+ end
25
+ end
26
+
27
+ describe "#previous" do
28
+ it "returns previous month" do
29
+ expect(described_class.for('01.06.2015').previous).to eq described_class.for('01.05.2015')
30
+ expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.06.2015')
31
+ expect(described_class.for('01.01.2016').previous).to eq described_class.for('01.12.2015')
32
+ end
33
+ end
34
+
35
+ describe "#month" do
36
+ it "returns month number" do
37
+ expect(described_class.for('01.01.2015').month).to eq 1
38
+ expect(described_class.for('01.06.2015').month).to eq 6
39
+ expect(described_class.for('01.12.2015').month).to eq 12
40
+ end
41
+ end
42
+
43
+ describe "#number" do
44
+ it "returns month number" do
45
+ expect(described_class.for('01.01.2015').number).to eq 1
46
+ expect(described_class.for('01.06.2015').number).to eq 6
47
+ expect(described_class.for('01.12.2015').number).to eq 12
48
+ end
49
+ end
50
+
51
+ describe "#year" do
52
+ it "returns year number" do
53
+ expect(described_class.for('01.01.2015').year).to eq 2015
54
+ expect(described_class.for('01.12.2015').year).to eq 2015
55
+ expect(described_class.for('01.01.2014').year).to eq 2014
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'periods/constants'
3
+
4
+ describe MonthlyPeriod do
5
+
6
+ let(:subject) { described_class.for("1.1.2015") }
7
+
8
+ it_behaves_like "Lint Check"
9
+
10
+ describe ".for" do
11
+ it "returns month of given date included" do
12
+ month = described_class.for('25.06.2015')
13
+
14
+ expect(month.start_date).to eq date('25.06.2015')
15
+ expect(month.end_date).to eq date('24.07.2015')
16
+ end
17
+ end
18
+
19
+ describe "#next" do
20
+ it "returns next month" do
21
+ expect(described_class.for('25.06.2015').next).to eq described_class.for('25.07.2015')
22
+ expect(described_class.for('01.07.2015').next).to eq described_class.for('01.08.2015')
23
+ expect(described_class.for('03.12.2015').next).to eq described_class.for('03.01.2016')
24
+ end
25
+ end
26
+
27
+ describe "#previous" do
28
+ it "returns previous month" do
29
+ expect(described_class.for('25.06.2015').previous).to eq described_class.for('25.05.2015')
30
+ expect(described_class.for('01.07.2015').previous).to eq described_class.for('01.06.2015')
31
+ expect(described_class.for('03.01.2016').previous).to eq described_class.for('03.12.2015')
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'periods/constants'
3
+
4
+ describe Period do
5
+
6
+ def new_period(start_date, end_date)
7
+ described_class.new(start_date, end_date)
8
+ end
9
+
10
+ describe "==" do
11
+ context "same period given" do
12
+ it "returns true" do
13
+ expect(new_period("01.02.2015", "28.02.2015") == new_period("01.02.2015", "28.02.2015")).to be_truthy
14
+ end
15
+ end
16
+
17
+ context "not same period given" do
18
+ it "returns false" do
19
+ expect(new_period("01.02.2015", "28.02.2015") == new_period("02.02.2015", "28.02.2015")).to be_falsey
20
+ expect(new_period("01.02.2015", "28.02.2015") == new_period("01.02.2015", "27.02.2015")).to be_falsey
21
+ expect(new_period("01.02.2015", "28.02.2015") == new_period("01.03.2015", "31.03.2015")).to be_falsey
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "comparing" do
27
+ it "compare correctly" do
28
+ expect(new_period("01.02.2015", "28.02.2015") >= new_period("01.02.2015", "28.02.2015")).to be_truthy
29
+ expect(new_period("01.02.2015", "28.02.2015") > new_period("01.01.2015", "31.01.2015")).to be_truthy
30
+ expect(new_period("01.02.2015", "28.02.2015") <= new_period("01.02.2015", "28.02.2015")).to be_truthy
31
+ expect(new_period("01.02.2015", "28.02.2015") < new_period("01.03.2015", "31.03.2015")).to be_truthy
32
+ end
33
+ end
34
+
35
+ describe "#next" do
36
+ it "returns next period" do
37
+ expect(new_period("25.06.2015", "19.08.2015").next).to eq new_period("20.08.2015", "14.10.2015")
38
+ expect(new_period("03.12.2015", "07.12.2015").next).to eq new_period("08.12.2015", "12.12.2015")
39
+ end
40
+ end
41
+
42
+ describe "#previous" do
43
+ it "returns previous period" do
44
+ expect(new_period("25.06.2015", "19.08.2015").previous).to eq new_period("30.04.2015", "24.06.2015")
45
+ expect(new_period("03.12.2015", "07.12.2015").previous).to eq new_period("28.11.2015", "02.12.2015")
46
+ end
47
+ end
48
+
49
+ describe "#days" do
50
+ it "returns days of period" do
51
+ expect(new_period("25.06.2015", "19.08.2015").days).to eq 56
52
+ expect(new_period("20.08.2015", "14.10.2015").days).to eq 56
53
+ expect(new_period("01.02.2015", "28.02.2015").days).to eq 28
54
+ expect(new_period("06.02.2015", "30.04.2015").days).to eq 84
55
+ end
56
+ end
57
+
58
+ describe "#include?" do
59
+ context "date included" do
60
+ it "returns true" do
61
+ expect(new_period("01.02.2015", "28.02.2015").include?("01.02.2015")).to be_truthy
62
+ expect(new_period("01.02.2015", "28.02.2015").include?("28.02.2015")).to be_truthy
63
+ end
64
+ end
65
+
66
+ context "date not included" do
67
+ it "returns false" do
68
+ expect(new_period("01.02.2015", "28.02.2015").include?("31.01.2015")).to be_falsey
69
+ expect(new_period("01.02.2015", "28.02.2015").include?("01.03.2015")).to be_falsey
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'periods/date_calculator'
3
+
4
+ module Periods
5
+ describe DateCalculator do
6
+
7
+ describe "#beginning_of_month" do
8
+ it "returns given date with first day of month" do
9
+ expect(described_class.new('01.06.2015').beginning_of_month).to eq date('01.06.2015')
10
+ expect(described_class.new('02.06.2015').beginning_of_month).to eq date('01.06.2015')
11
+ expect(described_class.new('30.06.2015').beginning_of_month).to eq date('01.06.2015')
12
+ end
13
+ end
14
+
15
+ describe "#end_of_month" do
16
+ it "returns given date with last day of month" do
17
+ expect(described_class.new('01.06.2015').end_of_month).to eq date('30.06.2015')
18
+ expect(described_class.new('02.06.2015').end_of_month).to eq date('30.06.2015')
19
+ expect(described_class.new('30.06.2015').end_of_month).to eq date('30.06.2015')
20
+ end
21
+ end
22
+ end
23
+ end
24
+