datesplit 0.0.3 → 0.0.4

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/datesplit.rb +18 -16
  2. data/spec/datesplit_spec.rb +28 -12
  3. metadata +3 -3
data/lib/datesplit.rb CHANGED
@@ -1,41 +1,43 @@
1
1
  require 'date'
2
2
  class Datesplit
3
+ include Enumerable
3
4
  def initialize(date_string)
4
5
  @date_string = String.new(date_string)
5
6
  end
6
7
 
7
8
  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_only) || returns.index(:year_one)]}")
12
- break
13
- end
14
- end
15
- raise "Can't process #{@date_string}" unless @start_date
16
- return @start_date
9
+ range.begin
17
10
  end
18
11
 
19
12
  def end_date
13
+ range.end
14
+ end
15
+
16
+ def range
17
+ return if @range
20
18
  expressions.each do |expression, returns|
21
19
  m = @date_string.match(expression)
22
20
  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_only) || returns.index(:year_two)]}")
21
+ @start = Date.parse("#{m.captures[returns.index(:month_only) || returns.index(:month_one)]} #{m.captures[returns.index(:day_one)]} #{m.captures[returns.index(:year_only) || returns.index(:year_one)]}")
22
+ @end = Date.parse("#{m.captures[returns.index(:month_only) || returns.index(:month_two) ]} #{m.captures[returns.index(:day_two)]} #{m.captures[returns.index(:year_only) || returns.index(:year_two)]}")
24
23
  break
25
24
  end
26
25
  end
27
- raise "Can't process #{@date_string}" unless @end_date
28
- return @end_date
29
- end
30
-
26
+ raise "Can't process #{@date_string}" unless @start && @end
27
+ @start..@end
28
+ end
29
+
31
30
  def number_of_days
32
- (end_date - start_date).to_i
31
+ range.to_a.size
33
32
  end
34
33
 
34
+ def each(&blk)
35
+ range.each(&blk)
36
+ end
37
+
35
38
  private
36
39
  def expressions
37
40
  {
38
-
39
41
  /(\w+)\.*(?:\t|\s)*(\d+)\w*(?:\t|\s)*(?:\-|to)(?:\t|\s)*(\d+)\w*,*(?:\t|\s)*(\d*)/ => [:month_only, :day_one, :day_two, :year_only], # September 13-15, 2011 (Year Optional)
40
42
  /(\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_only], # September 13 to October 15, 2011 (Year Optional)
41
43
  /(\w+)\.*(?:\t|\s)*(\d{1,2})\w*,*(?:\t|\s)*(\d{4})(?:\t|\s)*(?:\-|to)(?:\t|\s)*(\w+)\.*(?:\t|\s)*(\d+)\w*,*(?:\t|\s)*(\d{4})/ => [:month_one, :day_one, :year_one, :month_two, :day_two, :year_two] # September 13, 2012 to October 15 2013 (Year Required)
@@ -1,46 +1,62 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Datesplit do
4
- it "should return dates for 'April 11 to April 20, 2012'" do
4
+ it "handles a format of '<Month> <Day> to <Month> <Day>, <Year>'" do
5
5
  d = Datesplit.new('April 11 to April 20, 2012')
6
6
  d.start_date.to_s.should == Date.parse('April 11, 2012').to_s
7
7
  d.end_date.to_s.should == Date.parse('April 20, 2012').to_s
8
8
  end
9
9
 
10
- it "should return dates for 'April 11 to April 20'" do
10
+ it "handles a format of '<Month> <Day> to <Month> <Day>'" do
11
11
  d = Datesplit.new('April 11 to April 20')
12
12
  d.start_date.to_s.should == Date.parse('April 11').to_s
13
13
  d.end_date.to_s.should == Date.parse('April 20').to_s
14
14
  end
15
15
 
16
- it "should return dates for 'April 11th-20th 2012'" do
16
+ it "handles a format of '<Month> <Day>-<Day> <Year>'" do
17
17
  d = Datesplit.new('April 11th-20th 2012')
18
18
  d.start_date.to_s.should == Date.parse('April 11 2012').to_s
19
19
  d.end_date.to_s.should == Date.parse('April 20 2012').to_s
20
20
  end
21
21
 
22
- it "should return dates for 'April 11 to 20 2012'" do
22
+ it "handles a format of '<Month> <Day>-<Day>'" do
23
+ d = Datesplit.new('April 11th-20th')
24
+ d.start_date.to_s.should == Date.parse('April 11').to_s
25
+ d.end_date.to_s.should == Date.parse('April 20').to_s
26
+ end
27
+
28
+ it "handles a format of '<Month> <Day> to <Day> <Year>'" do
23
29
  d = Datesplit.new('April 11 to 20 2012')
24
30
  d.start_date.to_s.should == Date.parse('April 11 2012').to_s
25
31
  d.end_date.to_s.should == Date.parse('April 20 2012').to_s
26
32
  end
27
33
 
28
34
 
29
- it "should return dates for 'March 30-April 1, 2012'" do
35
+ it "handles a format of '<Month> <Day>-<Month> <Day>, <Year>'" do
30
36
  d = Datesplit.new('March 30-April 1, 2012')
31
37
  d.start_date.to_s.should == Date.parse('March 30 2012').to_s
32
38
  d.end_date.to_s.should == Date.parse('April 1 2012').to_s
33
39
  end
34
40
 
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
-
41
- it "should return dates for 'April 9, 2012 to January 1st 2013'" do
41
+ it "handles a format of '<Month> <Day>, <Year> to <Month> <Day> <Year>'" do
42
42
  d = Datesplit.new('April 9, 2012 to January 1st 2013')
43
43
  d.start_date.to_s.should == Date.parse('April 9 2012').to_s
44
44
  d.end_date.to_s.should == Date.parse('January 1 2013').to_s
45
45
  end
46
+
47
+ it "returns the correct number of days" do
48
+ d = Datesplit.new('April 9 to 13')
49
+ d.number_of_days.should == 5
50
+ end
51
+
52
+ it "responds properly to each" do
53
+ d = Datesplit.new('April 9 to 13')
54
+ total = 0
55
+ d.each_with_index do |date, i|
56
+ date.should be_a(Date)
57
+ total = i
58
+ end
59
+ total.should == 4
60
+ end
61
+
46
62
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sean Roberts
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-09-14 00:00:00 -04:00
17
+ date: 2011-09-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20