Class: NameableRecord::Name
- Inherits:
-
Object
- Object
- NameableRecord::Name
- 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
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#middle ⇒ Object
readonly
Returns the value of attribute middle.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Instance Method Summary collapse
- #<=>(another_name) ⇒ Object
- #==(another_name) ⇒ Object
- #eql?(another_name) ⇒ Boolean
-
#initialize(*args) ⇒ Name
constructor
A new instance of Name.
-
#to_s(pattern = '%l, %f') ⇒ Object
Creates a name based on pattern provided.
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
#first ⇒ Object (readonly)
Returns the value of attribute first.
5 6 7 |
# File 'lib/nameable_record/name.rb', line 5 def first @first end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
5 6 7 |
# File 'lib/nameable_record/name.rb', line 5 def last @last end |
#middle ⇒ Object (readonly)
Returns the value of attribute middle.
5 6 7 |
# File 'lib/nameable_record/name.rb', line 5 def middle @middle end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
5 6 7 |
# File 'lib/nameable_record/name.rb', line 5 def prefix @prefix end |
#suffix ⇒ Object (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 |
#eql?(another_name) ⇒ Boolean
21 22 23 |
# File 'lib/nameable_record/name.rb', line 21 def eql?( another_name ) self == another_name 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 |
# File 'lib/nameable_record/name.rb', line 38 def to_s( pattern='%l, %f' ) pattern = PREDEFINED_PATTERNS[pattern] if pattern.is_a?( Symbol ) PATTERN_MAP.inject( pattern ) do |name, mapping| name = name.gsub( mapping.first, (send( mapping.last ) || '') ) end end |