Module: Jamef::Helper

Defined in:
lib/jamef/helper.rb

Constant Summary collapse

ACCENTS_MAPPING =
{
  'E' => [200,201,202,203],
  'e' => [232,233,234,235],
  'A' => [192,193,194,195,196,197],
  'a' => [224,225,226,227,228,229,230],
  'C' => [199],
  'c' => [231],
  'O' => [210,211,212,213,214,216],
  'o' => [242,243,244,245,246,248],
  'I' => [204,205,206,207],
  'i' => [236,237,238,239],
  'U' => [217,218,219,220],
  'u' => [249,250,251,252],
  'N' => [209],
  'n' => [241],
  'Y' => [221],
  'y' => [253,255],
  'AE' => [306],
  'ae' => [346],
  'OE' => [188],
  'oe' => [189]
}

Class Method Summary collapse

Class Method Details

.format_boolean(value) ⇒ Object



72
73
74
# File 'lib/jamef/helper.rb', line 72

def self.format_boolean value
  value ? 'S' : 'N'
end

.format_city(value) ⇒ Object



51
52
53
# File 'lib/jamef/helper.rb', line 51

def self.format_city value
  unaccent(value).upcase
end

.format_date(value) ⇒ Object



64
65
66
# File 'lib/jamef/helper.rb', line 64

def self.format_date value
  value.strftime("%d/%m/%Y")
end

.format_decimal(value) ⇒ Object



43
44
45
# File 'lib/jamef/helper.rb', line 43

def self.format_decimal value
  sprintf('%.2f', Float(value))
end

.format_document(value) ⇒ Object



59
60
61
# File 'lib/jamef/helper.rb', line 59

def self.format_document value
  only_numbers(value)
end

.format_state(value) ⇒ Object



55
56
57
# File 'lib/jamef/helper.rb', line 55

def self.format_state value
  value.upcase
end

.format_time(value) ⇒ Object



68
69
70
# File 'lib/jamef/helper.rb', line 68

def self.format_time value
  value.strftime("%H:%M")
end

.format_zip(value) ⇒ Object



47
48
49
# File 'lib/jamef/helper.rb', line 47

def self.format_zip value
  only_numbers(value)
end

.only_numbers(string) ⇒ Object



39
40
41
# File 'lib/jamef/helper.rb', line 39

def self.only_numbers string
  string.gsub(/\D*/,'')
end

.parse_date(value) ⇒ Object



76
77
78
# File 'lib/jamef/helper.rb', line 76

def self.parse_date value
  Date.strptime(value, '%d/%m/%y')
end

.parse_decimal(value) ⇒ Object



80
81
82
# File 'lib/jamef/helper.rb', line 80

def self.parse_decimal value
  Float(value)
end

.unaccent(string) ⇒ Object

Remove the accents from the string. Uses String::ACCENTS_MAPPING as the source map.



29
30
31
32
33
34
35
36
37
# File 'lib/jamef/helper.rb', line 29

def self.unaccent string
  str = string.dup
    ACCENTS_MAPPING.each {|letter,accents|
    packed = accents.pack('U*')
    rxp = Regexp.new("[#{packed}]", nil)
    str.gsub!(rxp, letter)
  }
  str
end