Class: NameableRecord::Name

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(last, first, prefix = nil, middle = nil, suffix = nil) ⇒ Name

Returns a new instance of Name.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nameable_record/name.rb', line 5

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

  @name_pattern_map = {
      /%l/ => last || "",
      /%f/ => first || "",
      /%m/ => middle || "",
      /%p/ => prefix || "",
      /%s/ => suffix || "",
  }

  @@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"
  }

  self.freeze
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



3
4
5
# File 'lib/nameable_record/name.rb', line 3

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



3
4
5
# File 'lib/nameable_record/name.rb', line 3

def last
  @last
end

#middleObject (readonly)

Returns the value of attribute middle.



3
4
5
# File 'lib/nameable_record/name.rb', line 3

def middle
  @middle
end

#prefixObject (readonly)

Returns the value of attribute prefix.



3
4
5
# File 'lib/nameable_record/name.rb', line 3

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



3
4
5
# File 'lib/nameable_record/name.rb', line 3

def suffix
  @suffix
end

Instance Method Details

#to_s(pattern = "") ⇒ Object

Creates a name based on pattern provided. Defaults to given + additional given + family names concatentated.

Symbols:

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


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

def to_s( pattern="" )
  if pattern.is_a?( Symbol )
    to_return = @@predefined_patterns[pattern]
  else
    to_return = pattern
  end
  to_return = @@predefined_patterns[:full] if to_return.empty?

  @name_pattern_map.each do |pat, replacement|
    to_return = to_return.gsub( pat, replacement )
  end

  to_return.strip
end