From 21da6930522e1b1d5a3d3a4ceafd785e16db6234 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Fri, 30 Nov 2012 12:07:42 +0100 Subject: [PATCH 1/9] default option --- lib/slither/column.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 3ebe9b9..658247b 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -15,6 +15,7 @@ def initialize(name, length, options = {}) @type = options[:type] || :string @padding = options[:padding] || :space @truncate = options[:truncate] || false + @default = options[:default] # Only used with floats, this determines the decimal places @precision = options[:precision] end @@ -44,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % to_s(value)) + pad(formatter % value? ? to_s(value): @default ) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end From 1a5716e4c0d418b618563db6cc478c3649244ea4 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Fri, 30 Nov 2012 14:01:58 +0100 Subject: [PATCH 2/9] default option --- lib/slither/column.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 658247b..92163e3 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % value? ? to_s(value): @default ) + pad(formatter % value.present? ? to_s(value): to_s(@default) ) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end From fcdf4476b3e3c8b700b068bb1a5339e1e3e8d3b1 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Fri, 30 Nov 2012 14:30:38 +0100 Subject: [PATCH 3/9] default option --- lib/slither/column.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 92163e3..82abd6f 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % value.present? ? to_s(value): to_s(@default) ) + pad(formatter % (value.nil? && @default? ? to_s(value): to_s(@default)) ) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end From e4b0030d60afe830070f1fa3ec632d2ce70c2e77 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Fri, 30 Nov 2012 14:32:44 +0100 Subject: [PATCH 4/9] default option --- lib/slither/column.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 82abd6f..8a85204 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % (value.nil? && @default? ? to_s(value): to_s(@default)) ) + pad(formatter % (value.nil? && @default.present? ? to_s(value) : to_s(@default)) ) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end From 1256e3e80f9ef91c7d937e4775b2d2a97e5b160b Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Fri, 30 Nov 2012 14:35:05 +0100 Subject: [PATCH 5/9] default option --- lib/slither/column.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 8a85204..9d2dc48 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % (value.nil? && @default.present? ? to_s(value) : to_s(@default)) ) + pad(formatter % (value.nil? && @default.present? ? to_s(@default) : to_s(value) ) ) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end From 8608087c78c56699bd0b8645b6448584852983b3 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Fri, 30 Nov 2012 14:52:54 +0100 Subject: [PATCH 6/9] readable default option --- lib/slither/column.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 9d2dc48..0bf0534 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % (value.nil? && @default.present? ? to_s(@default) : to_s(value) ) ) + pad(formatter % parsed_value ) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end @@ -59,6 +59,10 @@ def formatter def aligner @alignment == :left ? '-' : '' end + + def parsed_value + value.nil? && @default.present? ? to_s(@default) : to_s(value) + end def sizer (@type == :float && @precision) ? @precision : @length From 647a7d07d624c0e54c5e0fcbde1f6e953d89da6d Mon Sep 17 00:00:00 2001 From: Pedro Pablo Guijarro Date: Mon, 3 Dec 2012 16:59:08 +0100 Subject: [PATCH 7/9] ruby 1.8 UTF8 compatible --- lib/slither/column.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 0bf0534..8861cd4 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % parsed_value ) + pad(formatter % version_secure_format(value)) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end @@ -60,7 +60,19 @@ def aligner @alignment == :left ? '-' : '' end - def parsed_value + def version_secure_format(value) + if RUBY_VERSION < "1.9" && defined?(ActiveSupport) + parsed_value(value).mb_chars.send(alignment_secure_method,sizer) + else + formatter % parsed_value(value) + end + end + + def alignment_secure_method + @alignment == :left ? :ljust : :rjust; + end + + def parsed_value(value) value.nil? && @default.present? ? to_s(@default) : to_s(value) end From 0c7b627ce7d46c694d2e7378be311a2f9ffd0ea9 Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Mon, 3 Dec 2012 19:31:24 +0100 Subject: [PATCH 8/9] Avoid to format string twice --- lib/slither/column.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/slither/column.rb b/lib/slither/column.rb index 8861cd4..dccc016 100644 --- a/lib/slither/column.rb +++ b/lib/slither/column.rb @@ -45,7 +45,7 @@ def parse(value) end def format(value) - pad(formatter % version_secure_format(value)) + pad(version_secure_format(value)) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end From c71f4f6f69932148b138baa2238eae5922309f63 Mon Sep 17 00:00:00 2001 From: Richard Zheng Date: Thu, 20 Sep 2012 14:51:51 -1000 Subject: [PATCH 9/9] allow change new_line string --- lib/slither/generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/slither/generator.rb b/lib/slither/generator.rb index a2a54bb..2d6afb2 100644 --- a/lib/slither/generator.rb +++ b/lib/slither/generator.rb @@ -19,8 +19,8 @@ def generate(data) raise(Slither::RequiredSectionEmptyError, "Required section '#{section.name}' was empty.") unless section.optional end end - @builder.join("\n") + @builder.join(@definition.options[:new_line] || "\n") end end -end \ No newline at end of file +end