holidate 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.
Files changed (2) hide show
  1. data/lib/holidate.rb +57 -28
  2. metadata +2 -2
data/lib/holidate.rb CHANGED
@@ -1,62 +1,91 @@
1
1
  require 'date'
2
2
 
3
3
  class Holidate
4
- def self.find_day nth, wday, month, year=Date.today.year
4
+ def self.find nth_, wday_, month_, year_=Date.today.year
5
+ nth = nth_int(nth_)
6
+ wday = wday_int(wday_)
7
+ month = month_int(month_)
8
+ year = year_int(year_)
5
9
  if nth > 0
6
10
  Date.new year, month, ( 1 + ( nth - 1) * 7 + (wday - Date.new(year, month, 1).wday) % 7)
7
- elsif nth < 0
11
+ else
8
12
  days_in_month = ((Date.new(year, month) >> 1) -1).day
9
13
  Date.new year, month, (days_in_month - (-nth - 1) * 7 - (Date.new(year, month, days_in_month).wday - wday) % 7)
10
14
  end
11
15
  end
16
+ singleton_class.send(:alias_method, :find_day, :find)
12
17
 
13
- def self.new_years_day year
18
+ def self.new_years_day year=Date.today.year
14
19
  Date.new year, 1, 1
15
20
  end
16
- self.singleton_class.send(:alias_method, :new_year, :new_years_day)
21
+ singleton_class.send(:alias_method, :new_year, :new_years_day)
17
22
 
18
- def self.martin_luther_king_jr_day year
19
- self.find_day 3, 1, 1, year
23
+ def self.martin_luther_king_jr_day year=Date.today.year
24
+ find 'third', 'monday', 'january', year
20
25
  end
21
- self.singleton_class.send(:alias_method, :mlk, :martin_luther_king_jr_day)
26
+ singleton_class.send(:alias_method, :mlk, :martin_luther_king_jr_day)
22
27
 
23
- def self.washingtons_birthday year
24
- self.find_day 3, 1, 2, year
28
+ def self.washingtons_birthday year=Date.today.year
29
+ find 'third', 'monday', 'february', year
25
30
  end
26
- self.singleton_class.send(:alias_method, :washington, :washingtons_birthday)
31
+ singleton_class.send(:alias_method, :washington, :washingtons_birthday)
27
32
 
28
- def self.memorial_day year
29
- self.find_day -1, 1, 5, year
33
+ def self.memorial_day year=Date.today.year
34
+ find 'last', 'monday', 'may', year
30
35
  end
31
- self.singleton_class.send(:alias_method, :memorial, :memorial_day)
36
+ singleton_class.send(:alias_method, :memorial, :memorial_day)
32
37
 
33
- def self.independence_day year
38
+ def self.independence_day year=Date.today.year
34
39
  Date.new year, 7, 4
35
40
  end
36
- self.singleton_class.send(:alias_method, :july_4, :independence_day)
41
+ singleton_class.send(:alias_method, :july_4, :independence_day)
37
42
 
38
- def self.labor_day year
39
- self.find_day 1, 1, 9, year
43
+ def self.labor_day year=Date.today.year
44
+ find 'first', 'monday', 'september', year
40
45
  end
41
- self.singleton_class.send(:alias_method, :labor, :labor_day)
46
+ singleton_class.send(:alias_method, :labor, :labor_day)
42
47
 
43
- def self.columbus_day year
44
- self.find_day 2, 1, 10, year
48
+ def self.columbus_day year=Date.today.year
49
+ find 'second', 'monday', 'october', year
45
50
  end
46
- self.singleton_class.send(:alias_method, :columbus, :columbus_day)
51
+ singleton_class.send(:alias_method, :columbus, :columbus_day)
47
52
 
48
- def self.veterans_day year
53
+ def self.veterans_day year=Date.today.year
49
54
  Date.new year, 11, 11
50
55
  end
51
- self.singleton_class.send(:alias_method, :veteran, :veterans_day)
56
+ singleton_class.send(:alias_method, :veteran, :veterans_day)
52
57
 
53
- def self.thanksgiving_day year
54
- self.find_day 4, 4, 11, year
58
+ def self.thanksgiving_day year=Date.today.year
59
+ find 'fourth', 'thursday', 'november', year
55
60
  end
56
- self.singleton_class.send(:alias_method, :thanksgiving, :thanksgiving_day)
61
+ singleton_class.send(:alias_method, :thanksgiving, :thanksgiving_day)
57
62
 
58
- def self.christmas_day year
63
+ def self.christmas_day year=Date.today.year
59
64
  Date.new year, 12, 25
60
65
  end
61
- self.singleton_class.send(:alias_method, :xmas, :christmas_day)
66
+ singleton_class.send(:alias_method, :xmas, :christmas_day)
67
+
68
+ private
69
+
70
+ def self.nth_int nth_
71
+ h = { 'first' => 1, 'second' => 2, 'third' => 3, 'fourth' => 4, 'fifth' => 5, 'last' => 1 }
72
+ nth = nth_.to_s.downcase
73
+ nth_int = nth.to_i == 0 ? h[nth.split(/[-_ ]/).first] : nth.to_i
74
+ nth_int = -nth_int if nth.to_s.downcase.end_with?('last')
75
+ nth_int
76
+ end
77
+
78
+ def self.wday_int wday
79
+ h = { 'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3, 'thu' => 4, 'fri' => 5, 'sat' => 6 }
80
+ (0..6).include?(wday) ? wday : h[wday.to_s.downcase[0..2]]
81
+ end
82
+
83
+ def self.month_int month
84
+ h = { 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12 }
85
+ (1..12).include?(month) ? month : h[month.to_s.downcase[0..2]]
86
+ end
87
+
88
+ def self.year_int year
89
+ year.to_s.to_i
90
+ end
62
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: holidate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-21 00:00:00.000000000 Z
12
+ date: 2015-02-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler