Permalink
Browse files

Fix #15: column-level strftime format

  • Loading branch information...
1 parent d8ef6fe commit 1cdd532ac688020082ae69e50146c30a85ce8b5d @arches committed Jun 19, 2012
Showing with 11 additions and 8 deletions.
  1. +1 −1 lib/column.rb
  2. +2 −1 lib/formatter.rb
  3. +3 −3 lib/row_group.rb
  4. +1 −1 spec/formatter_spec.rb
  5. +4 −2 spec/row_group_spec.rb
View
@@ -2,7 +2,7 @@ module TablePrint
class Column
attr_reader :formatters
attr_writer :width
- attr_accessor :name, :data
+ attr_accessor :name, :data, :time_format
def initialize(attr_hash={})
@formatters = []
View
@@ -2,8 +2,9 @@
module TablePrint
class TimeFormatter
- def initialize(time_format=TablePrint::Config.time_format)
+ def initialize(time_format=nil)
@format = time_format
+ @format ||= TablePrint::Config.time_format
end
def format(value)
View
@@ -208,10 +208,10 @@ def apply_formatters(column_name, value)
column_name = column_name.to_s
return value unless column_for(column_name)
- formatters = column_for(column_name).formatters || []
-
- value = value.strftime(TablePrint::Config.time_format) if value.is_a? Time
+ column = column_for(column_name)
+ formatters = column.formatters || []
+ formatters << TimeFormatter.new(column.time_format)
formatters << NoNewlineFormatter.new
formatters << FixedWidthFormatter.new(column_for(column_name).width)
View
@@ -3,7 +3,7 @@
include TablePrint
-describe TablePrint::TimeFormatter, :focus => true do
+describe TablePrint::TimeFormatter do
describe "#format" do
it "only operates on Time objects" do
f = TablePrint::TimeFormatter.new
View
@@ -168,9 +168,11 @@ def compare_rows(actual_rows, expected_rows)
end
it "uses the config'd time_format to format times" do
- row.stub(:column_for) {OpenStruct.new(:width => 20, :formatters => [])}
+ row.stub(:column_for) {OpenStruct.new(:width => 20, :formatters => [], :time_format => "%Y %m %d")}
- row.apply_formatters(:title, Time.local(2012, 6, 1, 14, 20, 20)).should == "2012-06-01 14:20:20 "
+ time_formatter = OpenStruct.new(:format => "")
+ TablePrint::TimeFormatter.should_receive(:new).with("%Y %m %d") {time_formatter}
+ row.apply_formatters(:title, Time.local(2012, 6, 1, 14, 20, 20))
end
end

0 comments on commit 1cdd532

Please sign in to comment.