Class: Namae::Name
- Inherits:
-
Struct
- Object
- Struct
- Namae::Name
- Includes:
- NameFormatting
- 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
-
#empty? ⇒ Boolean
Whether or not all the name components are nil.
-
#initialize(attributes = {}, sanitize = false) ⇒ Name
constructor
A new instance of Name.
-
#inspect ⇒ String
A string representation of the name.
-
#merge(other) ⇒ self
Merges the name with the passed-in name or hash.
- #merge_particles! ⇒ Object
- #normalize_initials(options = {}) ⇒ Object
-
#values_at(selector, ...) ⇒ Array
Returns an array containing the elements in self corresponding to the given selector(s).
Methods included from NameFormatting
#display_order, #initials, #sort_order
Constructor Details
#initialize(attributes = {}, sanitize = false) ⇒ Name
Returns a new instance of Name.
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/namae/name.rb', line 127 def initialize(attributes = {}, sanitize = false) super(*attributes.values_at(*Name.parts)) if sanitize && suffix && !given && family tokens = family.split(/\s+/) # Display-order plus comma suffix special case if tokens.length > 1 self.family = tokens.pop self.given = tokens.join(' ') end end end |
Class Attribute Details
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
102 103 104 |
# File 'lib/namae/name.rb', line 102 def defaults @defaults end |
.parts ⇒ Object (readonly)
Returns the value of attribute parts.
102 103 104 |
# File 'lib/namae/name.rb', line 102 def parts @parts end |
Instance Attribute Details
#appellation ⇒ Object
Returns the value of attribute appellation
85 86 87 |
# File 'lib/namae/name.rb', line 85 def appellation @appellation end |
#dropping_particle ⇒ Object
Returns the value of attribute dropping_particle
85 86 87 |
# File 'lib/namae/name.rb', line 85 def dropping_particle @dropping_particle end |
#family ⇒ Object
Returns the value of attribute family
85 86 87 |
# File 'lib/namae/name.rb', line 85 def family @family end |
#given ⇒ Object
Returns the value of attribute given
85 86 87 |
# File 'lib/namae/name.rb', line 85 def given @given end |
#nick ⇒ Object
Returns the value of attribute nick
85 86 87 |
# File 'lib/namae/name.rb', line 85 def nick @nick end |
#particle ⇒ Object
Returns the value of attribute particle
85 86 87 |
# File 'lib/namae/name.rb', line 85 def particle @particle end |
#suffix ⇒ Object
Returns the value of attribute suffix
85 86 87 |
# File 'lib/namae/name.rb', line 85 def suffix @suffix end |
#title ⇒ Object
Returns the value of attribute title
85 86 87 |
# File 'lib/namae/name.rb', line 85 def title @title end |
Class Method Details
.parse(name) ⇒ Name
Returns the parsed name.
114 115 116 117 118 |
# File 'lib/namae/name.rb', line 114 def parse(name) parse!(name) rescue new end |
Instance Method Details
#empty? ⇒ Boolean
Returns whether or not all the name components are nil.
142 143 144 |
# File 'lib/namae/name.rb', line 142 def empty? values.compact.empty? end |
#inspect ⇒ String
Returns a string representation of the name.
194 195 196 |
# File 'lib/namae/name.rb', line 194 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.
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/namae/name.rb', line 150 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 |
#merge_particles! ⇒ Object
186 187 188 189 190 191 |
# File 'lib/namae/name.rb', line 186 def merge_particles! self.family = [dropping_particle, particle, family].compact.join(' ') self.dropping_particle = nil self.particle = nil self end |