monthly 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c2be8c6c5fc118cbda20460380fc9c108667197
4
- data.tar.gz: 6870e51f2d5fb6f73410329aa2c1ee5f2eb7d788
3
+ metadata.gz: f2453c04f9094e31d3c8501aaa2d7e4d96d3b825
4
+ data.tar.gz: 7e5cf1f07f6053db57e83137aa76bcbf95043f57
5
5
  SHA512:
6
- metadata.gz: e6be8422a387d6ebf2a33e1b26b6be7f1828f722102661ff544e2a1f2c70d4b9fc46bd8d53761cf5143a317b81034e2c67c1b0172cf32f5480a5cb7476b8ba54
7
- data.tar.gz: 336d71f25b97da5c1367a2d7940e4af321a5f14a3448e710de65e095aff030b1ce0bc12abc0b5b4718edcde96209f98e20347fadcf5e2de91903007a75291e59
6
+ metadata.gz: 849ca30e014feb96378384af6a0725ef0a88c2d3bfd5ca8a553bda404083a3567e9a4140ae3fe0b33dbb75c5609e1fd160c669b8bba0361bbe21c48f205ca677
7
+ data.tar.gz: 72420ba9708f7b1f004a388aa58958627a8f6c0cad3c5e18dca6d4a77436f34c0e20b8e91bd8be484271dde480964784a08cd9f32fb7f10fb23bbb212d535f32
data/README.md CHANGED
@@ -21,6 +21,8 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
+ ### Partial Months
25
+
24
26
  If you have a monthly subscription of $1 per month, and a customer should pay for a period of 12.1.2015–4.3.2015, you can ask Monthly how many months that corresponds to:
25
27
 
26
28
  ```
@@ -30,6 +32,17 @@ days_per_month = 30 # common practice
30
32
  Monthly.partial_months(from, to, days_per_month) # 0.7333333333333333
31
33
  ```
32
34
 
35
+ ### Split period into months
36
+
37
+ ```
38
+ from = Date.parse '1.1.2015'
39
+ to = Date.parse '3.2.2015'
40
+ Monthly.periods_by_month(from, to)
41
+ # Returns:
42
+ # {:from=>#<Date: 2015-01-01 ((2457024j,0s,0n),+0s,2299161j)>, :to=>#<Date: 2015-01-31 ((2457054j,0s,0n),+0s,2299161j)>}
43
+ # {:from=>#<Date: 2015-02-01 ((2457055j,0s,0n),+0s,2299161j)>, :to=>#<Date: 2015-02-03 ((2457057j,0s,0n),+0s,2299161j)>}
44
+ ```
45
+
33
46
  ## Development
34
47
 
35
48
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,66 @@
1
+ module Monthly
2
+ class Splitter
3
+ attr_reader :from, :to
4
+
5
+ def initialize(from, to)
6
+ @from, @to = from, to
7
+ @current_from = self.from
8
+ @current_to = last_day_of_month(self.from)
9
+ end
10
+
11
+ def execute
12
+ [].tap do |out|
13
+ out << {from: self.from, to: self.to} if out_of_range?(@current_to)
14
+ while in_range?(@current_to)
15
+ out << {from: @current_from, to: @current_to}
16
+ out << {from: next_from, to: self.to} if last_month_is_partial?
17
+ set_next_from_and_to
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def in_range?(date)
25
+ date <= self.to
26
+ end
27
+
28
+ def out_of_range?(date)
29
+ !in_range?(date)
30
+ end
31
+
32
+ def last_month_is_partial?
33
+ out_of_range?(next_to) && self.to > @current_to
34
+ end
35
+
36
+ def set_next_from_and_to
37
+ set_next_from
38
+ set_next_to
39
+ end
40
+
41
+ def set_next_from
42
+ @current_from = next_from
43
+ end
44
+
45
+ def set_next_to
46
+ @current_to = next_to
47
+ end
48
+
49
+ def next_from
50
+ first_day_of_month(@current_from.next_month)
51
+ end
52
+
53
+ def next_to
54
+ last_day_of_month(@current_to.next_month)
55
+ end
56
+
57
+ def first_day_of_month(date)
58
+ Date.new(date.year, date.month, 1)
59
+ end
60
+
61
+ def last_day_of_month(date)
62
+ Date.new(date.year, date.month + 1, 1).prev_day
63
+ end
64
+
65
+ end
66
+ end
@@ -1,3 +1,3 @@
1
1
  module Monthly
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/monthly.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  require "monthly/version"
2
2
  require "monthly/calculator"
3
+ require "monthly/splitter"
3
4
 
4
5
  module Monthly
5
6
  def self.partial_months(from, to, days_per_month=30)
6
7
  calculator = Calculator.new(from, to, days_per_month)
7
8
  calculator.partial_months
8
9
  end
10
+
11
+ def self.periods_by_month(from, to)
12
+ Splitter.new(from, to).execute
13
+ end
9
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monthly
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
  - Søren Torp Petersen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-26 00:00:00.000000000 Z
11
+ date: 2015-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,7 @@ files:
73
73
  - bin/setup
74
74
  - lib/monthly.rb
75
75
  - lib/monthly/calculator.rb
76
+ - lib/monthly/splitter.rb
76
77
  - lib/monthly/version.rb
77
78
  - monthly.gemspec
78
79
  homepage: https://github.com/2rp/monthly/