datebox 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.txt +4 -0
- data/README.md +2 -2
- data/lib/datebox/relative.rb +31 -33
- data/lib/datebox/version.rb +1 -1
- data/test/unit/test_relative.rb +18 -18
- 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: 987247b0d89fd90410e93d3c0474ed9872ee3fef
|
4
|
+
data.tar.gz: 6f5f3555c722c186ba548f13728c8e6fa3e2ef44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6edab522ecc2b28c1a0b7261e2bbb85aefc7ec95f788dc235bda73795b883686d4b59cd78d7e82d214577f7abdf86965a697cac2f806e280b9a7f8faf58f74d
|
7
|
+
data.tar.gz: 0d8a4330cc658a23616e899349e54be21132dca95d1271f05e4c94cbaff5b3d07e190e10cd26d7ac8ec148d2479c3c3c9d81993782c2820a1a349cb4da8a16d6
|
data/CHANGELOG.txt
ADDED
data/README.md
CHANGED
@@ -26,7 +26,7 @@ Allows splitting periods (returns ending dates of periods)
|
|
26
26
|
|
27
27
|
It's also possible to calculate periods relative to given dates
|
28
28
|
|
29
|
-
period_month = Datebox::Relative.
|
30
|
-
preiod_week = Datebox::Relative.
|
29
|
+
period_month = Datebox::Relative.last_month.to('2013-07-09') # uses period method
|
30
|
+
preiod_week = Datebox::Relative.last(:week).to('2013-07-09') # uses peiod symbol
|
31
31
|
|
32
32
|
It's best to have a look at code & tests
|
data/lib/datebox/relative.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
module Datebox
|
2
2
|
class Relative
|
3
3
|
|
4
|
-
|
4
|
+
@period_proc = nil
|
5
|
+
|
6
|
+
def initialize(proc)
|
7
|
+
@period_proc = proc
|
8
|
+
end
|
9
|
+
|
10
|
+
def to(relative_to)
|
11
|
+
relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
|
12
|
+
@period_proc.call relative_to
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
5
16
|
|
6
|
-
def to(relative_to)
|
7
|
-
relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
|
8
|
-
@period_proc.call relative_to
|
9
|
-
end
|
10
|
-
|
11
17
|
def last(period, options = {})
|
12
18
|
raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
|
13
19
|
case period.to_sym
|
@@ -20,86 +26,78 @@ module Datebox
|
|
20
26
|
end
|
21
27
|
|
22
28
|
def same_day
|
23
|
-
|
24
|
-
self
|
29
|
+
new Proc.new {|relative_to| Period.new(relative_to, relative_to) }
|
25
30
|
end
|
26
31
|
|
27
32
|
def day_before
|
28
|
-
|
29
|
-
self
|
33
|
+
new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }
|
30
34
|
end
|
31
35
|
|
32
36
|
def last_n_days(options = {})
|
33
37
|
days = (options[:days] || options['days']).to_i
|
34
38
|
inclusive = (options[:exclusive] || options['exclusive']) ? false : true # inclusive by default
|
35
39
|
days = 1 if days.nil? || days <= 0 # days should always > 0 since it only return last x days
|
36
|
-
|
40
|
+
proc = inclusive ?
|
37
41
|
Proc.new {|relative_to| Period.new(relative_to - days + 1, relative_to) } :
|
38
42
|
Proc.new {|relative_to| Period.new(relative_to - days, relative_to - 1) }
|
39
|
-
|
43
|
+
new proc
|
40
44
|
end
|
41
45
|
|
42
46
|
def last_week(options = {})
|
43
47
|
last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
|
44
|
-
|
48
|
+
new Proc.new { |relative_to|
|
45
49
|
end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
|
46
50
|
Period.new(end_date - 6, end_date)
|
47
|
-
|
48
|
-
self
|
51
|
+
}
|
49
52
|
end
|
50
53
|
|
51
54
|
def last_weekdays_between(start_day, end_day)
|
52
|
-
|
55
|
+
new Proc.new { |relative_to|
|
53
56
|
end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == end_day }
|
54
57
|
start_date = (end_date - 7 .. end_date).to_a.find { |d| d.strftime("%A") == start_day }
|
55
58
|
Period.new(start_date, end_date)
|
56
|
-
|
57
|
-
self
|
59
|
+
}
|
58
60
|
end
|
59
61
|
|
60
62
|
def last_weeks_weekdays_as!(*days) #this one returns array!
|
61
|
-
|
63
|
+
new Proc.new { |relative_to|
|
62
64
|
days.map do |p|
|
63
65
|
(relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == p }
|
64
66
|
end
|
65
|
-
|
66
|
-
self
|
67
|
+
}
|
67
68
|
end
|
68
69
|
|
69
70
|
def last_month
|
70
|
-
|
71
|
+
new Proc.new { |relative_to|
|
71
72
|
previous_month_start = Date.parse("#{relative_to.prev_month.strftime('%Y-%m')}-01")
|
72
73
|
previous_month_end = previous_month_start.next_month - 1
|
73
74
|
Period.new(previous_month_start, previous_month_end)
|
74
|
-
|
75
|
-
self
|
75
|
+
}
|
76
76
|
end
|
77
77
|
|
78
78
|
def month_to_date
|
79
|
-
|
79
|
+
new Proc.new { |relative_to|
|
80
80
|
month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01")
|
81
81
|
Period.new(month_start, relative_to)
|
82
|
-
|
83
|
-
self
|
82
|
+
}
|
84
83
|
end
|
85
84
|
|
86
85
|
def last_year
|
87
|
-
|
86
|
+
new Proc.new { |relative_to|
|
88
87
|
previous_year_start = Date.parse("#{relative_to.prev_year.year}-01-01")
|
89
88
|
previous_year_end = previous_year_start.next_year - 1
|
90
89
|
Period.new(previous_year_start, previous_year_end)
|
91
|
-
|
92
|
-
self
|
90
|
+
}
|
93
91
|
end
|
94
92
|
|
95
93
|
def year_to_date
|
96
|
-
|
94
|
+
new Proc.new { |relative_to|
|
97
95
|
year_start = Date.parse("#{relative_to.year}-01-01")
|
98
96
|
Period.new(year_start, relative_to)
|
99
|
-
|
100
|
-
self
|
97
|
+
}
|
101
98
|
end
|
102
99
|
|
100
|
+
end
|
103
101
|
end
|
104
102
|
|
105
103
|
end
|
data/lib/datebox/version.rb
CHANGED
data/test/unit/test_relative.rb
CHANGED
@@ -4,36 +4,36 @@ class TestRelative < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def test_calculates_correctly
|
6
6
|
# day
|
7
|
-
assert_equal [Date.parse('2013-07-07')], Datebox::Relative.
|
8
|
-
assert_equal [Date.parse('2013-07-06')], Datebox::Relative.
|
7
|
+
assert_equal [Date.parse('2013-07-07')], Datebox::Relative.same_day.to('2013-07-07').dates
|
8
|
+
assert_equal [Date.parse('2013-07-06')], Datebox::Relative.day_before.to('2013-07-07').dates
|
9
9
|
|
10
10
|
# n days
|
11
|
-
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.
|
12
|
-
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.
|
11
|
+
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {:days => 30}).to('2014-06-30')
|
12
|
+
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {days: 30, exclusive: true}).to('2014-07-01')
|
13
13
|
|
14
14
|
# week
|
15
|
-
assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.
|
16
|
-
assert_equal Datebox::Period.new('2013-06-30', '2013-07-06'), Datebox::Relative.
|
17
|
-
assert_equal Datebox::Period.new('2013-07-01', '2013-07-05'), Datebox::Relative.
|
18
|
-
assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.
|
15
|
+
assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.last_week.to('2013-07-09') # tue
|
16
|
+
assert_equal Datebox::Period.new('2013-06-30', '2013-07-06'), Datebox::Relative.last_week({:last_weekday => "Saturday"}).to('2013-07-09') #tue
|
17
|
+
assert_equal Datebox::Period.new('2013-07-01', '2013-07-05'), Datebox::Relative.last_weekdays_between("Monday", "Friday").to('2013-07-09') # tue
|
18
|
+
assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.last_weekdays_between("Monday", "Tuesday").to('2013-07-09') #tue
|
19
19
|
|
20
20
|
# month
|
21
|
-
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.
|
22
|
-
assert_equal Datebox::Period.new('2013-07-01', '2013-07-09'), Datebox::Relative.
|
23
|
-
assert_equal Datebox::Period.new('2013-02-01', '2013-02-28'), Datebox::Relative.
|
21
|
+
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.last_month.to('2013-07-09')
|
22
|
+
assert_equal Datebox::Period.new('2013-07-01', '2013-07-09'), Datebox::Relative.month_to_date.to('2013-07-09')
|
23
|
+
assert_equal Datebox::Period.new('2013-02-01', '2013-02-28'), Datebox::Relative.last(:month).to('2013-03-02')
|
24
24
|
|
25
25
|
# year
|
26
|
-
assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.
|
27
|
-
assert_equal Datebox::Period.new('2013-01-01', '2013-07-09'), Datebox::Relative.
|
26
|
+
assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.last_year.to('2013-07-09')
|
27
|
+
assert_equal Datebox::Period.new('2013-01-01', '2013-07-09'), Datebox::Relative.year_to_date.to('2013-07-09')
|
28
28
|
|
29
29
|
# anything
|
30
|
-
assert_equal Datebox::Period.new('2013-06-01', '2013-06-01'), Datebox::Relative.
|
31
|
-
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.
|
32
|
-
assert_equal Datebox::Period.new('2014-02-03', '2014-02-09'), Datebox::Relative.
|
33
|
-
assert_equal Datebox::Period.new('2014-02-02', '2014-02-08'), Datebox::Relative.
|
30
|
+
assert_equal Datebox::Period.new('2013-06-01', '2013-06-01'), Datebox::Relative.last(:day).to('2013-06-02')
|
31
|
+
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.last(:month).to('2013-07-09')
|
32
|
+
assert_equal Datebox::Period.new('2014-02-03', '2014-02-09'), Datebox::Relative.last(:week).to('2014-02-10')
|
33
|
+
assert_equal Datebox::Period.new('2014-02-02', '2014-02-08'), Datebox::Relative.last(:week, {:last_weekday => "Saturday"}).to('2014-02-10')
|
34
34
|
|
35
35
|
# the one that's different
|
36
|
-
assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.
|
36
|
+
assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.last_weeks_weekdays_as!("Monday", "Wednesday").to('2013-07-05') #fri
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datebox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Borkowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Offers help with managing dates and periods
|
14
14
|
email:
|
@@ -18,6 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- .gitignore
|
21
|
+
- CHANGELOG.txt
|
21
22
|
- Gemfile
|
22
23
|
- LICENSE
|
23
24
|
- README.md
|