datesplit 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/datesplit.rb +42 -0
- data/spec/datesplit_spec.rb +40 -0
- data/spec/spec_helper.rb +7 -0
- metadata +69 -0
data/lib/datesplit.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'date'
|
2
|
+
class Datesplit
|
3
|
+
def initialize(date_string)
|
4
|
+
@date_string = String.new(date_string)
|
5
|
+
end
|
6
|
+
|
7
|
+
def start_date
|
8
|
+
expressions.each do |expression, returns|
|
9
|
+
m = @date_string.match(expression)
|
10
|
+
if m
|
11
|
+
@start_date = Date.parse("#{m.captures[returns.index(:month_only) || returns.index(:month_one)]} #{m.captures[returns.index(:day_one)]} #{m.captures[returns.index(:year)]}")
|
12
|
+
break
|
13
|
+
end
|
14
|
+
end
|
15
|
+
raise "Can't process #{@date_string}" unless @start_date
|
16
|
+
return @start_date
|
17
|
+
end
|
18
|
+
|
19
|
+
def end_date
|
20
|
+
expressions.each do |expression, returns|
|
21
|
+
m = @date_string.match(expression)
|
22
|
+
if m
|
23
|
+
@end_date = Date.parse("#{m.captures[returns.index(:month_only) || returns.index(:month_two) ]} #{m.captures[returns.index(:day_two)]} #{m.captures[returns.index(:year)]}")
|
24
|
+
break
|
25
|
+
end
|
26
|
+
end
|
27
|
+
raise "Can't process #{@date_string}" unless @end_date
|
28
|
+
return @end_date
|
29
|
+
end
|
30
|
+
|
31
|
+
def number_of_days
|
32
|
+
(end_date - start_date).to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def expressions
|
37
|
+
{
|
38
|
+
/(\w+)\.*(?:\t|\s)*(\d+)\w*(?:\t|\s)*(?:\-|to)(?:\t|\s)*(\d+)\w*,*(?:\t|\s)*(\d*)/ => [:month_only, :day_one, :day_two, :year], # September 13-15, 2011 (Year Optional)
|
39
|
+
/(\w+)\.*(?:\t|\s)*(\d+)\w*(?:\t|\s)*(?:\-|to)(?:\t|\s)*(\w+)\.*(?:\t|\s)*(\d+)\w*,*(?:\t|\s)*(\d*)/ => [:month_one, :day_one, :month_two, :day_two, :year] # September 13 to October 15, 2011 (Year Optional)
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Datesplit do
|
4
|
+
it "should return dates for 'April 11 to April 20, 2012'" do
|
5
|
+
d = Datesplit.new('April 11 to April 20, 2012')
|
6
|
+
d.start_date.to_s.should == Date.parse('April 11, 2012').to_s
|
7
|
+
d.end_date.to_s.should == Date.parse('April 20, 2012').to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return dates for 'April 11 to April 20'" do
|
11
|
+
d = Datesplit.new('April 11 to April 20')
|
12
|
+
d.start_date.to_s.should == Date.parse('April 11').to_s
|
13
|
+
d.end_date.to_s.should == Date.parse('April 20').to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return dates for 'April 11th-20th 2012'" do
|
17
|
+
d = Datesplit.new('April 11th-20th 2012')
|
18
|
+
d.start_date.to_s.should == Date.parse('April 11 2012').to_s
|
19
|
+
d.end_date.to_s.should == Date.parse('April 20 2012').to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return dates for 'April 11 to 20 2012'" do
|
23
|
+
d = Datesplit.new('April 11 to 20 2012')
|
24
|
+
d.start_date.to_s.should == Date.parse('April 11 2012').to_s
|
25
|
+
d.end_date.to_s.should == Date.parse('April 20 2012').to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
it "should return dates for 'March 30-April 1, 2012'" do
|
30
|
+
d = Datesplit.new('March 30-April 1, 2012')
|
31
|
+
d.start_date.to_s.should == Date.parse('March 30 2012').to_s
|
32
|
+
d.end_date.to_s.should == Date.parse('April 1 2012').to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return dates for 'Apr 27 to April 29, 2012'" do
|
36
|
+
d = Datesplit.new('Apr 27 to April 29, 2012')
|
37
|
+
d.start_date.to_s.should == Date.parse('Apr 27 2012').to_s
|
38
|
+
d.end_date.to_s.should == Date.parse('April 29, 2012').to_s
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datesplit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sean Roberts
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-14 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Generate date objects from a date duration string (eg. 'September 9th-12th, 2012')
|
22
|
+
email:
|
23
|
+
- roberts.sean@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/datesplit.rb
|
32
|
+
- spec/datesplit_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
homepage: http://github.com/seanroberts/datesplit
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.10
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Generate date objects from a date duration string (eg. 'September 9th-12th, 2012')
|
67
|
+
test_files:
|
68
|
+
- spec/datesplit_spec.rb
|
69
|
+
- spec/spec_helper.rb
|