interpreted_date 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  class InterpretedDate
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -17,6 +17,32 @@ class InterpretedDate
17
17
  new(year: date.year, month: date.month, day: date.day)
18
18
  end
19
19
 
20
+ def decade=(value)
21
+ @decade = value.to_i if value
22
+ end
23
+
24
+ def year=(value)
25
+ @year = value.to_i if value
26
+ end
27
+
28
+ def month=(value)
29
+ if value.nil?
30
+ @month = nil
31
+ elsif value.to_i == 0
32
+ if Date::ABBR_MONTHNAMES.index(value) && Date::ABBR_MONTHNAMES.index(value) > 0
33
+ @month = Date::ABBR_MONTHNAMES.index(value)
34
+ elsif Date::MONTHNAMES.index(value) && Date::MONTHNAMES.index(value) > 0
35
+ @month = Date::MONTHNAMES.index(value)
36
+ end
37
+ else
38
+ @month = value.to_i
39
+ end
40
+ end
41
+
42
+ def day=(value)
43
+ @day = value.to_i if value
44
+ end
45
+
20
46
  def interpreted_year
21
47
  @interpreted_year ||= year || (decade && (decade + 5)) || DEFAULT_YEAR
22
48
  end
@@ -42,22 +68,25 @@ class InterpretedDate
42
68
 
43
69
  def to_s(options = {})
44
70
  preposition = options.fetch(:preposition) { false }
71
+ output = nil
45
72
  if day && month && year && date
46
- date.strftime("%B %-d, %Y")
73
+ output = preposition ? "on " : ""
74
+ output << date.strftime("%B %-d, %Y")
47
75
  elsif month && year && date
48
- date.strftime("%B %Y")
76
+ output = preposition ? "in " : ""
77
+ output << date.strftime("%B %Y")
49
78
  elsif year && date
50
79
  output = preposition ? "in " : ""
51
80
  output << date.strftime("%Y")
52
- output
53
81
  elsif decade
54
82
  output = preposition ? "in the " : ""
55
83
  output << decade.to_s
56
84
  output << "s"
57
- output
58
85
  elsif day && month && date
59
- date.strftime("%B %-d")
86
+ output = preposition ? "on " : ""
87
+ output << date.strftime("%B %-d")
60
88
  end
89
+ output
61
90
  end
62
91
 
63
92
  def self.from_json(json)
@@ -56,6 +56,11 @@ describe InterpretedDate do
56
56
  it "returns the right integer" do
57
57
  subject.to_i.should eq(Date.new(1955, 3).to_time.to_i)
58
58
  end
59
+
60
+ it "returns the right string with a preposition" do
61
+ subject.to_s(preposition: true).should eq("in March 1955")
62
+ end
63
+
59
64
  end
60
65
 
61
66
  describe "with decade, year, month and day" do
@@ -68,6 +73,11 @@ describe InterpretedDate do
68
73
  it "returns the right integer" do
69
74
  subject.to_i.should eq(Date.new(1955, 3, 1).to_time.to_i)
70
75
  end
76
+
77
+ it "returns the right string with a preposition" do
78
+ subject.to_s(preposition: true).should eq("on March 1, 1955")
79
+ end
80
+
71
81
  end
72
82
 
73
83
  describe "with month and day" do
@@ -80,6 +90,11 @@ describe InterpretedDate do
80
90
  it "returns nil integer" do
81
91
  subject.to_i.should be_nil
82
92
  end
93
+
94
+ it "returns the right string with a preposition" do
95
+ subject.to_s(preposition: true).should eq("on October 31")
96
+ end
97
+
83
98
  end
84
99
 
85
100
  describe "with invalid date" do
@@ -100,6 +115,18 @@ describe InterpretedDate do
100
115
  it "correctly parses the json" do
101
116
  subject.should == InterpretedDate.from_json('year' => 2012, 'month' => 2, 'day' => 31)
102
117
  end
118
+
119
+ it "correctly parses dates where values are strings" do
120
+ subject.should == InterpretedDate.from_json('year' => 2012, 'month' => '2', 'day' => 31)
121
+ end
122
+
123
+ it "correctly parses date where the month is given by name" do
124
+ subject.should == InterpretedDate.from_json('year' => 2012, 'month' => 'February', 'day' => 31)
125
+ end
126
+
127
+ it "correctly parses date where the month is given by abbreviated name" do
128
+ subject.should == InterpretedDate.from_json('year' => 2012, 'month' => 'Feb', 'day' => 31)
129
+ end
103
130
  end
104
131
 
105
132
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interpreted_date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: