span 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzVhYjA4MDBjNmExMDRmODlkNzU4MjAwMjRiMmYyMzZlZWVkODkxYQ==
4
+ NWM2ZmU3YzBkZTdmMjI2MGYyYzkyMTBkOGE1N2MwMGJiZmEzMmZmNg==
5
5
  data.tar.gz: !binary |-
6
- YmEyMDhhNjk2YTMyYjlmMTNjMDI1MmU5MDIyYmU2OGUxYWQyMDU3NQ==
6
+ OWI3ZWFiYjY1ZTU0YTI0YTE5YmUzNWEzM2QyNjU3MDZhYzI2ZjI0Ng==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- M2NkZTgyZjM2NTg1Y2U5NjI5Mzc5ZGIzOWEyN2UyMDY0YzczY2IzMzMzYWZi
10
- NWYxMDM2ODczZWVjZGQ5MGI1MWY3NDUwN2Y4ODcxYzhkZGQxY2E4NmQ3MjI5
11
- MDBkNTgwMGExNTdjZWM0MDZmZmQwMGRkMTRiYWZjNGY2YmE0MjY=
9
+ OGExM2M5ZWQ3YWQ1ODg5ZGU5ZTkwMGEwZGM4ZTY1Y2MxOThhNTM4NTA2YzBh
10
+ ZGViOGFiOWE1ODQ3ZmU5N2IyZTRjMzNjNjBkY2JlMDVhOGI5ODRlOGJhMmMw
11
+ MTlkNzFiYTYxZTk4ZTk5YzY2ODM0MWI0NGFiODBkYzU2YWI0OTY=
12
12
  data.tar.gz: !binary |-
13
- MjdmMTYwZTBjMzQ4ZmZjNmNhMGU1YzUyYzgzYjY3ODU5MzMwNmEyYTFjOTE0
14
- ZjIzY2EyOWQ5MmU5ZGU2YmYzNDFmNzllMjdjNmUyYjZiODJmNDY2ZWU1ZjNj
15
- YmI5MzkyYWEwMDU1ZDViODEyYzliODQwOTU3YzJkNGU1Yjg3YjM=
13
+ MmYyYjZiZmFkNWUwY2Y5MjBmM2ZmMDFmN2FjZDRmOWJjMjUxMmE0MTQ0NzYy
14
+ YjczOGRlZGM2ZjFmOTQ0ZDg0ZDAzYzA4ZDY0ZmE0OGJjZTE3MTFlZDViY2Zi
15
+ M2I5NzhiNzc1MGRhNTM0ZTk5ZTU1OGNmNzkxZjFjNzIzMDE3YmM=
@@ -0,0 +1,121 @@
1
+ require 'date'
2
+
3
+ module Span
4
+ class DateDiff
5
+ attr_reader \
6
+ :end_date,
7
+ :start_date
8
+
9
+ def self.compute( *args , &block )
10
+ new( *args , &block ).to_hash
11
+ end
12
+
13
+ def initialize( end_date_input , start_date_input )
14
+ @end_date = end_date_input.to_date
15
+ @start_date = start_date_input.to_date
16
+ end
17
+
18
+ def build_diff_hash
19
+ check_from = start_date
20
+
21
+ [ :years , :months , :weeks , :days ].reduce Hash.new do | memo , part |
22
+ check_from , num_of_part , part_singular =
23
+ send \
24
+ :"diff_#{ part }",
25
+ check_from,
26
+ part
27
+
28
+ memo[ part_singular ] = num_of_part
29
+ memo
30
+ end
31
+ end
32
+
33
+ def default_diff( check_from , part , part_singular )
34
+ num_of_part = 0
35
+ part_next_method = :"next_#{ part_singular }"
36
+
37
+ loop do
38
+ check = check_from.send part_next_method
39
+
40
+ break if check > end_date
41
+
42
+ check_from = check
43
+ num_of_part += 1
44
+ end
45
+
46
+ [ check_from , num_of_part , part_singular ]
47
+ end
48
+
49
+ def diff_days( *args )
50
+ default_diff *args , :day
51
+ end
52
+
53
+ def diff_months( check_from , * )
54
+ num_of_part = 0
55
+ part_singular = :month
56
+ part_next_method = :"next_#{ part_singular }"
57
+
58
+ loop do
59
+ check = check_from.send part_next_method
60
+
61
+ if check > end_date
62
+ if start_date.day != check_from.day
63
+ day = start_date.strftime '%d'
64
+ month = check_from.strftime '%m'
65
+ year = check_from.strftime '%Y'
66
+
67
+ date_to_try =
68
+ begin
69
+ Date.parse "#{ day }/#{ month }/#{ year }"
70
+ rescue ArgumentError
71
+ false
72
+ end
73
+
74
+ check_from =
75
+ if date_to_try
76
+ date_to_try
77
+ else
78
+ next_month = check_from.next_month.strftime '%m'
79
+ Date.parse( "01/#{ next_month }/#{ year }" ).prev_day
80
+ end
81
+ end
82
+
83
+ break
84
+ end
85
+
86
+ check_from = check
87
+ num_of_part += 1
88
+ end
89
+
90
+ [ check_from , num_of_part , part_singular ]
91
+ end
92
+
93
+ def diff_weeks( check_from , * )
94
+ num_of_part = 0
95
+ part_singular = :week
96
+ part_next_method = :next_day
97
+ check_from = check_from.prev_day
98
+
99
+ loop do
100
+ check = check_from.send part_next_method
101
+ 6.times { check = check.send part_next_method }
102
+
103
+ break if check > end_date
104
+
105
+ check_from = check
106
+ num_of_part += 1
107
+ end
108
+
109
+ [ check_from , num_of_part , part_singular ]
110
+ end
111
+
112
+ def diff_years( *args )
113
+ default_diff *args , :year
114
+ end
115
+
116
+ def to_hash
117
+ @_hash ||= build_diff_hash
118
+ end
119
+ end
120
+ end
121
+
data/lib/span/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Span
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/span.rb CHANGED
@@ -1,126 +1,8 @@
1
- require 'date'
2
-
1
+ require 'span/date_diff'
3
2
  require 'span/version'
4
3
 
5
4
  module Span
6
5
  def self.compute( *args , &block )
7
- TimeDiff.compute *args , &block
8
- end
9
-
10
- class TimeDiff
11
- attr_reader \
12
- :end_date,
13
- :start_date
14
-
15
- def self.compute( *args , &block )
16
- new( *args , &block ).to_hash
17
- end
18
-
19
- def initialize( end_date_input , start_date_input )
20
- @end_date = end_date_input.to_date
21
- @start_date = start_date_input.to_date
22
- end
23
-
24
- def build_diff_hash
25
- check_from = start_date
26
-
27
- [ :years , :months , :weeks , :days ].reduce Hash.new do | memo , part |
28
- check_from , num_of_part , part_singular =
29
- send \
30
- :"diff_#{ part }",
31
- check_from,
32
- part
33
-
34
- memo[ part_singular ] = num_of_part
35
- memo
36
- end
37
- end
38
-
39
- def default_diff( check_from , part , part_singular )
40
- num_of_part = 0
41
- part_next_method = :"next_#{ part_singular }"
42
-
43
- loop do
44
- check = check_from.send part_next_method
45
-
46
- break if check > end_date
47
-
48
- check_from = check
49
- num_of_part += 1
50
- end
51
-
52
- [ check_from , num_of_part , part_singular ]
53
- end
54
-
55
- def diff_days( *args )
56
- default_diff *args , :day
57
- end
58
-
59
- def diff_months( check_from , * )
60
- num_of_part = 0
61
- part_singular = :month
62
- part_next_method = :"next_#{ part_singular }"
63
-
64
- loop do
65
- check = check_from.send part_next_method
66
-
67
- if check > end_date
68
- if start_date.day != check_from.day
69
- day = start_date.strftime '%d'
70
- month = check_from.strftime '%m'
71
- year = check_from.strftime '%Y'
72
-
73
- date_to_try =
74
- begin
75
- Date.parse "#{ day }/#{ month }/#{ year }"
76
- rescue ArgumentError
77
- false
78
- end
79
-
80
- check_from =
81
- if date_to_try
82
- date_to_try
83
- else
84
- next_month = check_from.next_month.strftime '%m'
85
- Date.parse( "01/#{ next_month }/#{ year }" ).prev_day
86
- end
87
- end
88
-
89
- break
90
- end
91
-
92
- check_from = check
93
- num_of_part += 1
94
- end
95
-
96
- [ check_from , num_of_part , part_singular ]
97
- end
98
-
99
- def diff_weeks( check_from , * )
100
- num_of_part = 0
101
- part_singular = :week
102
- part_next_method = :next_day
103
- check_from = check_from.prev_day
104
-
105
- loop do
106
- check = check_from.send part_next_method
107
- 6.times { check = check.send part_next_method }
108
-
109
- break if check > end_date
110
-
111
- check_from = check
112
- num_of_part += 1
113
- end
114
-
115
- [ check_from , num_of_part , part_singular ]
116
- end
117
-
118
- def diff_years( *args )
119
- default_diff *args , :year
120
- end
121
-
122
- def to_hash
123
- @_hash ||= build_diff_hash
124
- end
6
+ DateDiff.compute *args , &block
125
7
  end
126
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: span
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Cook
@@ -65,6 +65,7 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/span.rb
68
+ - lib/span/date_diff.rb
68
69
  - lib/span/version.rb
69
70
  - span.gemspec
70
71
  - test/span_test.rb