Class: TableFu::Formatting

Inherits:
Object
  • Object
show all
Defined in:
lib/table_fu/formatting.rb

Overview

Override this class to add more formatting methods

Methods expect one or more arguments, which could be nil, and should return the appropriate formatting and style.

Class Method Summary collapse

Class Method Details

.currency(num) ⇒ Object

Returns a currency formatted number



10
11
12
13
14
15
16
17
18
# File 'lib/table_fu/formatting.rb', line 10

def currency(num)
  begin
    parts = num.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
    "$#{parts.join('.')}"
  rescue
    num
  end
end

.last_name(name) ⇒ Object

Returns the last name of a name

=> last_name("Jeff Larson")
>> Larson


23
24
25
26
27
28
29
30
# File 'lib/table_fu/formatting.rb', line 23

def last_name(name)
  name.strip!
  if name.match(/\s(\w+)$/)
    $1
  else
    name
  end
end

.last_name_first_name(name) ⇒ Object

Returns that last name first of a name

=> last_name_first_name("Jeff Larson")
>> Larson, Jeff


34
35
36
37
38
# File 'lib/table_fu/formatting.rb', line 34

def last_name_first_name(name)
  last = last_name(name)
  first = name.gsub(last, '').strip    
  "#{last}, #{first}"
end

.method_missing(method, *args) ⇒ Object

Returns an error message if the given formatter isn’t available



41
42
43
# File 'lib/table_fu/formatting.rb', line 41

def method_missing(method, *args)
  "#{method.to_s} not a valid formatter!"
end