what_date 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +14 -10
- data/lib/what_date/client.rb +1 -0
- data/lib/what_date/date_of_month.rb +41 -9
- data/lib/what_date/version.rb +1 -1
- data/what_date.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7f4e7f5299beb9358ddb950978a85d43345a1ba2
|
4
|
+
data.tar.gz: 4f1014d45467de87328c5daed2c4b3a021db2fca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c89b1f0e607cba9b25fe4247169f4e6290dbece02bd43654e93f653620c1949b56a51afa7330ea5171a91f744553458833f0a79a9730fbe98d23e7483059f5fb
|
7
|
+
data.tar.gz: b2c1e9b149a29d4717b30ae8c0739de66e19dd09e47687eb38c467677d3bac1822554339fa3575f83f857f13892b388f67c5af040a5cab8ad820c423912d8a77
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# WhatDate
|
2
|
-
|
2
|
+
Simple solution to such questions as "What date is next Monday?"
|
3
3
|
## Installation
|
4
4
|
|
5
5
|
Add this line to your application's Gemfile:
|
@@ -23,19 +23,23 @@ You can query the date of any day in the current week, the previous week or the
|
|
23
23
|
$client = WhatDate.client
|
24
24
|
|
25
25
|
## get dates
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
client.friday
|
27
|
+
client.tuesday(Date.new(2018,2,3)) ##date of tuesday in the week of the given date
|
28
|
+
client.prev_tuesday
|
29
|
+
client.next_tuesday
|
30
30
|
|
31
31
|
```
|
32
|
-
You can also ask for the date of a particular day of a given month. Use ordinals such as first, second and up to fifth.
|
32
|
+
You can also ask for the date of a particular day of a given month. Use ordinals such as first, second and up to fifth. You can enquire the last date too.
|
33
33
|
|
34
34
|
```
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
client.first_monday_of_june_2018
|
36
|
+
##4 June 2018
|
37
|
+
|
38
|
+
client.third_tuesday_of_may_2018
|
39
|
+
##19 June 2018
|
40
|
+
|
41
|
+
client.last_monday_of_may_2018
|
42
|
+
##28 May 2018
|
39
43
|
|
40
44
|
```
|
41
45
|
|
data/lib/what_date/client.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
module WhatDate
|
2
2
|
module DateOfMonth
|
3
3
|
|
4
|
+
ORDINALS ={ "first" => 1, "second" => 2, "third" => 3, "fourth" => 4, "fifth" => 5, "last" => nil }.freeze
|
5
|
+
ORDERS = '(first||second||third||fourth||fifth||last)'.freeze
|
6
|
+
WEEKDAYS = '(monday||tuesday||wednesday||thursday||friday||saturday||sunday)'.freeze
|
7
|
+
MONTHS = '(jan||feb||mar||apr||may||jun||jul||aug||sep||oct||nov||dec||january||february||march||april||may||june||july||august||september||october||november||december||sept)'.freeze
|
8
|
+
|
9
|
+
|
4
10
|
def date_of_month(order:1, day:, month:, year: Date.today.year)
|
5
11
|
month_int = Date::MONTHNAMES.index(month)
|
6
12
|
first_date = Date.new(year, month_int, 1)
|
@@ -11,23 +17,49 @@ module WhatDate
|
|
11
17
|
else
|
12
18
|
inc = (order - 1) * 7 + (lookup - first_day + 1)
|
13
19
|
end
|
14
|
-
|
20
|
+
begin
|
21
|
+
Date.new(year, month_int, inc)
|
22
|
+
rescue ArgumentError
|
23
|
+
nil
|
24
|
+
end
|
15
25
|
end
|
16
26
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
month = methods[3].titleize
|
27
|
+
def method_missing(name, *args, &block)
|
28
|
+
if name.to_s =~ Regexp.new("^#{ORDERS}_#{WEEKDAYS}_of_#{MONTHS}_\\d+$", true)
|
29
|
+
methods = name.to_s.split("_")
|
30
|
+
order = ORDINALS[methods[0].downcase]
|
31
|
+
day = format_date_string methods[1]
|
32
|
+
month = format_date_string methods[3]
|
24
33
|
year = methods[4].to_i
|
25
|
-
|
34
|
+
order == nil ? date_of_last_week_day_in_month(day, month, year) :
|
35
|
+
date_of_month(order: order, day: day, month: month, year: year)
|
26
36
|
else
|
27
37
|
super
|
28
38
|
end
|
29
39
|
end
|
30
40
|
|
41
|
+
def last_date_of_month(month:, year:)
|
42
|
+
begin
|
43
|
+
Date.parse("#{year}-#{month}-1").end_of_month
|
44
|
+
rescue ArgumentError
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def format_date_string(str)
|
52
|
+
str.downcase.capitalize
|
53
|
+
end
|
54
|
+
|
55
|
+
def date_of_last_week_day_in_month(lookup_day, month, year)
|
56
|
+
last_date = last_date_of_month(month: month, year: year)
|
57
|
+
last_day = last_date.wday
|
58
|
+
lookup = Date::DAYNAMES.index(lookup_day)
|
59
|
+
diff = last_day - lookup
|
60
|
+
diff += 7 if lookup > last_day
|
61
|
+
last_date.days_ago(diff)
|
62
|
+
end
|
31
63
|
|
32
64
|
end
|
33
65
|
end
|
data/lib/what_date/version.rb
CHANGED
data/what_date.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["zorro.ej@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{A simple tool to return the dates given a day}
|
13
|
-
spec.description = %q{You may ask for the date of next Tuesday, this Monday, last
|
13
|
+
spec.description = %q{You may ask for the date of next Tuesday, this Monday, last Saturday, or the first Saturday of Nov 2011}
|
14
14
|
spec.homepage = "https://github.com/ej2015/what_date.git"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: what_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edgar
|
@@ -80,8 +80,8 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.11'
|
83
|
-
description: You may ask for the date of next Tuesday, this Monday, last
|
84
|
-
or the first
|
83
|
+
description: You may ask for the date of next Tuesday, this Monday, last Saturday,
|
84
|
+
or the first Saturday of Nov 2011
|
85
85
|
email:
|
86
86
|
- zorro.ej@gmail.com
|
87
87
|
executables: []
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.6.14
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: A simple tool to return the dates given a day
|