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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 79d947d42c8a1beb11fcbc8327ac2933f7c0cd4a19dfb4cb07adaf48ce6402e7
4
- data.tar.gz: eab41983ec577e1aabb9d3abace7a74197c0d56409bb51427ff2d77c0cba98cc
2
+ SHA1:
3
+ metadata.gz: 7f4e7f5299beb9358ddb950978a85d43345a1ba2
4
+ data.tar.gz: 4f1014d45467de87328c5daed2c4b3a021db2fca
5
5
  SHA512:
6
- metadata.gz: d3a8e5c833ba814f98a8ca0c08e8768f7b2edd3caed7ce311d2ae04b78664b96ac962394138eb672f419c3f29dfa0fd18cce5f63d7a7e9d8e13419e0487d1dc5
7
- data.tar.gz: 4c83b1bede76efe2eafe93c58094a86f86a1af451a1c9a16a5ea26c4044f85551792677ed019383c39cd5f38c93af266a6c33a7808c2f0845871407db6ba0e61
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
- $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
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. But'last' does not work yet.
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
- $client.first_monday_of_june_2018
36
- $##4 June 2018
37
- $client.third_tuesday_of_may_2018
38
- $##19 June 2018
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
 
@@ -1,5 +1,6 @@
1
1
  require 'active_support'
2
2
  require 'active_support/core_ext/date/calculations'
3
+ require 'active_support/core_ext/time/calculations'
3
4
  require 'what_date/date_of_month'
4
5
 
5
6
  module WhatDate
@@ -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
- Date.new(year, month_int, inc)
20
+ begin
21
+ Date.new(year, month_int, inc)
22
+ rescue ArgumentError
23
+ nil
24
+ end
15
25
  end
16
26
 
17
-
18
- def method_missing(meth, *args)
19
- if meth.to_s =~ /^(first||second||third||fourth||fifth)_(monday||tuesday||wednesday||thursday||friday||saturday||sunday)_of_[A-Za-z]+_\d+$/
20
- methods = meth.to_s.split("_")
21
- order = { "first" => 1, "second" => 2, "third" => 3, "fourth" => 4, "fifth" => 5 }[methods[0]]
22
- day = methods[1].titleize
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
- date_of_month(order: order, day: day, month: month, year: year)
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
@@ -1,3 +1,3 @@
1
1
  module WhatDate
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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 Saturyda, or the first Saturyda of Nov 2011}
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.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 Saturyda,
84
- or the first Saturyda of Nov 2011
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.7.3
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