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

Instance Method Summary collapse

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}%">&nbsp;</div>
<div>11%</div></tt>

val: the value to be formatted
options: formatter options


271
272
273
274
275
276
277
278
279
# File 'lib/ar_to_html_table/column_formatter.rb', line 271

def bar_and_percentage(val, options)
  if options[:cell_type] == :td
    width = val * bar_reduction_factor(val)
    bar = (val.to_f > MIN_PERCENT_BAR_VALUE) ? "<div class=\"hbar\" style=\"width:#{width}%\">&nbsp;</div>" : ''
    bar + "<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


256
257
258
# File 'lib/ar_to_html_table/column_formatter.rb', line 256

def currency_without_sign(val, options)
  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


243
244
245
# File 'lib/ar_to_html_table/column_formatter.rb', line 243

def float_with_precision(val, options)
  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, options) #: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


100
101
102
# File 'lib/ar_to_html_table/column_formatter.rb', line 100

def hours_to_time(val, options)
  "#{"%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)



226
227
228
229
230
231
232
# File 'lib/ar_to_html_table/column_formatter.rb', line 226

def integer_with_delimiter(val, options = {})
  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


140
141
142
# File 'lib/ar_to_html_table/column_formatter.rb', line 140

def long_date(val, options)
  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


179
180
181
# File 'lib/ar_to_html_table/column_formatter.rb', line 179

def long_day_name(val, options)
  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


153
154
155
# File 'lib/ar_to_html_table/column_formatter.rb', line 153

def long_month_name(val, options)
  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, options)
  if options[:cell_type] == :th
    val
  else
    val.blank? ? I18n.t(options[: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


114
115
116
# File 'lib/ar_to_html_table/column_formatter.rb', line 114

def ordinalize(val, options)
  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


206
207
208
# File 'lib/ar_to_html_table/column_formatter.rb', line 206

def percentage(val, options)
  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

Examples

# Given a value of 3600, the formatter will output
=> 00:05:00

val: the value to be formatted
options: formatter options


80
81
82
83
84
85
86
87
88
# File 'lib/ar_to_html_table/column_formatter.rb', line 80

def seconds_to_time(val, options)
  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
  "#{"%02d" % hours}:#{"%02d" % minutes}:#{"%02d" % seconds}"
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


127
128
129
# File 'lib/ar_to_html_table/column_formatter.rb', line 127

def short_date(val, options)
  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


192
193
194
# File 'lib/ar_to_html_table/column_formatter.rb', line 192

def short_day_name(val, options)
  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


166
167
168
# File 'lib/ar_to_html_table/column_formatter.rb', line 166

def short_month_name(val, options)
  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, options)
  if options[:cell_type] == :th
    val
  else    
    val.blank? ? I18n.t(options[:unknown_key] || 'tables.unknown') : val
  end
end