datebox 0.0.2 → 0.0.3
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/lib/datebox/relative.rb +16 -0
- data/lib/datebox/version.rb +1 -1
- data/test/unit/test_period.rb +1 -1
- data/test/unit/test_relative.rb +9 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 534c7045625091172d8375d28ad40df3da50fda6
|
4
|
+
data.tar.gz: ff7ce2e3dca7fb342c1427b68d0e16f66b0ba7ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1780507857dd3e510748945757e95b5231280f786b0d3060c372369497e426bd5743599b1b5ad2f85d3d196a9a7fc1a995bfd6af6591e9414884c64d4af3d6d9
|
7
|
+
data.tar.gz: 045aa40f4ab00fab930882380dd2ac16651fb72dace765a128fee17e10067627ea5c7c0129baef20bc4147d3429b065487a6ce924f212a4d49937ed83706da4a
|
data/lib/datebox/relative.rb
CHANGED
@@ -8,12 +8,28 @@ module Datebox
|
|
8
8
|
relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
|
9
9
|
@period_proc.call relative_to
|
10
10
|
end
|
11
|
+
|
12
|
+
def last(period)
|
13
|
+
periods = [:day, :week, :month, :year]
|
14
|
+
raise "Expected one of: #{periods}" unless periods.include?(period)
|
15
|
+
case period
|
16
|
+
when :day then day_before
|
17
|
+
when :week then last_week
|
18
|
+
when :month then last_month
|
19
|
+
when :year then last_year
|
20
|
+
end
|
21
|
+
end
|
11
22
|
|
12
23
|
def same_day
|
13
24
|
@period_proc = Proc.new {|relative_to| Period.new(relative_to, relative_to) }
|
14
25
|
self
|
15
26
|
end
|
16
27
|
|
28
|
+
def day_before
|
29
|
+
@period_proc = Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
17
33
|
def last_week(last_weekday = "Sunday")
|
18
34
|
@period_proc = Proc.new do |relative_to|
|
19
35
|
end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
|
data/lib/datebox/version.rb
CHANGED
data/test/unit/test_period.rb
CHANGED
data/test/unit/test_relative.rb
CHANGED
@@ -1,21 +1,29 @@
|
|
1
|
-
require '
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
2
|
|
3
3
|
class TestRelative < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_calculates_correctly
|
6
6
|
# day
|
7
7
|
assert_equal [Date.parse('2013-07-07')], Datebox::Relative.new.same_day.to('2013-07-07').dates
|
8
|
+
assert_equal [Date.parse('2013-07-06')], Datebox::Relative.new.day_before.to('2013-07-07').dates
|
9
|
+
|
8
10
|
# week
|
9
11
|
assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.new.last_week.to('2013-07-09') #tue
|
10
12
|
assert_equal Datebox::Period.new('2013-07-01', '2013-07-05'), Datebox::Relative.new.last_weekdays_between("Monday", "Friday").to('2013-07-09') #tue
|
11
13
|
assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.new.last_weekdays_between("Monday", "Tuesday").to('2013-07-09') #tue
|
14
|
+
|
12
15
|
# month
|
13
16
|
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last_month.to('2013-07-09')
|
14
17
|
assert_equal Datebox::Period.new('2013-07-01', '2013-07-09'), Datebox::Relative.new.month_to_date.to('2013-07-09')
|
18
|
+
|
15
19
|
# year
|
16
20
|
assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.new.last_year.to('2013-07-09')
|
17
21
|
assert_equal Datebox::Period.new('2013-01-01', '2013-07-09'), Datebox::Relative.new.year_to_date.to('2013-07-09')
|
18
22
|
|
23
|
+
# anything
|
24
|
+
assert_equal Datebox::Period.new('2013-06-01', '2013-06-01'), Datebox::Relative.new.last(:day).to('2013-06-02')
|
25
|
+
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last(:month).to('2013-07-09')
|
26
|
+
|
19
27
|
# the one that's different
|
20
28
|
assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.new.last_weeks_weekdays_as!("Monday", "Wednesday").to('2013-07-05') #fri
|
21
29
|
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.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Borkowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Offers help with managing dates and periods
|
14
14
|
email:
|