Skip to content

Commit bae1340

Browse files
committed
Merge pull request #12 from mrdougal/published-at-fixes
Fixes to meta 'published' and 'updated' methods, on ruby 2.0.0
2 parents f6c40e3 + 8d1f463 commit bae1340

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/pith/plugins/publication/input.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ def updated_at
2222
private
2323

2424
def parse_date(date_string)
25-
Time.parse(date_string) if date_string
25+
26+
return unless date_string
27+
28+
# yaml may parse the date for us, in which case
29+
# we will be passed a Time or Date object
30+
return date_string if date_string.instance_of? Time
31+
return date_string.to_time if date_string.respond_to?('to_time')
32+
33+
# cast to a string prior to casting
34+
# as ruby 1.8.7 Date doesn't respond to 'to_time'
35+
Time.parse(date_string.to_s)
36+
2637
end
2738

2839
end

spec/pith/plugins/publication_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
@template.published_at.should == Time.local(1999, 12, 25, 22, 30)
2020
end
2121

22+
it "honours any parsing (of Time) that yaml may do on the 'published' meta-field" do
23+
@template.meta["published"] = Time.local(1999, 12, 25, 22, 30)
24+
@template.published_at.should == Time.local(1999, 12, 25, 22, 30)
25+
end
26+
27+
it "honours any parsing (to Date) of time that yaml may do on the 'published' meta-field" do
28+
@template.meta["published"] = Date.new(1999, 12, 25)
29+
@template.published_at.should == Time.local(1999, 12, 25)
30+
end
31+
2232
end
2333

2434
describe "#updated_at" do
@@ -30,9 +40,14 @@
3040

3141
it "can be overridden with an 'updated' meta-field" do
3242
@template.meta["published"] = "25 Dec 1999 22:30"
33-
@template.meta["published"] = "1 Jan 2000 03:00"
43+
@template.meta["updated"] = "1 Jan 2000 03:00"
3444
@template.updated_at.should == Time.local(2000, 1, 1, 3, 0)
3545
end
46+
47+
it "honours any parsing of time that yaml may do on the 'updated' meta-field" do
48+
@template.meta["updated"] = Date.new(2000, 1, 1)
49+
@template.updated_at.should == Time.local(2000, 1, 1)
50+
end
3651

3752
end
3853

0 commit comments

Comments
 (0)