datebox 0.0.2
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 +7 -0
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +32 -0
- data/Rakefile +1 -0
- data/datebox.gemspec +23 -0
- data/lib/datebox.rb +7 -0
- data/lib/datebox/period.rb +55 -0
- data/lib/datebox/relative.rb +79 -0
- data/lib/datebox/version.rb +3 -0
- data/test/test_helper.rb +5 -0
- data/test/unit/test_period.rb +23 -0
- data/test/unit/test_relative.rb +23 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ee3f21eef1adfc455da526dbbfd037f2b17e1d9
|
4
|
+
data.tar.gz: 2760bf9e09bf08a526df3622eeab16f3ada22247
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3cc78e68d9eacec4962e39137bd17df8d64156e2dc888b1a99e9c8f1da20d5742cef5f5a27de94ca43798da466a623836afc38e1853cff4fed8e053d7a6b5d53
|
7
|
+
data.tar.gz: 15c4049a449cf389c75adf6c41d2ce17583effb1a12720dc5b6fa7caa685ca4469ad8e13302129c70749c04ee10e80d5b8ae44c5256f6a3af917f509cb1529a6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Forward3D
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Datebox
|
2
|
+
|
3
|
+
Provides help with managing dates and periods
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install datebox
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Include gem in Gemfile
|
12
|
+
|
13
|
+
gem 'datebox'
|
14
|
+
# or
|
15
|
+
gem 'datebox', :git => 'git@github.com:forward3d/datebox'
|
16
|
+
|
17
|
+
Allows using periods
|
18
|
+
|
19
|
+
period = Datebox::Period.new("2013-06-10", "2013-06-27")
|
20
|
+
period.from
|
21
|
+
period.to
|
22
|
+
|
23
|
+
Allows splitting periods (returns ending dates of periods)
|
24
|
+
|
25
|
+
Datebox::Period.split_dates(Date.parse("2013-06-14"), Date.parse("2013-06-27"), "week")
|
26
|
+
|
27
|
+
It's also possible to calculate periods relative to given dates
|
28
|
+
|
29
|
+
period_month = Datebox::Relative.new.last_month.to('2013-07-09')
|
30
|
+
preiod_week = Datebox::Relative.new.last_week.to('2013-07-09')
|
31
|
+
|
32
|
+
It's best to have a look at code & tests
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/datebox.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'datebox'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "datebox"
|
8
|
+
gem.version = Datebox::VERSION
|
9
|
+
gem.authors = ["Robert Borkowski"]
|
10
|
+
gem.email = ["robert.borkowski@forward3d.com"]
|
11
|
+
gem.description = %q{Offers help with managing dates and periods}
|
12
|
+
gem.summary = %q{Got fed up with implementing date related functionality everywhere}
|
13
|
+
gem.homepage = "https://github.com/forward3d/datebox"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = [] #gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = [] #gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
# gem.test_files = `git grep test`.split(/\n/).map {|f| File.basename(f.split(':').first)}
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.license = 'MIT'
|
22
|
+
|
23
|
+
end
|
data/lib/datebox.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Datebox
|
2
|
+
class Period
|
3
|
+
attr_reader :from, :to
|
4
|
+
|
5
|
+
def initialize(from, to)
|
6
|
+
@from = from.is_a?(Date) ? from : Date.parse(from)
|
7
|
+
@to = to.is_a?(Date) ? to : Date.parse(to)
|
8
|
+
raise "FROM date should not be later than TO date" if @to < @from
|
9
|
+
end
|
10
|
+
|
11
|
+
def dates
|
12
|
+
(@from..@to).to_a
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
@from == other.from && @to == other.to
|
17
|
+
end
|
18
|
+
|
19
|
+
def split_dates(period, options = {})
|
20
|
+
self.class.split_dates(from, to, period, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
def split_dates(start_date, end_date, period, options = {})
|
25
|
+
return (start_date..end_date).to_a if period == "day"
|
26
|
+
return split_weekly_dates(start_date, end_date, options) if period == "week"
|
27
|
+
return split_monthly_dates(start_date, end_date) if period == "month"
|
28
|
+
end
|
29
|
+
|
30
|
+
def split_weekly_dates(start_date, end_date, options = {})
|
31
|
+
last_weekday = options[:last_weekday] || "Sunday"
|
32
|
+
end_dates = []
|
33
|
+
end_of_week = (end_date.downto end_date - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
|
34
|
+
while end_of_week - 6 >= start_date
|
35
|
+
end_dates << end_of_week
|
36
|
+
end_of_week -= 7
|
37
|
+
end
|
38
|
+
end_dates.sort
|
39
|
+
end
|
40
|
+
|
41
|
+
def split_monthly_dates(start_date, end_date)
|
42
|
+
end_dates = []
|
43
|
+
beginning_of_month = ::Date.parse("#{end_date.year}-#{end_date.month}-01").next_month
|
44
|
+
end_of_month = (beginning_of_month - 1 == end_date) ? end_date : beginning_of_month.prev_month - 1
|
45
|
+
while beginning_of_month.prev_month >= start_date
|
46
|
+
end_dates << end_of_month
|
47
|
+
beginning_of_month = ::Date.parse("#{end_of_month.year}-#{end_of_month.month}-01")
|
48
|
+
end_of_month = beginning_of_month - 1
|
49
|
+
end
|
50
|
+
end_dates.sort
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Datebox
|
2
|
+
class Relative
|
3
|
+
|
4
|
+
@period = nil
|
5
|
+
@period_proc = nil
|
6
|
+
|
7
|
+
def to(relative_to)
|
8
|
+
relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
|
9
|
+
@period_proc.call relative_to
|
10
|
+
end
|
11
|
+
|
12
|
+
def same_day
|
13
|
+
@period_proc = Proc.new {|relative_to| Period.new(relative_to, relative_to) }
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def last_week(last_weekday = "Sunday")
|
18
|
+
@period_proc = Proc.new do |relative_to|
|
19
|
+
end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
|
20
|
+
Period.new(end_date - 6, end_date)
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def last_weekdays_between(start_day, end_day)
|
26
|
+
@period_proc = Proc.new do |relative_to|
|
27
|
+
end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == end_day }
|
28
|
+
start_date = (end_date - 7 .. end_date).to_a.find { |d| d.strftime("%A") == start_day }
|
29
|
+
Period.new(start_date, end_date)
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def last_weeks_weekdays_as!(*days) #this one returns array!
|
35
|
+
@period_proc = Proc.new do |relative_to|
|
36
|
+
days.map do |p|
|
37
|
+
(relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == p }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def last_month
|
44
|
+
@period_proc = Proc.new do |relative_to|
|
45
|
+
previous_month_start = Date.parse("#{relative_to.prev_month.strftime('%Y-%m')}-01")
|
46
|
+
previous_month_end = previous_month_start.next_month - 1
|
47
|
+
Period.new(previous_month_start, previous_month_end)
|
48
|
+
end
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
def month_to_date
|
53
|
+
@period_proc = Proc.new do |relative_to|
|
54
|
+
month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01")
|
55
|
+
Period.new(month_start, relative_to)
|
56
|
+
end
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def last_year
|
61
|
+
@period_proc = Proc.new do |relative_to|
|
62
|
+
previous_year_start = Date.parse("#{relative_to.prev_year.year}-01-01")
|
63
|
+
previous_year_end = previous_year_start.next_year - 1
|
64
|
+
Period.new(previous_year_start, previous_year_end)
|
65
|
+
end
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def year_to_date
|
70
|
+
@period_proc = Proc.new do |relative_to|
|
71
|
+
year_start = Date.parse("#{relative_to.year}-01-01")
|
72
|
+
Period.new(year_start, relative_to)
|
73
|
+
end
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require '../test_helper'
|
2
|
+
|
3
|
+
class TestPeriod < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_splits_periods_correctly
|
6
|
+
#day
|
7
|
+
assert_equal [Date.today - 1], Datebox::Period.split_dates(Date.today - 1, Date.today - 1, "day")
|
8
|
+
assert_equal [Date.today - 3, Date.today - 2, Date.today - 1], Datebox::Period.split_dates(Date.today - 3, Date.today - 1, "day")
|
9
|
+
#week
|
10
|
+
assert_equal [], Datebox::Period.split_dates(Date.parse("2013-06-15"), Date.parse("2013-06-22"), "week") #sat to sat
|
11
|
+
assert_equal [Date.parse("2013-06-23")], Datebox::Period.split_dates(Date.parse("2013-06-14"), Date.parse("2013-06-27"), "week") #fri to thu
|
12
|
+
assert_equal [Date.parse("2013-06-23")], Datebox::Period.split_dates(Date.parse("2013-06-17"), Date.parse("2013-06-23"), "week") #mon to sun
|
13
|
+
assert_equal [Date.parse("2013-06-16"), Date.parse("2013-06-23")], Datebox::Period.split_dates(Date.parse("2013-06-10"), Date.parse("2013-06-27"), "week") #mon to thu
|
14
|
+
#month
|
15
|
+
assert_equal [], Datebox::Period.split_dates(Date.parse("2013-01-02"), Date.parse("2013-01-31"), "month")
|
16
|
+
assert_equal [Date.parse("2013-01-31")], Datebox::Period.split_dates(Date.parse("2013-01-01"), Date.parse("2013-01-31"), "month")
|
17
|
+
assert_equal [Date.parse("2013-01-31")], Datebox::Period.split_dates(Date.parse("2012-12-02"), Date.parse("2013-02-02"), "month")
|
18
|
+
assert_equal [Date.parse("2013-01-31"), Date.parse("2013-02-28")], Datebox::Period.split_dates(Date.parse("2012-12-10"), Date.parse("2013-03-03"), "month")
|
19
|
+
#instance method test
|
20
|
+
assert_equal [Date.parse("2013-06-16"), Date.parse("2013-06-23")], Datebox::Period.new("2013-06-10", "2013-06-27").split_dates("week") #mon to thu
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require '../test_helper'
|
2
|
+
|
3
|
+
class TestRelative < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_calculates_correctly
|
6
|
+
# day
|
7
|
+
assert_equal [Date.parse('2013-07-07')], Datebox::Relative.new.same_day.to('2013-07-07').dates
|
8
|
+
# week
|
9
|
+
assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.new.last_week.to('2013-07-09') #tue
|
10
|
+
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
|
+
assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.new.last_weekdays_between("Monday", "Tuesday").to('2013-07-09') #tue
|
12
|
+
# month
|
13
|
+
assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last_month.to('2013-07-09')
|
14
|
+
assert_equal Datebox::Period.new('2013-07-01', '2013-07-09'), Datebox::Relative.new.month_to_date.to('2013-07-09')
|
15
|
+
# year
|
16
|
+
assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.new.last_year.to('2013-07-09')
|
17
|
+
assert_equal Datebox::Period.new('2013-01-01', '2013-07-09'), Datebox::Relative.new.year_to_date.to('2013-07-09')
|
18
|
+
|
19
|
+
# the one that's different
|
20
|
+
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
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datebox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Borkowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Offers help with managing dates and periods
|
14
|
+
email:
|
15
|
+
- robert.borkowski@forward3d.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- datebox.gemspec
|
26
|
+
- lib/datebox.rb
|
27
|
+
- lib/datebox/period.rb
|
28
|
+
- lib/datebox/relative.rb
|
29
|
+
- lib/datebox/version.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/unit/test_period.rb
|
32
|
+
- test/unit/test_relative.rb
|
33
|
+
homepage: https://github.com/forward3d/datebox
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata: {}
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 2.0.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: Got fed up with implementing date related functionality everywhere
|
57
|
+
test_files: []
|