Class: Namae::Name
- Inherits:
-
Struct
- Object
- Struct
- Namae::Name
- Defined in:
- lib/namae/name.rb
Overview
A Name represents a single personal name, exposing its constituent parts (e.g., family name, given name etc.). Name instances are typically created and returned from Namae.parse.
name = Namae.parse('Yukihiro "Matz" Matsumoto')[0]
name.family #=> Matsumoto
name.nick #=> Matz
name.given #=> Yukihiro
Class Attribute Summary collapse
-
.defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
.parts ⇒ Object
readonly
Returns the value of attribute parts.
Instance Attribute Summary collapse
-
#appellation ⇒ Object
Returns the value of attribute appellation.
-
#dropping_particle ⇒ Object
Returns the value of attribute dropping_particle.
-
#family ⇒ Object
Returns the value of attribute family.
-
#given ⇒ Object
Returns the value of attribute given.
-
#nick ⇒ Object
Returns the value of attribute nick.
-
#particle ⇒ Object
Returns the value of attribute particle.
-
#suffix ⇒ Object
Returns the value of attribute suffix.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
-
.parse(name) ⇒ Name
The parsed name.
-
.parse!(name) ⇒ Name
The parsed name.
Instance Method Summary collapse
-
#display_order ⇒ String
(also: #to_s)
The name in display order.
-
#empty? ⇒ Boolean
Whether or not all the name components are nil.
-
#initialize(attributes = {}) ⇒ Name
constructor
A new instance of Name.
-
#initials(options = {}) ⇒ String
The name’s initials.
-
#inspect ⇒ String
A string representation of the name.
-
#merge(other) ⇒ self
Merges the name with the passed-in name or hash.
-
#sort_order(delimiter = ', ') ⇒ String
The name in sort order.
-
#values_at(selector, ...) ⇒ Array
Returns an array containing the elements in self corresponding to the given selector(s).
Constructor Details
Class Attribute Details
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
28 29 30 |
# File 'lib/namae/name.rb', line 28 def defaults @defaults end |
.parts ⇒ Object (readonly)
Returns the value of attribute parts.
28 29 30 |
# File 'lib/namae/name.rb', line 28 def parts @parts end |
Instance Attribute Details
#appellation ⇒ Object
Returns the value of attribute appellation
13 14 15 |
# File 'lib/namae/name.rb', line 13 def appellation @appellation end |
#dropping_particle ⇒ Object
Returns the value of attribute dropping_particle
13 14 15 |
# File 'lib/namae/name.rb', line 13 def dropping_particle @dropping_particle end |
#family ⇒ Object
Returns the value of attribute family
13 14 15 |
# File 'lib/namae/name.rb', line 13 def family @family end |
#given ⇒ Object
Returns the value of attribute given
13 14 15 |
# File 'lib/namae/name.rb', line 13 def given @given end |
#nick ⇒ Object
Returns the value of attribute nick
13 14 15 |
# File 'lib/namae/name.rb', line 13 def nick @nick end |
#particle ⇒ Object
Returns the value of attribute particle
13 14 15 |
# File 'lib/namae/name.rb', line 13 def particle @particle end |
#suffix ⇒ Object
Returns the value of attribute suffix
13 14 15 |
# File 'lib/namae/name.rb', line 13 def suffix @suffix end |
#title ⇒ Object
Returns the value of attribute title
13 14 15 |
# File 'lib/namae/name.rb', line 13 def title @title end |
Class Method Details
.parse(name) ⇒ Name
Returns the parsed name.
40 41 42 43 44 |
# File 'lib/namae/name.rb', line 40 def parse(name) parse!(name) rescue new end |
Instance Method Details
#display_order ⇒ String Also known as: to_s
Returns the name in display order.
61 62 63 |
# File 'lib/namae/name.rb', line 61 def display_order [given_part, family_part].reject(&:empty?).join(' ') end |
#empty? ⇒ Boolean
Returns whether or not all the name components are nil.
66 67 68 |
# File 'lib/namae/name.rb', line 66 def empty? values.compact.empty? end |
#initials(options = {}) ⇒ String
Returns the name’s initials.
93 94 95 96 97 98 99 100 101 |
# File 'lib/namae/name.rb', line 93 def initials( = {}) = Name.defaults[:initials].merge() if [:expand] [initials_of(given_part, ), family].compact.join(' ') else initials_of([given_part, family_part].join(' '), ) end end |
#inspect ⇒ String
Returns a string representation of the name.
120 121 122 |
# File 'lib/namae/name.rb', line 120 def inspect "#<Name #{each_pair.map { |k,v| [k,v.inspect].join('=') if v }.compact.join(' ')}>" end |
#merge(other) ⇒ self
Merges the name with the passed-in name or hash.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/namae/name.rb', line 74 def merge(other) raise ArgumentError, "failed to merge #{other.class} into Name" unless other.respond_to?(:each_pair) other.each_pair do |part, value| writer = "#{part}=" send(writer, value) if !value.nil? && respond_to?(writer) end self end |
#sort_order(delimiter = ', ') ⇒ String
Returns the name in sort order.
56 57 58 |
# File 'lib/namae/name.rb', line 56 def sort_order(delimiter = ', ') [family_part, given_part].reject(&:empty?).join(delimiter) end |