Class: NameableRecord::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/nameable_record/name.rb

Constant Summary collapse

PREDEFINED_PATTERNS =
{
  :full                      => "%l, %f %s",
  :full_with_middle          => "%l, %f %m %s",
  :full_with_prefix          => "%l, %p %f %s",
  :full_sentence             => "%p %f %l %s",
  :full_sentence_with_middle => "%p %f %m %l %s"
}.freeze
PATTERN_MAP =
{
   /%l/ => :last,
   /%f/ => :first,
   /%m/ => :middle,
   /%p/ => :prefix,
   /%s/ => :suffix
}.freeze
PREFIX_BASE =

The order of this matters because of PREFIXES_CORRECTIONS

%w(Mr Mrs Miss Dr General Pastor Bishop Agent Rep)
SUFFIX_BASE =

The order of this matters because of SUFFIXES_CORRECTIONS

%w(Jr III V IV Esq MD DDS)
PREFIXES =
PREFIX_BASE.map { |p| [p, "#{p}.", p.upcase, "#{p.upcase}.", p.downcase, "#{p.downcase}."] }.flatten.sort
SUFFIXES =
SUFFIX_BASE.map { |p| [p, "#{p}.", p.upcase, "#{p.upcase}.", p.downcase, "#{p.downcase}."] }.flatten.sort
PREFIXES_CORRECTIONS =
SUFFIXES_CORRECTIONS =

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Name

Returns a new instance of Name.



7
8
9
10
11
# File 'lib/nameable_record/name.rb', line 7

def initialize( *args )
  @last, @first, @prefix, @middle, @suffix = args

  self.freeze
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



5
6
7
# File 'lib/nameable_record/name.rb', line 5

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



5
6
7
# File 'lib/nameable_record/name.rb', line 5

def last
  @last
end

#middleObject (readonly)

Returns the value of attribute middle.



5
6
7
# File 'lib/nameable_record/name.rb', line 5

def middle
  @middle
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/nameable_record/name.rb', line 5

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



5
6
7
# File 'lib/nameable_record/name.rb', line 5

def suffix
  @suffix
end

Instance Method Details

#<=>(another_name) ⇒ Object



25
26
27
# File 'lib/nameable_record/name.rb', line 25

def <=>( another_name )
  self.to_s( :full_with_middle ) <=> another_name.to_s( :full_with_middle )
end

#==(another_name) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/nameable_record/name.rb', line 13

def ==( another_name )
  return false unless another_name.is_a?( self.class )

  %w(last first middle suffix prefix).map do |attr|
    another_name.send( attr ) == send( attr )
  end.all? { |r| r == true }
end

#conversationalObject

Returns the name in a conversational format.



53
54
55
56
57
58
59
60
61
62
# File 'lib/nameable_record/name.rb', line 53

def conversational
  [
    prefix,
    first,
    middle,
    last,
    suffix
  ].reject( &:blank? ).
    join( ' ' )
end

#eql?(another_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/nameable_record/name.rb', line 21

def eql?( another_name )
  self == another_name
end

#sortableObject

Returns the name in a sortable format.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nameable_record/name.rb', line 66

def sortable
  [
    last,
    [
      prefix,
      first,
      middle,
      suffix
    ].reject( &:blank? ).
      join( ' ' )
  ].reject( &:blank? ).
    join( ', ' )
end

#to_s(pattern = '%l, %f') ⇒ Object

Creates a name based on pattern provided. Defaults to last, first.

Symbols:

%l - last name
%f - first name
%m - middle name
%p - prefix
%s - suffix


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nameable_record/name.rb', line 38

def to_s( pattern='%l, %f' )
  if pattern.is_a?( Symbol )
    return conversational if pattern == :conversational
    return sortable if pattern == :sortable
    pattern = PREDEFINED_PATTERNS[pattern]
  end

  PATTERN_MAP.inject( pattern ) do |name, mapping|
    name = name.gsub( mapping.first,
                      (send( mapping.last ) || '') )
  end
end