date_ext 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/date_ext.gemspec +60 -0
- data/init.rb +1 -0
- data/lib/core_ext.rb +3 -0
- data/lib/date_ext.rb +3 -0
- data/lib/month.rb +143 -0
- data/lib/weekday.rb +53 -0
- data/test/month_test.rb +132 -0
- data/test/test_helper.rb +13 -0
- data/test/weekday_test.rb +101 -0
- metadata +81 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 hasclass
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= date_extensions
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2009 hasclass. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "date_ext"
|
8
|
+
gem.summary = %Q{date extensions helps you work with weekdays, workdays, holidays, months, weeks, etc.}
|
9
|
+
gem.description = %Q{Ruby classes for weekday, month, etc.}
|
10
|
+
gem.email = "sebastian.burkhard@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/hasclass/test"
|
12
|
+
gem.authors = ["hasclass"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
test.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/,features\/ --aggregate coverage.data}
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "test #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
data/date_ext.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{date_ext}
|
8
|
+
s.version = "0.0.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["hasclass"]
|
12
|
+
s.date = %q{2009-12-09}
|
13
|
+
s.description = %q{Ruby classes for weekday, month, etc.}
|
14
|
+
s.email = %q{sebastian.burkhard@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"date_ext.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"lib/core_ext.rb",
|
29
|
+
"lib/date_ext.rb",
|
30
|
+
"lib/month.rb",
|
31
|
+
"lib/weekday.rb",
|
32
|
+
"test/month_test.rb",
|
33
|
+
"test/test_helper.rb",
|
34
|
+
"test/weekday_test.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/hasclass/test}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{date extensions helps you work with weekdays, workdays, holidays, months, weeks, etc.}
|
41
|
+
s.test_files = [
|
42
|
+
"test/month_test.rb",
|
43
|
+
"test/test_helper.rb",
|
44
|
+
"test/weekday_test.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'date_ext'
|
data/lib/core_ext.rb
ADDED
data/lib/date_ext.rb
ADDED
data/lib/month.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
# == Synopsis
|
2
|
+
#
|
3
|
+
# Representation of a month
|
4
|
+
#
|
5
|
+
# == Usage
|
6
|
+
#
|
7
|
+
# m = Month.new(2009,2)
|
8
|
+
# m = m + 1#
|
9
|
+
# m.to_s -> "2009-03"
|
10
|
+
#
|
11
|
+
# m1 = Month.new(2009,2)
|
12
|
+
# m2 = Month.new(2009,12)
|
13
|
+
# (m1..m2).to_a -> [<Month: 2009-02>, .. <Month: 2009-12>]
|
14
|
+
#
|
15
|
+
# d = Date.new(2009,8,6)
|
16
|
+
# d.to_month -> <Month: 2009-08>
|
17
|
+
#
|
18
|
+
#
|
19
|
+
# == Author
|
20
|
+
# S Burkhard
|
21
|
+
#
|
22
|
+
# == Version
|
23
|
+
# 0.1
|
24
|
+
class Month
|
25
|
+
include Comparable
|
26
|
+
|
27
|
+
attr_accessor :year, :month
|
28
|
+
|
29
|
+
# Month.new(2009,2)
|
30
|
+
#
|
31
|
+
# Month.new("2009","2")
|
32
|
+
#
|
33
|
+
# Month.new(Date.new(2009,2,3))
|
34
|
+
def initialize(date_or_year, month = nil)
|
35
|
+
year = date_or_year
|
36
|
+
if date_or_year.is_a? Date
|
37
|
+
year = date_or_year.year
|
38
|
+
month = date_or_year.month
|
39
|
+
end
|
40
|
+
@year = year.to_i
|
41
|
+
@month = month.to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
# If _months_ is a Numeric value, create a new Month object that is x months earlier than the current one.
|
46
|
+
#
|
47
|
+
# If _months_ is not a Numeric, a TypeError is raised.
|
48
|
+
def -(months)
|
49
|
+
raise TypeError, 'expected numeric' unless months.is_a?(Numeric)
|
50
|
+
d = first_day << months
|
51
|
+
d.to_month
|
52
|
+
end
|
53
|
+
|
54
|
+
# If _months_ is a Numeric value, create a new Month object that is x months later than the current one.
|
55
|
+
#
|
56
|
+
# If _months_ is not a Numeric, a TypeError is raised.
|
57
|
+
def +(months)
|
58
|
+
raise TypeError, 'expected numeric' unless months.is_a?(Numeric)
|
59
|
+
d = first_day >> months
|
60
|
+
d.to_month
|
61
|
+
end
|
62
|
+
|
63
|
+
# Formats current month according to the directives in the given format string.
|
64
|
+
def strftime(string)
|
65
|
+
first_day.strftime(string)
|
66
|
+
end
|
67
|
+
|
68
|
+
# alias to strftime.
|
69
|
+
# default format string: %Y-%m
|
70
|
+
def to_s(string = "%m/%y")
|
71
|
+
strftime(string)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Creates a new Month object that is 1 month later than the current one
|
75
|
+
def next
|
76
|
+
self + 1
|
77
|
+
end
|
78
|
+
|
79
|
+
# Creates a new Month object that is 1 month earlier than the current one
|
80
|
+
def previous
|
81
|
+
self - 1
|
82
|
+
end
|
83
|
+
|
84
|
+
# alias to next
|
85
|
+
def succ
|
86
|
+
self.next
|
87
|
+
end
|
88
|
+
|
89
|
+
# Compare this month with another month.
|
90
|
+
def <=>(other)
|
91
|
+
self.first_day <=> other.first_day
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the last weekday (everyday except saturday or sunday) of the current month.
|
95
|
+
def last_cday
|
96
|
+
date = last_day
|
97
|
+
while date.cwday > 5
|
98
|
+
date = date -= 1
|
99
|
+
end
|
100
|
+
date
|
101
|
+
end
|
102
|
+
|
103
|
+
# Returns the first weekday (everyday except saturday or sunday) of the current month.
|
104
|
+
def first_cday
|
105
|
+
date = first_day
|
106
|
+
while date.cwday > 5
|
107
|
+
date = date += 1
|
108
|
+
end
|
109
|
+
date
|
110
|
+
end
|
111
|
+
|
112
|
+
# Returns the first commercial weekday with the given weekday number
|
113
|
+
def first_cwday(wday)
|
114
|
+
date = first_day
|
115
|
+
while date.cwday != wday
|
116
|
+
date = date += 1
|
117
|
+
end
|
118
|
+
date
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the last day of current month as a Date object.
|
122
|
+
def last_day
|
123
|
+
first_of_next_month = first_day + 32
|
124
|
+
Date.new(first_of_next_month.year, first_of_next_month.month, 1) - 1 # back off one day from first of that month
|
125
|
+
end
|
126
|
+
|
127
|
+
# Returns the first day of current month as a Date object.
|
128
|
+
def first_day
|
129
|
+
Date.new year, month, 1
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns self
|
133
|
+
def to_month
|
134
|
+
return self
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class Date
|
139
|
+
# Creates a Month object of the current month
|
140
|
+
def to_month
|
141
|
+
Month.new(self)
|
142
|
+
end
|
143
|
+
end
|
data/lib/weekday.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module WeekDayCoreExt
|
2
|
+
def is_weekday?
|
3
|
+
self.cwday < 6
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_weekday
|
7
|
+
self.is_a?(Weekday) ? self : Weekday.new(self.year, self.month, self.day)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
class Weekday < Date
|
14
|
+
|
15
|
+
def initialize(ajd=0, of=0, sg=ITALY)
|
16
|
+
super(ajd, of, sg)
|
17
|
+
raise 'Is not a weekday' unless self.is_weekday?
|
18
|
+
end
|
19
|
+
|
20
|
+
def -(x)
|
21
|
+
if x.is_a?(Numeric)
|
22
|
+
# weekend_offset = 0
|
23
|
+
# weekend_offset += 2 if (self.cwday - (x % 5)) <= 0
|
24
|
+
# weekend_offset += ((x / 5)) * 2 if x >= 5
|
25
|
+
# return self.class.new!(@ajd - x - weekend_offset, @of, @sg)
|
26
|
+
days = x % 5
|
27
|
+
weeks = (x / 5) * 7
|
28
|
+
days += 2 if (self.cwday - days) <= 0
|
29
|
+
return self.class.new!(@ajd - weeks - days, @of, @sg)
|
30
|
+
elsif x.is_a?(Date)
|
31
|
+
return @ajd - x.ajd
|
32
|
+
else
|
33
|
+
raise TypeError, 'expected numeric or date'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def +(x)
|
38
|
+
if x.is_a?(Numeric)
|
39
|
+
days = x % 5
|
40
|
+
weeks = (x / 5) * 7
|
41
|
+
days += 2 if (self.cwday + days) >= 6
|
42
|
+
return self.class.new!(@ajd + weeks + days, @of, @sg)
|
43
|
+
elsif x.is_a?(Date)
|
44
|
+
return @ajd + x.ajd
|
45
|
+
else
|
46
|
+
raise TypeError, 'expected numeric or date'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.today
|
51
|
+
super.class.today.to_weekday
|
52
|
+
end
|
53
|
+
end
|
data/test/month_test.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class MonthTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
context "Date#to_month" do
|
9
|
+
setup do
|
10
|
+
@d = Date.new(2009,2,1)
|
11
|
+
@m = @d.to_month
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have correct flag" do
|
15
|
+
assert_equal @m.year, @d.year
|
16
|
+
assert_equal @m.month, @d.month
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "Date#+/-" do
|
21
|
+
setup do
|
22
|
+
@m = Month.new 2009,3
|
23
|
+
end
|
24
|
+
|
25
|
+
should "have correct flag" do
|
26
|
+
assert_equal 2008, (@m - 12).year
|
27
|
+
assert_equal 3, (@m - 12).month
|
28
|
+
|
29
|
+
assert_equal 2009, (@m + 1).year
|
30
|
+
assert_equal 4, (@m + 1).month
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "Date#<=>" do
|
35
|
+
setup do
|
36
|
+
@m1 = Month.new 2009,3
|
37
|
+
@m12 = Month.new 2009,3
|
38
|
+
@m2 = Month.new 2009,4
|
39
|
+
@m3 = Month.new 2008,2
|
40
|
+
@m4 = Month.new 2008,4
|
41
|
+
end
|
42
|
+
|
43
|
+
should "have correct flag" do
|
44
|
+
assert_equal true, @m1 > @m4
|
45
|
+
assert_equal true, @m1 > @m3
|
46
|
+
assert_equal true, @m1 < @m2
|
47
|
+
assert_equal true, @m1 == @m12
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "Date#next" do
|
52
|
+
setup do
|
53
|
+
@d = Date.new(2009,2,1)
|
54
|
+
@m = @d.to_month.next
|
55
|
+
end
|
56
|
+
|
57
|
+
should "have correct flag" do
|
58
|
+
assert_equal @m.year, @d.year
|
59
|
+
assert_equal @m.month, @d.month+1
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "Date#next December" do
|
64
|
+
setup do
|
65
|
+
@d = Date.new(2008,12,5)
|
66
|
+
@m = @d.to_month.next
|
67
|
+
end
|
68
|
+
|
69
|
+
should "have correct flag" do
|
70
|
+
assert_equal @m.year, 2009
|
71
|
+
assert_equal @m.month, 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "Date#previous" do
|
76
|
+
setup do
|
77
|
+
@d = Date.new(2009,2,1)
|
78
|
+
@m = @d.to_month.previous
|
79
|
+
end
|
80
|
+
|
81
|
+
should "have correct flag" do
|
82
|
+
assert_equal @m.year, @d.year
|
83
|
+
assert_equal @m.month, @d.month-1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "Date#previous January" do
|
88
|
+
setup do
|
89
|
+
@d = Date.new(2009,1,31)
|
90
|
+
@m = @d.to_month.previous
|
91
|
+
end
|
92
|
+
|
93
|
+
should "have correct flag" do
|
94
|
+
assert_equal @m.year, 2008
|
95
|
+
assert_equal @m.month, 12
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
context "new" do
|
101
|
+
setup do
|
102
|
+
@d = Date.new(2009,2,1)
|
103
|
+
@m = Month.new(@d)
|
104
|
+
end
|
105
|
+
|
106
|
+
should "have correct flag" do
|
107
|
+
assert_equal @m.year, @d.year
|
108
|
+
assert_equal @m.month, @d.month
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
context "new" do
|
114
|
+
setup { @m = Month.new(2009,2)}
|
115
|
+
|
116
|
+
should "have correct flag" do
|
117
|
+
assert_equal @m.year, 2009
|
118
|
+
assert_equal @m.month, 2
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "new" do
|
123
|
+
setup { @m = Month.new("2009","02")}
|
124
|
+
|
125
|
+
should "initialize" do
|
126
|
+
assert_equal @m.year, 2009
|
127
|
+
assert_equal @m.month, 2
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'redgreen'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'active_support'
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
|
10
|
+
require 'date_ext'
|
11
|
+
|
12
|
+
class Test::Unit::TestCase
|
13
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class WeekdayTest < Test::Unit::TestCase
|
7
|
+
context "range friday ... friday_next_week" do
|
8
|
+
setup {
|
9
|
+
@fri = Weekday.new(2009,12,4)
|
10
|
+
@fri_next = Weekday.new(2009,12,11)
|
11
|
+
@range = (@fri...@fri_next)
|
12
|
+
}
|
13
|
+
|
14
|
+
should "have 5 elements" do
|
15
|
+
assert_equal 5, @range.to_a.length
|
16
|
+
end
|
17
|
+
|
18
|
+
should "all be weekdays" do
|
19
|
+
assert @range.to_a.all?{|wd| wd.is_a?(Weekday)}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "Weekday methods" do
|
24
|
+
should "return a Weekday object on #new" do
|
25
|
+
assert_equal Weekday, Weekday.new(2009,12,3).class
|
26
|
+
end
|
27
|
+
|
28
|
+
should "return a Weekday object on #today" do
|
29
|
+
assert_equal Weekday, Weekday.today.class
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "Weekdays" do
|
34
|
+
setup {
|
35
|
+
@thu = Weekday.new(2009,12,3)
|
36
|
+
@fri = Weekday.new(2009,12,4)
|
37
|
+
|
38
|
+
@mon = Weekday.new(2009,12,7)
|
39
|
+
@tue = Weekday.new(2009,12,8)
|
40
|
+
@wed = Weekday.new(2009,12,9)
|
41
|
+
}
|
42
|
+
context "Wednesday" do
|
43
|
+
should "be valid" do
|
44
|
+
assert Weekday, @wed
|
45
|
+
end
|
46
|
+
should "return previous Tuesday on -1" do
|
47
|
+
assert_equal Weekday.new(2009,12,8), @wed - 1
|
48
|
+
end
|
49
|
+
should "return previous Monday on -2" do
|
50
|
+
assert_equal Weekday.new(2009,12,7), @wed - 2
|
51
|
+
end
|
52
|
+
should "return previous Fri on -3" do
|
53
|
+
assert_equal @fri, @wed - 3
|
54
|
+
end
|
55
|
+
should "return previous Thu on -4" do
|
56
|
+
assert_equal @thu, @wed - 4
|
57
|
+
end
|
58
|
+
should "return previous Wed on -5" do
|
59
|
+
assert_equal Weekday.new(2009,12,2), @wed - 5
|
60
|
+
end
|
61
|
+
end
|
62
|
+
context "Monday" do
|
63
|
+
should "be valid" do
|
64
|
+
assert Weekday, @mon
|
65
|
+
end
|
66
|
+
should "return next Tuesday on +1" do
|
67
|
+
assert_equal @tue, @mon + 1
|
68
|
+
end
|
69
|
+
should "return next Wed on +2" do
|
70
|
+
assert_equal @wed, @mon + 2
|
71
|
+
end
|
72
|
+
should "return previous Friday on -1" do
|
73
|
+
assert_equal @fri, @mon - 1
|
74
|
+
end
|
75
|
+
should "return previous Thursday on -2" do
|
76
|
+
assert_equal @thu, @mon - 2
|
77
|
+
end
|
78
|
+
should "return monday a week ago on -5" do
|
79
|
+
assert_equal Weekday.new(2009,11,30), @mon - 5
|
80
|
+
end
|
81
|
+
should "return monday 2 weeks ago on -10" do
|
82
|
+
assert_equal Weekday.new(2009,11,23), @mon - 10
|
83
|
+
end
|
84
|
+
should "return wed 4 weeks ago on -18" do
|
85
|
+
assert_equal Weekday.new(2009,11,11), @mon - 18
|
86
|
+
end
|
87
|
+
should "return tuesay 4 weeks ago on -19" do
|
88
|
+
assert_equal Weekday.new(2009,11,10), @mon - 19
|
89
|
+
end
|
90
|
+
should "return monday 4 weeks ago on -20" do
|
91
|
+
assert_equal Weekday.new(2009,11,9), @mon - 20
|
92
|
+
end
|
93
|
+
should "not return any weekends for the last 3 years" do
|
94
|
+
arr = (0..800).to_a
|
95
|
+
days = arr.map{|i| [@mon - i, @tue - i, @wed - i, @thu - 1, @fri - 1]}.flatten
|
96
|
+
assert days.all?{|wd| wd.cwday <= 5}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hasclass
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-09 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Ruby classes for weekday, month, etc.
|
26
|
+
email: sebastian.burkhard@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- date_ext.gemspec
|
42
|
+
- init.rb
|
43
|
+
- lib/core_ext.rb
|
44
|
+
- lib/date_ext.rb
|
45
|
+
- lib/month.rb
|
46
|
+
- lib/weekday.rb
|
47
|
+
- test/month_test.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
- test/weekday_test.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/hasclass/test
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: date extensions helps you work with weekdays, workdays, holidays, months, weeks, etc.
|
78
|
+
test_files:
|
79
|
+
- test/month_test.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
- test/weekday_test.rb
|