Skip to content
Browse files

time formatter

  • Loading branch information...
1 parent 025b210 commit d8ef6fef3de92346e6bc83d63802ffc66785b53a @arches committed Jun 19, 2012
Showing with 33 additions and 1 deletion.
  1. +11 −0 lib/formatter.rb
  2. +22 −1 spec/formatter_spec.rb
View
11 lib/formatter.rb
@@ -1,6 +1,17 @@
require 'config'
module TablePrint
+ class TimeFormatter
+ def initialize(time_format=TablePrint::Config.time_format)
+ @format = time_format
+ end
+
+ def format(value)
+ return value unless value.is_a? Time
+ value.strftime @format
+ end
+ end
+
class NoNewlineFormatter
def format(value)
value.to_s.gsub(/\r\n/, "\n").gsub(/\n/, " ")
View
23 spec/formatter_spec.rb
@@ -3,12 +3,33 @@
include TablePrint
+describe TablePrint::TimeFormatter, :focus => true do
+ describe "#format" do
+ it "only operates on Time objects" do
+ f = TablePrint::TimeFormatter.new
+ f.format(12).should == 12
+ end
+
+ it "uses the config'd time_format" do
+ f = TablePrint::TimeFormatter.new
+ time = Time.local(2012, 01, 11, 1, 23, 45)
+ f.format(time).should == "2012-01-11 01:23:45" # default time format is set in config.rb
+ end
+
+ it "overrides the config'd time format with one it was passed" do
+ f = TablePrint::TimeFormatter.new("%Y")
+ time = Time.local(2012, 01, 11, 1, 23, 45)
+ f.format(time).should == "2012" # default time format is set in config.rb
+ end
+ end
+end
+
describe TablePrint::NoNewlineFormatter do
before(:each) do
@f = TablePrint::NoNewlineFormatter.new
end
- describe "#formater" do
+ describe "#format" do
it "replaces carriage returns with spaces" do
@f.format("foo\r\nbar").should == "foo bar"
end

0 comments on commit d8ef6fe

Please sign in to comment.
Something went wrong with that request. Please try again.