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

Class Method Details

+ (Object) currency(num)

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

+ (Object) last_name(name)

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

+ (Object) last_name_first_name(name)

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

Returns an html link constructed from link, linkname



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

def link(linkname, href)
  "<a href='#{href}' title='#{linkname}'>#{linkname}</a>"
end

+ (Object) method_missing(method)

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



46
47
48
# File 'lib/table_fu/formatting.rb', line 46

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