Module: ArToHtmlTable::ColumnFormatter
- Included in:
- ArToHtmlTable::ColumnFormats::ClassMethods
- Defined in:
- lib/ar_to_html_table/column_formatter.rb
Constant Summary collapse
- MIN_PERCENT_BAR_VALUE =
Below which no bar is drawn
2.0
- REDUCTION_FACTOR =
Scale the bar graps so they have room for the percentage number in most cases
0.80
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#bar_and_percentage(val, options) ⇒ Object
Formats a number as a horizontal CSS-based bar followed by the number formatted as a percentage.
-
#currency_without_sign(val, options) ⇒ Object
Formats a number as an float with a delimiter and precision of 2.
-
#float_with_precision(val, options) ⇒ Object
Formats a number as an float with a delimiter and precision of 1.
-
#group_not_set_on_blank(val, options) ⇒ Object
:nodoc:.
-
#hours_to_time(val, options) ⇒ Object
Interprets an integer as a number of hours and outputs the value in the format hh:00.
-
#integer_with_delimiter(val, options = {}) ⇒ Object
Formats a number as an integer with a delimiter.
-
#long_date(val, options) ⇒ Object
Display as a long date (localized).
-
#long_day_name(val, options) ⇒ Object
Display as a long day name (localized).
-
#long_month_name(val, options) ⇒ Object
Display as a long month name (localized).
-
#not_set_on_blank(val, options) ⇒ Object
If the value is #blank? then display a localized version of “Not Set”.
-
#ordinalize(val, options) ⇒ Object
Ordinalize a number (ie 1st, 2nd, 3rd, .…).
-
#percentage(val, options) ⇒ Object
Interprets an integer as a percentage with a single digit of precision.
-
#seconds_to_time(val, options) ⇒ Object
Interprets an integer as a duration and outputs the duration in the format hh:mm:ss or mm:hh:ss if the time is less than an hour.
-
#short_date(val, options) ⇒ Object
Display as a short date (localized).
-
#short_day_name(val, options) ⇒ Object
Display as a short day name (localized).
-
#short_month_name(val, options) ⇒ Object
Display as a short month name (localized).
-
#unknown_on_blank(val, options) ⇒ Object
If the value is #blank? then display a localized version of “Unknown”.
Class Method Details
.included(base) ⇒ Object
:nodoc:
14 15 16 17 18 19 20 21 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 14 def self.included(base) #:nodoc: #base.class_eval do # extend ActiveSupport::Memoizable # memoize :integer_with_delimiter # memoize :float_with_precision # memoize :currency_without_sign #end end |
Instance Method Details
#bar_and_percentage(val, options) ⇒ Object
Formats a number as a horizontal CSS-based bar followed by the number formatted as a percentage.
Examples
# Given a value of 11, the formatter will output
<tt><div class="hbar" style="width:#{width}%"> </div>
<div>11%</div></tt>
val: the value to be formatted
options: formatter options
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 278 def (val, ) if [:cell_type] == :td val = val.to_f width = val * (val) = (val > MIN_PERCENT_BAR_VALUE) ? "<div class=\"hbar\" style=\"width:#{width}%\"> </div>" : '' + "<div>" + percentage(val, :precision => 1) + "</div>" else percentage(val, :precision => 1) end end |
#currency_without_sign(val, options) ⇒ Object
Formats a number as an float with a delimiter and precision of 2.
Examples
# Given a value of 1245, the formatter will output
1,345.00
val: the value to be formatted
options: formatter options
263 264 265 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 263 def currency_without_sign(val, ) number_with_precision(val.to_f, :precision => 2) end |
#float_with_precision(val, options) ⇒ Object
Formats a number as an float with a delimiter and precision of 1.
Examples
# Given a value of 1245, the formatter will output
1,345.0
val: the value to be formatted
options: formatter options
250 251 252 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 250 def float_with_precision(val, ) number_with_precision(val.to_f, :precision => 1) end |
#group_not_set_on_blank(val, options) ⇒ Object
:nodoc:
46 47 48 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 46 def group_not_set_on_blank(val, ) #:nodoc: # Need more context to do this end |
#hours_to_time(val, options) ⇒ Object
Interprets an integer as a number of hours and outputs the value in the format hh:00
Examples
# Given a value of 11, the formatter will output
=> 11:00
val: the value to be formatted
options: formatter options
107 108 109 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 107 def hours_to_time(val, ) "#{"%02d" % val}:00" end |
#integer_with_delimiter(val, options = {}) ⇒ Object
Formats a number as an integer with a delimiter. If Cldr::Format module is included into I18n then the value is localized (recommended for multilanguage applications). If not, number_with_delimiter is used formatting.
Examples
# Given a value of 1245 and no Cldr::Format, the formatter will output
1,345
val: the value to be formatted
options: formatter options
– TODO this should be done just once at instantiation but we have a potential ordering issue since I18n initializer may not have run yet (needs to be checked)
233 234 235 236 237 238 239 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 233 def integer_with_delimiter(val, = {}) if I18n::Backend::Simple.included_modules.include? Cldr::Format I18n.localize(val.to_i, :format => :short) else number_with_delimiter(val.to_i) end end |
#long_date(val, options) ⇒ Object
Display as a long date (localized)
Examples
# Given a value of 2010/10/1, the formatter will output
=> October 1, 2010
val: the value to be formatted
options: formatter options
147 148 149 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 147 def long_date(val, ) val ? val.to_date.to_s(:long) : val end |
#long_day_name(val, options) ⇒ Object
Display as a long day name (localized)
Examples
# Given a value of 1, the formatter will output
=> 'Monday'
val: the value to be formatted
options: formatter options
186 187 188 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 186 def long_day_name(val, ) val ? I18n.t('date.day_names')[val.to_i] : val end |
#long_month_name(val, options) ⇒ Object
Display as a long month name (localized)
Examples
# Given a value of 9, the formatter will output
=> 'September'
val: the value to be formatted
options: formatter options
160 161 162 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 160 def long_month_name(val, ) val ? I18n.t('date.month_names')[val.to_i] : val end |
#not_set_on_blank(val, options) ⇒ Object
If the value is #blank? then display a localized version of “Not Set”.
Examples
# Given a value <em>nil</em> the following will be output
# if the locale is set to "en" and the default translations
# are not changed:
(Not Set)
val: the value to be formatted
options: formatter options
38 39 40 41 42 43 44 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 38 def not_set_on_blank(val, ) if [:cell_type] == :th val else val.blank? ? I18n.t([:not_set_key] || 'tables.not_set') : val end end |
#ordinalize(val, options) ⇒ Object
Ordinalize a number (ie 1st, 2nd, 3rd, .…). Localization is handled externally to this method
Examples
# Given a value of 14, the formatter will output
=> 14th
val: the value to be formatted
options: formatter options
121 122 123 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 121 def ordinalize(val, ) val ? val.to_i.ordinalize : val end |
#percentage(val, options) ⇒ Object
Interprets an integer as a percentage with a single digit of precision. Shim for #number_to_percentage
Examples
# Given a value of 48, the formatter will output
=> 48.00%
val: the value to be formatted
options: formatter options
213 214 215 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 213 def percentage(val, ) number_to_percentage(val ? val.to_f : 0, :precision => 1) end |
#seconds_to_time(val, options) ⇒ Object
Interprets an integer as a duration and outputs the duration in the format hh:mm:ss or mm:hh:ss if the time is less than an hour.
Examples
# Given a value of 3600, the formatter will output
=> 05:00
# Given a value of 3600, the formatter will output
=> 05:00
val: the value to be formatted
options: formatter options
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 83 def seconds_to_time(val, ) val = val.to_i hours = val / 3600 minutes = (val / 60) - (hours * 60) seconds = val % 60 (minutes += 1; seconds = 0) if seconds == 60 (hours += 1; minutes = 0) if minutes == 60 if hours > 0 "#{"%02d" % hours}:#{"%02d" % minutes}:#{"%02d" % seconds}" else "#{"%02d" % minutes}:#{"%02d" % seconds}" end end |
#short_date(val, options) ⇒ Object
Display as a short date (localized)
Examples
# Given a value of 2010/10/1, the formatter will output
=> 1 Oct
val: the value to be formatted
options: formatter options
134 135 136 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 134 def short_date(val, ) val ? val.to_date.to_s(:short) : val end |
#short_day_name(val, options) ⇒ Object
Display as a short day name (localized)
Examples
# Given a value of 1, the formatter will output
=> 'Mon'
val: the value to be formatted
options: formatter options
199 200 201 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 199 def short_day_name(val, ) val ? I18n.t('date.abbr_day_names')[val.to_i] : val end |
#short_month_name(val, options) ⇒ Object
Display as a short month name (localized)
Examples
# Given a value of 9, the formatter will output
=> 'Sep'
val: the value to be formatted
options: formatter options
173 174 175 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 173 def short_month_name(val, ) val ? I18n.t('date.abbr_month_names')[val.to_i] : val end |
#unknown_on_blank(val, options) ⇒ Object
If the value is #blank? then display a localized version of “Unknown”.
Examples
# Given a value _nil_ the following will be output
# if the locale is set to "en" and the default translations
# are not changed:
(Unknown)
val: the value to be formatted
options: formatter options
62 63 64 65 66 67 68 |
# File 'lib/ar_to_html_table/column_formatter.rb', line 62 def unknown_on_blank(val, ) if [:cell_type] == :th val else val.blank? ? I18n.t([:unknown_key] || 'tables.unknown') : val end end |