lib/hammer_cli/output/formatters.rb in hammer_cli-0.0.14 vs lib/hammer_cli/output/formatters.rb in hammer_cli-0.0.15
- old
+ new
@@ -1,22 +1,22 @@
module HammerCLI::Output
module Formatters
- # Registry for formatters
+ # Registry for formatters
class FormatterLibrary
def initialize(formatter_map={})
@_formatters = {}
- formatter_map.each do |type, formatters|
+ formatter_map.each do |type, formatters|
register_formatter(type, *Array(formatters))
end
end
def register_formatter(type, *formatters)
if @_formatters[type].nil?
@_formatters[type] = FormatterContainer.new *formatters
- else
+ else
formatters.each { |f| @_formatters[type].add_formatter(f) }
end
end
def formatter_for_type(type)
@@ -28,11 +28,11 @@
# All the tags the formatter has, needs to be present in the addapter.
# Otherwise the formatter won't apply. Formatters with :flat tag are used first
# as we expect them to serialize the value.
#
# - by format: :flat x :data
- # - by output: :file X :screen
+ # - by output: :file X :screen
# abstract formatter
class FieldFormatter
def tags
@@ -97,10 +97,16 @@
def tags
[:flat]
end
def format(list)
- list.join(", ") if list
+ if list.is_a? Array
+ list.join(", ")
+ elsif list
+ list.to_s
+ else
+ ""
+ end
end
end
HammerCLI::Output::Output.register_formatter(DateFormatter.new, :Date)
HammerCLI::Output::Output.register_formatter(ListFormatter.new, :List)