Class: Namie::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/namie/formatter.rb

Overview

Format name

Constant Summary collapse

FORMATS =
{
  default: '%t %f %m %l %s',
  airport: '%t %l, %f %m %s',
  strip: '%f %l'
}
ABBR =
{
  t: :title,
  f: :first,
  m: :middle,
  l: :last,
  s: :suffix
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, format) ⇒ Formatter

Returns a new instance of Formatter.



19
20
21
22
23
24
# File 'lib/namie/formatter.rb', line 19

def initialize(name, format)
  @name = name
  format ||= :default
  @format = format.is_a?(Symbol) ? FORMATS[format] : format
  @out = @format.dup
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/namie/formatter.rb', line 4

def format
  @format
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/namie/formatter.rb', line 4

def name
  @name
end

#outObject

Returns the value of attribute out.



4
5
6
# File 'lib/namie/formatter.rb', line 4

def out
  @out
end

Instance Method Details

#to_sObject

%t Title %f First Name %m Middle Name(s) %l Last Name %s suffix



33
34
35
36
37
38
39
# File 'lib/namie/formatter.rb', line 33

def to_s
  ABBR.each do |k, v|
    val = name.send(v)
    out.gsub!("%#{k}", val || '')
  end
  out.gsub(/\s{2,}/, ' ').strip.chomp
end