monthly 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 +4 -4
- data/README.md +13 -0
- data/lib/monthly/splitter.rb +66 -0
- data/lib/monthly/version.rb +1 -1
- data/lib/monthly.rb +5 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2453c04f9094e31d3c8501aaa2d7e4d96d3b825
|
4
|
+
data.tar.gz: 7e5cf1f07f6053db57e83137aa76bcbf95043f57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/monthly/version.rb
CHANGED
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.
|
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-
|
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/
|