date_ext 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/date_ext.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{date_ext}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hasclass"]
12
- s.date = %q{2009-12-09}
12
+ s.date = %q{2010-01-13}
13
13
  s.description = %q{Ruby classes for weekday, month, etc.}
14
14
  s.email = %q{sebastian.burkhard@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,10 +28,14 @@ Gem::Specification.new do |s|
28
28
  "lib/core_ext.rb",
29
29
  "lib/date_ext.rb",
30
30
  "lib/month.rb",
31
+ "lib/quarter.rb",
31
32
  "lib/weekday.rb",
33
+ "lib/year.rb",
32
34
  "test/month_test.rb",
35
+ "test/quarter_test.rb",
33
36
  "test/test_helper.rb",
34
- "test/weekday_test.rb"
37
+ "test/weekday_test.rb",
38
+ "test/year_test.rb"
35
39
  ]
36
40
  s.homepage = %q{http://github.com/hasclass/test}
37
41
  s.rdoc_options = ["--charset=UTF-8"]
@@ -40,8 +44,10 @@ Gem::Specification.new do |s|
40
44
  s.summary = %q{date extensions helps you work with weekdays, workdays, holidays, months, weeks, etc.}
41
45
  s.test_files = [
42
46
  "test/month_test.rb",
47
+ "test/quarter_test.rb",
43
48
  "test/test_helper.rb",
44
- "test/weekday_test.rb"
49
+ "test/weekday_test.rb",
50
+ "test/year_test.rb"
45
51
  ]
46
52
 
47
53
  if s.respond_to? :specification_version then
data/lib/core_ext.rb CHANGED
@@ -1,3 +1,10 @@
1
1
  class Date
2
2
  include WeekDayCoreExt
3
+ end
4
+
5
+ class Date
6
+ # Creates a Month object of the current month
7
+ def to_month
8
+ Month.new(self)
9
+ end
3
10
  end
data/lib/date_ext.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require 'month'
2
2
  require 'weekday'
3
+ require 'year'
4
+ require 'quarter'
3
5
  require 'core_ext'
data/lib/month.rb CHANGED
@@ -40,7 +40,6 @@ class Month
40
40
  @year = year.to_i
41
41
  @month = month.to_i
42
42
  end
43
-
44
43
 
45
44
  # If _months_ is a Numeric value, create a new Month object that is x months earlier than the current one.
46
45
  #
@@ -93,20 +92,12 @@ class Month
93
92
 
94
93
  # Returns the last weekday (everyday except saturday or sunday) of the current month.
95
94
  def last_cday
96
- date = last_day
97
- while date.cwday > 5
98
- date = date -= 1
99
- end
100
- date
95
+ last_weekday
101
96
  end
102
97
 
103
98
  # Returns the first weekday (everyday except saturday or sunday) of the current month.
104
99
  def first_cday
105
- date = first_day
106
- while date.cwday > 5
107
- date = date += 1
108
- end
109
- date
100
+ first_weekday
110
101
  end
111
102
 
112
103
  # Returns the first commercial weekday with the given weekday number
@@ -117,7 +108,16 @@ class Month
117
108
  end
118
109
  date
119
110
  end
120
-
111
+
112
+ def first_weekday
113
+ d = first_day
114
+ Weekday.civil_or_newer(d.year, d.month, d.day)
115
+ end
116
+
117
+ def last_weekday
118
+ last_day.to_weekday
119
+ end
120
+
121
121
  # Returns the last day of current month as a Date object.
122
122
  def last_day
123
123
  first_of_next_month = first_day + 32
@@ -135,9 +135,3 @@ class Month
135
135
  end
136
136
  end
137
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/quarter.rb ADDED
@@ -0,0 +1,47 @@
1
+ class Quarter
2
+ attr_accessor :year, :quarter
3
+
4
+ def initialize(year, quarter)
5
+ @year = year
6
+ @quarter = quarter
7
+ end
8
+
9
+ def to_year
10
+ Year.new(@year)
11
+ end
12
+
13
+ # def -(x)
14
+ # if x.is_a?(Numeric)
15
+ # return self.class.new(@year - x)
16
+ # elsif x.is_a?(Quarter)
17
+ # return @year - x.year
18
+ # else
19
+ # raise TypeError, 'expected numeric or year'
20
+ # end
21
+ # end
22
+ #
23
+ # def +(x)
24
+ # if x.is_a?(Numeric)
25
+ # return self.class.new(@year + x)
26
+ # elsif x.is_a?(Quarter)
27
+ # return @year + x.year
28
+ # else
29
+ # raise TypeError, 'expected numeric or year'
30
+ # end
31
+ # end
32
+
33
+ def months; (first_month..last_month).to_a; end
34
+
35
+ def first_month; Month.new(@year,@quarter); end
36
+ def last_month; Month.new(@year,@quarter+2); end
37
+
38
+ def dates; (first_date..last_date).to_a; end
39
+
40
+ def first_date; first_month.first_day; end
41
+ def last_date; last_month.last_day; end
42
+
43
+ def weekdays; (first_weekday..last_date).to_a; end
44
+
45
+ def first_weekday; first_month.first_weekday; end
46
+ def last_weekday; last_weekday.last_weekday; end
47
+ end
data/lib/weekday.rb CHANGED
@@ -2,21 +2,39 @@ module WeekDayCoreExt
2
2
  def is_weekday?
3
3
  self.cwday < 6
4
4
  end
5
-
5
+
6
+ # Returns a Weekday. Doesn't raise an exception if not a weekday,
7
+ # but returns the most previous weekday.
8
+ #
6
9
  def to_weekday
7
- self.is_a?(Weekday) ? self : Weekday.new(self.year, self.month, self.day)
10
+ Weekday.civil(self.year, self.month, self.day)
11
+ end
12
+
13
+ # Returns a Weekday. Raises Exception if not a weekday
14
+ #
15
+ def to_weekday!
16
+ Weekday.civil!(self.year, self.month, self.day)
8
17
  end
9
18
  end
10
19
 
20
+ class Weekday < Date
21
+ OLDER = '-'
22
+ NEWER = '+'
11
23
 
24
+ def self.civil(y=-4712, m=1, d=1, sg=ITALY)
25
+ weekday_or_nearest(y,m,d,OLDER)
26
+ end
12
27
 
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?
28
+ def self.civil_or_newer(y=-4712, m=1, d=1, sg=ITALY)
29
+ weekday_or_nearest(y,m,d,NEWER)
18
30
  end
19
-
31
+
32
+ def self.civil!(y=-4712, m=1, d=1, sg=ITALY)
33
+ day = new(y,m,d)
34
+ raise "Is not a Weekday" unless day.is_weekday?
35
+ day
36
+ end
37
+
20
38
  def -(x)
21
39
  if x.is_a?(Numeric)
22
40
  # weekend_offset = 0
@@ -33,7 +51,7 @@ class Weekday < Date
33
51
  raise TypeError, 'expected numeric or date'
34
52
  end
35
53
  end
36
-
54
+
37
55
  def +(x)
38
56
  if x.is_a?(Numeric)
39
57
  days = x % 5
@@ -50,4 +68,17 @@ class Weekday < Date
50
68
  def self.today
51
69
  super.class.today.to_weekday
52
70
  end
71
+
72
+ def self.today!
73
+ super.class.today.to_weekday!
74
+ end
75
+
76
+ private
77
+ def self.weekday_or_nearest(y,m,d,operator = OLDER)
78
+ day = Date.new(y,m,d)
79
+ while !day.is_weekday?
80
+ day = day.send(operator, 1)
81
+ end
82
+ day.to_weekday!
83
+ end
53
84
  end
data/lib/year.rb ADDED
@@ -0,0 +1,42 @@
1
+ class Year
2
+ attr_accessor :year
3
+
4
+ def initialize(year)
5
+ @year = year
6
+ end
7
+
8
+ def -(x)
9
+ if x.is_a?(Numeric)
10
+ return self.class.new(@year - x)
11
+ elsif x.is_a?(Year)
12
+ return @year - x.year
13
+ else
14
+ raise TypeError, 'expected numeric or year'
15
+ end
16
+ end
17
+
18
+ def +(x)
19
+ if x.is_a?(Numeric)
20
+ return self.class.new(@year + x)
21
+ elsif x.is_a?(Year)
22
+ return @year + x.year
23
+ else
24
+ raise TypeError, 'expected numeric or year'
25
+ end
26
+ end
27
+
28
+ def months; (first_month..last_month).to_a; end
29
+
30
+ def first_month; Month.new(year,1); end
31
+ def last_month; Month.new(year,12); end
32
+
33
+ def dates; (first_date..last_date).to_a; end
34
+
35
+ def first_date; Date.new(year,1,1); end
36
+ def last_date; Date.new(year,12,31); end
37
+
38
+ def weekdays; (first_weekday..last_date).to_a; end
39
+
40
+ def first_weekday; first_month.first_weekday; end
41
+ def last_weekday; last_month.last_weekday; end
42
+ end
data/test/month_test.rb CHANGED
@@ -5,6 +5,19 @@ require File.dirname(__FILE__) + '/test_helper'
5
5
 
6
6
  class MonthTest < Test::Unit::TestCase
7
7
 
8
+ context "December" do
9
+ setup {
10
+ @month = Month.new(2011,12)
11
+ }
12
+ should "have last_day 2011-12-31" do
13
+ assert_equal Date.new(2011,12,31), @month.last_day
14
+ end
15
+ should "have last_weekday 2011-12-30" do
16
+ assert_equal Date.new(2011,12,30), @month.last_weekday
17
+ end
18
+
19
+ end
20
+
8
21
  context "Date#to_month" do
9
22
  setup do
10
23
  @d = Date.new(2009,2,1)
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class QuarterTest < Test::Unit::TestCase
4
+
5
+ context "Quarter 2009" do
6
+ setup {
7
+ @quarter = Quarter.new(2009,1)
8
+ }
9
+ should "have 2009-01 as first_month" do
10
+ assert_equal Month.new(2009,1), @quarter.first_month
11
+ end
12
+ should "have 2009-12 as last_month" do
13
+ assert_equal Month.new(2009,3), @quarter.last_month
14
+ end
15
+ should "have 2009-01-01 as first_date" do
16
+ assert_equal Date.new(2009,1,1), @quarter.first_date
17
+ end
18
+ should "have 2009-12-31 as last_date" do
19
+ assert_equal Date.new(2009,3,31), @quarter.last_date
20
+ end
21
+ end
22
+
23
+ end
data/test/weekday_test.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
-
4
-
5
-
6
3
  class WeekdayTest < Test::Unit::TestCase
7
4
  context "range friday ... friday_next_week" do
8
5
  setup {
@@ -10,35 +7,48 @@ class WeekdayTest < Test::Unit::TestCase
10
7
  @fri_next = Weekday.new(2009,12,11)
11
8
  @range = (@fri...@fri_next)
12
9
  }
13
-
10
+
14
11
  should "have 5 elements" do
15
12
  assert_equal 5, @range.to_a.length
16
13
  end
17
-
14
+
18
15
  should "all be weekdays" do
19
16
  assert @range.to_a.all?{|wd| wd.is_a?(Weekday)}
20
17
  end
21
18
  end
22
-
19
+
23
20
  context "Weekday methods" do
24
21
  should "return a Weekday object on #new" do
25
22
  assert_equal Weekday, Weekday.new(2009,12,3).class
26
23
  end
27
-
24
+
28
25
  should "return a Weekday object on #today" do
29
26
  assert_equal Weekday, Weekday.today.class
30
27
  end
31
28
  end
32
-
29
+
30
+ context "On weekends" do
31
+ should "return previous friday if #civil on weekend" do
32
+ assert_equal Weekday.civil(2009,12,4), Weekday.civil(2009,12,5)
33
+ assert_equal Weekday.civil(2009,12,4), Weekday.civil(2009,12,6)
34
+ end
35
+
36
+ should "return next monday if #civil_or_newer on weekend" do
37
+ assert_equal Weekday.civil(2009,12,7), Weekday.civil_or_newer(2009,12,5)
38
+ assert_equal Weekday.civil(2009,12,7), Weekday.civil_or_newer(2009,12,6)
39
+ end
40
+ end
41
+
33
42
  context "Weekdays" do
34
43
  setup {
35
44
  @thu = Weekday.new(2009,12,3)
36
45
  @fri = Weekday.new(2009,12,4)
37
-
46
+
38
47
  @mon = Weekday.new(2009,12,7)
39
48
  @tue = Weekday.new(2009,12,8)
40
49
  @wed = Weekday.new(2009,12,9)
41
50
  }
51
+
42
52
  context "Wednesday" do
43
53
  should "be valid" do
44
54
  assert Weekday, @wed
@@ -59,6 +69,7 @@ class WeekdayTest < Test::Unit::TestCase
59
69
  assert_equal Weekday.new(2009,12,2), @wed - 5
60
70
  end
61
71
  end
72
+
62
73
  context "Monday" do
63
74
  should "be valid" do
64
75
  assert Weekday, @mon
data/test/year_test.rb ADDED
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class YearTest < Test::Unit::TestCase
4
+
5
+ context "Year 2009" do
6
+ setup {
7
+ @year = Year.new(2009)
8
+ }
9
+ should "assign year" do
10
+ assert_equal 2009, @year.year
11
+ end
12
+ should "have 2009-01 as first_month" do
13
+ assert_equal Month.new(2009,1), @year.first_month
14
+ end
15
+ should "have 2009-12 as last_month" do
16
+ assert_equal Month.new(2009,12), @year.last_month
17
+ end
18
+ should "have 2009-01-01 as first_date" do
19
+ assert_equal Date.new(2009,1,1), @year.first_date
20
+ end
21
+ should "have 2009-12-31 as last_date" do
22
+ assert_equal Date.new(2009,12,31), @year.last_date
23
+ end
24
+ should "have 2009-01-01 as first_weekday" do
25
+ assert_equal Date.new(2009,1,1), @year.first_weekday
26
+ end
27
+ should "have 2009-12-31 as last_weekday" do
28
+ assert_equal Date.new(2009,12,31), @year.last_weekday
29
+ end
30
+ end
31
+
32
+ context "Year 2011" do
33
+ setup {
34
+ @year = Year.new(2011)
35
+ }
36
+ should "have 2011-01-03 as first_weekday" do
37
+ assert_equal Date.new(2011,1,3), @year.first_weekday
38
+ end
39
+ should "have 2011-12-30 as last_weekday" do
40
+ assert_equal Date.new(2011,12,30), @year.last_weekday
41
+ end
42
+
43
+ end
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - hasclass
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-09 00:00:00 +01:00
12
+ date: 2010-01-13 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -43,10 +43,14 @@ files:
43
43
  - lib/core_ext.rb
44
44
  - lib/date_ext.rb
45
45
  - lib/month.rb
46
+ - lib/quarter.rb
46
47
  - lib/weekday.rb
48
+ - lib/year.rb
47
49
  - test/month_test.rb
50
+ - test/quarter_test.rb
48
51
  - test/test_helper.rb
49
52
  - test/weekday_test.rb
53
+ - test/year_test.rb
50
54
  has_rdoc: true
51
55
  homepage: http://github.com/hasclass/test
52
56
  licenses: []
@@ -77,5 +81,7 @@ specification_version: 3
77
81
  summary: date extensions helps you work with weekdays, workdays, holidays, months, weeks, etc.
78
82
  test_files:
79
83
  - test/month_test.rb
84
+ - test/quarter_test.rb
80
85
  - test/test_helper.rb
81
86
  - test/weekday_test.rb
87
+ - test/year_test.rb