Skip to content

Commit

Permalink
time formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
arches committed Jun 19, 2012
1 parent 025b210 commit d8ef6fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/formatter.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,17 @@
require 'config' require 'config'


module TablePrint 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 class NoNewlineFormatter
def format(value) def format(value)
value.to_s.gsub(/\r\n/, "\n").gsub(/\n/, " ") value.to_s.gsub(/\r\n/, "\n").gsub(/\n/, " ")
Expand Down
23 changes: 22 additions & 1 deletion spec/formatter_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@


include TablePrint 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 describe TablePrint::NoNewlineFormatter do
before(:each) do before(:each) do
@f = TablePrint::NoNewlineFormatter.new @f = TablePrint::NoNewlineFormatter.new
end end


describe "#formater" do describe "#format" do
it "replaces carriage returns with spaces" do it "replaces carriage returns with spaces" do
@f.format("foo\r\nbar").should == "foo bar" @f.format("foo\r\nbar").should == "foo bar"
end end
Expand Down

0 comments on commit d8ef6fe

Please sign in to comment.