timelord 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +12 -12
- data/lib/timelord.rb +21 -5
- data/spec/timelord_spec.rb +12 -0
- metadata +53 -42
data/Rakefile
CHANGED
@@ -1,20 +1,15 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
1
|
+
require 'bundler'
|
2
|
+
require 'rdoc/task'
|
3
|
+
require 'rubygems/package_task'
|
4
|
+
require 'rspec/core/rake_task'
|
5
5
|
|
6
6
|
specification = Gem::Specification.load("timelord.gemspec")
|
7
7
|
|
8
|
-
|
9
|
-
t.
|
8
|
+
RSpec::Core::RakeTask.new do |t|
|
9
|
+
t.pattern = FileList['spec/**/*_spec.rb']
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
package.need_zip = true
|
14
|
-
package.need_tar = true
|
15
|
-
end
|
16
|
-
|
17
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
12
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
18
13
|
rdoc.rdoc_dir = 'doc'
|
19
14
|
rdoc.title = 'Timelord'
|
20
15
|
rdoc.options << '--line-numbers' << '--inline-source'
|
@@ -22,5 +17,10 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
22
17
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
18
|
end
|
24
19
|
|
20
|
+
Gem::PackageTask.new(specification) do |pkg|
|
21
|
+
pkg.need_zip = false
|
22
|
+
pkg.need_tar = false
|
23
|
+
end
|
24
|
+
|
25
25
|
task :default => [:test]
|
26
26
|
task :test => [:spec]
|
data/lib/timelord.rb
CHANGED
@@ -1,14 +1,29 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
class Timelord
|
4
|
+
VERSION = "0.0.6"
|
4
5
|
|
5
6
|
SHORT_MONTHS = %w(jan feb mar apr may jun jul aug sep oct nov dec).freeze
|
7
|
+
LONG_MONTHS = %w(january febuary march april may june july august september october november december).freeze
|
6
8
|
SHORT_MATCHER = SHORT_MONTHS.join('|').freeze
|
9
|
+
LONG_MATCHER = LONG_MONTHS.join('|').freeze
|
7
10
|
ORDINAL_MATCHER = "st|nd|rd|th".freeze
|
8
11
|
DAY_NAMES = %w(monday tuesday wednesday thursday friday saturday sunday).freeze
|
9
12
|
SHORT_DAY_NAMES = %w(mon tue wed thu fri sat sun).freeze
|
10
13
|
DAY_MATCHER = (DAY_NAMES + SHORT_DAY_NAMES).join('|').freeze
|
11
14
|
|
15
|
+
def self.set_date(date)
|
16
|
+
@today = date
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.today
|
20
|
+
@today ||= Date.today
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.reset
|
24
|
+
@today = Date.today
|
25
|
+
end
|
26
|
+
|
12
27
|
# Parses a date str. Second parameter switches between international and american date formats.
|
13
28
|
#
|
14
29
|
# Timelord.parse("Tuesday").to_s # "2011-01-04"
|
@@ -17,7 +32,6 @@ class Timelord
|
|
17
32
|
#
|
18
33
|
# For more examples, check out the spec[https://github.com/halogenandtoast/timelord/blob/master/spec/timelord_spec.rb]
|
19
34
|
def self.parse(str, format = :international)
|
20
|
-
today = Date.today
|
21
35
|
if str =~ /\b(\d{4})\/(\d{1,2})\/(\d{1,2})\b/i
|
22
36
|
Date.civil($1.to_i, $2.to_i, $3.to_i)
|
23
37
|
elsif str =~ /\b(\d{4})\-(\d{1,2})\-(\d{1,2})\b/i
|
@@ -41,6 +55,12 @@ class Timelord
|
|
41
55
|
elsif str =~ /\b(#{SHORT_MATCHER})\s+(\d{1,2})\b/i
|
42
56
|
date = Date.civil(today.year, SHORT_MONTHS.index($1.downcase) + 1, $2.to_i)
|
43
57
|
set_to_future(date)
|
58
|
+
elsif str =~ /\b(\d{1,2})\s+(#{LONG_MATCHER})\b/i
|
59
|
+
date = Date.civil(today.year, LONG_MONTHS.index($2.downcase) + 1, $1.to_i)
|
60
|
+
set_to_future(date)
|
61
|
+
elsif str =~ /\b(#{LONG_MATCHER})\s+(\d{1,2})\b/i
|
62
|
+
date = Date.civil(today.year, LONG_MONTHS.index($1.downcase) + 1, $2.to_i)
|
63
|
+
set_to_future(date)
|
44
64
|
elsif str =~ /\b(\d{1,2})(#{ORDINAL_MATCHER})\b/i
|
45
65
|
date = Date.civil(today.year, today.month, $1.to_i)
|
46
66
|
set_to_future(date)
|
@@ -67,10 +87,6 @@ class Timelord
|
|
67
87
|
|
68
88
|
private
|
69
89
|
|
70
|
-
def self.today
|
71
|
-
Date.today
|
72
|
-
end
|
73
|
-
|
74
90
|
def self.next_weekday(date_index)
|
75
91
|
current_date = today.strftime("%A").downcase
|
76
92
|
current_index = DAY_NAMES.index(current_date)
|
data/spec/timelord_spec.rb
CHANGED
@@ -6,6 +6,13 @@ describe Timelord, 'parse' do
|
|
6
6
|
Timecop.freeze(local_time)
|
7
7
|
end
|
8
8
|
|
9
|
+
it "can set today to a different value" do
|
10
|
+
actual_date = Time.local(2010,12,5,10,5,0)
|
11
|
+
Timelord.set_date(actual_date)
|
12
|
+
Timelord.parse("today").should == actual_date
|
13
|
+
Timelord.set_date(Date.today)
|
14
|
+
end
|
15
|
+
|
9
16
|
it "returns nil when no time is present" do
|
10
17
|
Timelord.parse("No time here").should == nil
|
11
18
|
end
|
@@ -118,6 +125,11 @@ describe Timelord, 'parse' do
|
|
118
125
|
Timelord.parse("On 1 Jan I need to do something.").should == first_of_january
|
119
126
|
end
|
120
127
|
|
128
|
+
it 'parses month_name day format' do
|
129
|
+
first_of_march = Date.civil(2011,3,1)
|
130
|
+
Timelord.parse("March 1 I need to do stuff.").should == first_of_march
|
131
|
+
end
|
132
|
+
|
121
133
|
after do
|
122
134
|
Timecop.return
|
123
135
|
end
|
metadata
CHANGED
@@ -1,72 +1,83 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: timelord
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 5
|
10
|
-
version: 0.0.5
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Matthew Mongeau
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
date: 2012-02-11 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: timecop
|
16
|
+
requirement: &70233931643220 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70233931643220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70233931641880 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70233931641880
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70233931640280 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70233931640280
|
22
47
|
description:
|
23
48
|
email: halogenandtoast@gmail.com
|
24
49
|
executables: []
|
25
|
-
|
26
50
|
extensions: []
|
27
|
-
|
28
51
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
52
|
+
files:
|
31
53
|
- README.rdoc
|
32
54
|
- LICENSE
|
33
55
|
- Rakefile
|
34
56
|
- lib/timelord.rb
|
35
57
|
- spec/spec_helper.rb
|
36
58
|
- spec/timelord_spec.rb
|
37
|
-
has_rdoc: true
|
38
59
|
homepage: http://github.com/halogenandtoast/timelord
|
39
60
|
licenses: []
|
40
|
-
|
41
61
|
post_install_message:
|
42
62
|
rdoc_options: []
|
43
|
-
|
44
|
-
require_paths:
|
63
|
+
require_paths:
|
45
64
|
- lib
|
46
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
66
|
none: false
|
48
|
-
requirements:
|
49
|
-
- -
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
|
52
|
-
|
53
|
-
- 0
|
54
|
-
version: "0"
|
55
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
72
|
none: false
|
57
|
-
requirements:
|
58
|
-
- -
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
|
61
|
-
segments:
|
62
|
-
- 0
|
63
|
-
version: "0"
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
64
77
|
requirements: []
|
65
|
-
|
66
78
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.
|
79
|
+
rubygems_version: 1.8.16
|
68
80
|
signing_key:
|
69
81
|
specification_version: 3
|
70
82
|
summary: Pull dates out of strings
|
71
83
|
test_files: []
|
72
|
-
|