us_bank_holidays 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 421d85478436ebcc1574e2ccc9639b887029e46e
4
- data.tar.gz: 3a0cd71dd007bfddc392a016739eabcae93af9c4
3
+ metadata.gz: 55df8b485e62ef28d7d1872ff78e21a807e241e0
4
+ data.tar.gz: 0c36413d6b45bb274e3c70a1929c6a517ab3af0e
5
5
  SHA512:
6
- metadata.gz: c1a919b687012cc56dea4f7afa1259715b83e037ac275e236dda25f1ebfd9fd9de6e642d3f8ad89f03e5cfc46846b46a84f95c16e84f96d78489daf0cd4a48cd
7
- data.tar.gz: 18552331c1658fdc6a1eda5db1bdfe61485c6736a5ae318064fadeb48a45fc6304ca625c6c5c810ec2e612cb46b21a6043160d5b0012fa72e72ffce041748e82
6
+ metadata.gz: 2153d108279fd2296d312274210153bf10ee38c50d1239baf4dc9e5ae3c1303d01fed80801ea77268419bcd6ee0f8ab3e5eb2703f5fab3b8bf9c22e15039ec40
7
+ data.tar.gz: 8ab9877ee8d665d75ba9385fde4aaa05ed186db5bdaa900cb5f24ba5c8258722d884dcd9e815515b9a1d6fd6698682bf614a189d147c029def5a3f632b9c2956
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  https://github.com/albertosaurus/us_bank_holidays
4
4
 
5
5
  [![Build Status](https://travis-ci.org/albertosaurus/us_bank_holidays.png?branch=master)](https://travis-ci.org/albertosaurus/us_bank_holidays)
6
+ [![Code Climate](https://codeclimate.com/github/albertosaurus/us_bank_holidays.png)](https://codeclimate.com/github/albertosaurus/us_bank_holidays)
6
7
 
7
8
  Patches `Date` to make working with US bank holidays easier
8
9
 
@@ -1,16 +1,20 @@
1
1
  module UsBankHolidays
2
2
 
3
- # Utility class to make it easier to work with a particular month
3
+ # Utility class to make it easier to work with a particular month. It represents
4
+ # a month of a specific year.
4
5
  class Month
5
6
 
6
7
  attr_reader :year, :month
7
8
 
9
+ # Initializes an instance from a year and a month. Raises an error if the
10
+ # month is not in the allowed range, i.e. it must be between 1 and 12 inclusive.
8
11
  def initialize(year, month)
9
12
  if month < 1 || month > 12
10
13
  raise ArgumentError, "Month is out of range, must be between 1 and 12, got #{month}"
11
14
  end
12
15
  @month = month
13
- @year = year
16
+ @year = year
17
+ @weeks = []
14
18
  init_month
15
19
  end
16
20
 
@@ -41,6 +45,9 @@ module UsBankHolidays
41
45
  :saturdays => 6
42
46
  }.each do |day, index|
43
47
 
48
+ # For each day of the week, define a method that will return the set of
49
+ # all the matching dates in the month. For example, this lets me determine
50
+ # all the Mondays of the month, or all the Fridays.
44
51
  define_method(day) do
45
52
  @weeks.map { |w|
46
53
  w[index]
@@ -49,10 +56,14 @@ module UsBankHolidays
49
56
 
50
57
  end
51
58
 
59
+ # Returns true if the given month contains the given date, false otherwise.
60
+ def contains?(date)
61
+ year == date.year && month == date.month
62
+ end
63
+
52
64
  private
53
65
 
54
66
  def init_month
55
- @weeks = []
56
67
  current_date = Date.new(year, month, 1)
57
68
  week = init_first_week(current_date)
58
69
  while current_date.month == month
@@ -60,31 +71,16 @@ module UsBankHolidays
60
71
  current_date += 1
61
72
  if week.size == 7 || current_date.month != month
62
73
  @weeks << week.freeze
63
- unless current_date.month > month
64
- week = []
65
- end
74
+ week = [] if contains?(current_date)
66
75
  end
67
76
  end
68
77
  @weeks.freeze
69
78
  end
70
79
 
71
80
  def init_first_week(start_date)
72
- case start_date.wday
73
- when 0
74
- []
75
- when 1
76
- [nil]
77
- when 2
78
- [nil, nil]
79
- when 3
80
- [nil, nil, nil]
81
- when 4
82
- [nil, nil, nil, nil]
83
- when 5
84
- [nil, nil, nil, nil, nil]
85
- when 6
86
- [nil, nil, nil, nil, nil, nil]
87
- end
81
+ offset = []
82
+ start_date.wday.times { offset << nil }
83
+ offset
88
84
  end
89
85
 
90
86
  end
@@ -1,3 +1,3 @@
1
1
  module UsBankHolidays
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -19,6 +19,27 @@ describe UsBankHolidays::Month do
19
19
  }
20
20
  end
21
21
 
22
+ describe '.contains?' do
23
+
24
+ it 'should return true if the month contains the given date' do
25
+ (1..31).each do |mday|
26
+ january.contains?(Date.new(january.year, january.month, mday)).should be_true
27
+ end
28
+ end
29
+
30
+ it 'should return false if the month is not the same' do
31
+ (1..31).each do |mday|
32
+ january.contains?(Date.new(2014, 8, mday)).should be_false
33
+ end
34
+ end
35
+
36
+ it 'should return false if the year is not the same' do
37
+ (1..31).each do |mday|
38
+ january.contains?(Date.new(2015, 1, mday)).should be_false
39
+ end
40
+ end
41
+ end
42
+
22
43
  describe '.to_s' do
23
44
 
24
45
  let(:january_str) {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: us_bank_holidays
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur Shagall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-08 00:00:00.000000000 Z
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler