saturday 1.0.2 → 1.1.0

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 (3) hide show
  1. data/lib/saturday.rb +14 -8
  2. data/lib/saturday/dates.rb +27 -24
  3. metadata +1 -1
@@ -1,14 +1,20 @@
1
- class Saturday
2
1
 
3
- @@by = ['by_year','by_year_and_month']
4
- class << self
5
- @@by.each do |method|
2
+ require 'saturday/dates'
3
+ include Dates
4
+ class Saturday
5
+ class << self
6
+ BY.each do |method|
6
7
  define_method(method) do |year=nil,month=nil|
7
- date=Dates.new(year,month)
8
+ date=Dates::Saturday.new(year,month)
8
9
  date.find_saturdays
9
10
  end
10
11
  end
11
- end
12
- end
13
12
 
14
- require 'saturday/dates'
13
+ MONTHS_MAP.each_with_index do |(key,value),index|
14
+ define_method("in_#{key.to_s}") do |year=nil,month=nil|
15
+ date=Dates::Saturday.new(year,month = index)
16
+ date.find_saturdays
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,34 +1,36 @@
1
1
  require 'date'
2
- class Saturday::Dates
2
+ module Dates
3
+ BY = ['by_year','by_year_and_month']
4
+ MONTHS_MAP = {:nil => :nil,
5
+ :jan => 31,
6
+ :feb => 28,
7
+ :mar => 31,
8
+ :apr => 30,
9
+ :may => 31,
10
+ :jun => 30,
11
+ :jul => 31,
12
+ :aug => 31,
13
+ :sep => 30,
14
+ :oct => 31,
15
+ :nov => 30,
16
+ :dec => 31}
17
+ class Saturday
3
18
  attr_accessor :date ,:year ,:month
4
19
 
5
- MONTHS_MAP = [{:january => 31},
6
- {:february => 28},
7
- {:march => 31},
8
- {:april => 30},
9
- {:may => 31},
10
- {:june => 30},
11
- {:july => 31},
12
- {:august => 31},
13
- {:september => 30},
14
- {:october => 31},
15
- {:november => 30},
16
- {:december => 31}]
17
-
18
- def initialize(year=nil,month=nil)
19
- @year = year
20
- @month = month
21
- @date = @year && @month.nil? ? Date.new(year) : Date.new(year,month)
22
- end
20
+ def initialize(year=nil,month=nil)
21
+ @year = year
22
+ @month = month
23
+ @date = @year && @month.nil? ? Date.new(year) : Date.new(year,month)
24
+ end
23
25
 
24
26
  def find_saturdays
25
- @date.leap? ? MONTHS_MAP[1][:february] = 29 : MONTHS_MAP
27
+ @date.leap? ? MONTHS_MAP[:feb] = 29 : MONTHS_MAP
26
28
  dates = []
27
29
  year ,month = @date.year ,@date.month
28
30
  if @year && @month
29
- MONTHS_MAP[@month - 1].each_pair { |key,value| dates << find_dates(year,month,value)}
30
- else
31
- MONTHS_MAP.each {|k| k.each_pair { |key,value| dates << find_dates(year,MONTHS_MAP.index(k) + 1,value)} }
31
+ MONTHS_MAP.each_with_index { |(key,value),index| dates << find_dates(year,month,value) if month == index}
32
+ else
33
+ MONTHS_MAP.each_with_index { |(key,value),index| dates << find_dates(year,month = index,value) unless index==0}
32
34
  end
33
35
  dates
34
36
  end
@@ -36,8 +38,9 @@ class Saturday::Dates
36
38
  private
37
39
 
38
40
  def find_dates(year,month,value)
39
- saturdays = []
41
+ saturdays = Array.new
40
42
  1.upto(value) {|o| saturdays << Date.new(year,month,o) if Date.new(year,month,o).saturday? }
41
43
  saturdays
42
44
  end
45
+ end
43
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saturday
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: