Class: Namae::Name

Inherits:
Struct
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NameFormatting

#display_order, #initials, #sort_order

Constructor Details

#initialize(attributes = {}, sanitize = false) ⇒ Name

Returns a new instance of Name.

Examples:

Name.new(:family => 'Matsumoto')

Parameters:

  • attributes (Hash) (defaults to: {})

    the individual parts of the name

  • sanitize (Boolean) (defaults to: false)

    whether or not to apply extra sanitation rules



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

.defaultsObject (readonly)

Returns the value of attribute defaults.



102
103
104
# File 'lib/namae/name.rb', line 102

def defaults
  @defaults
end

.partsObject (readonly)

Returns the value of attribute parts.



102
103
104
# File 'lib/namae/name.rb', line 102

def parts
  @parts
end

Instance Attribute Details

#appellationObject

Returns the value of attribute appellation

Returns:

  • (Object)

    the current value of appellation



85
86
87
# File 'lib/namae/name.rb', line 85

def appellation
  @appellation
end

#dropping_particleObject

Returns the value of attribute dropping_particle

Returns:

  • (Object)

    the current value of dropping_particle



85
86
87
# File 'lib/namae/name.rb', line 85

def dropping_particle
  @dropping_particle
end

#familyObject

Returns the value of attribute family

Returns:

  • (Object)

    the current value of family



85
86
87
# File 'lib/namae/name.rb', line 85

def family
  @family
end

#givenObject

Returns the value of attribute given

Returns:

  • (Object)

    the current value of given



85
86
87
# File 'lib/namae/name.rb', line 85

def given
  @given
end

#nickObject

Returns the value of attribute nick

Returns:

  • (Object)

    the current value of nick



85
86
87
# File 'lib/namae/name.rb', line 85

def nick
  @nick
end

#particleObject

Returns the value of attribute particle

Returns:

  • (Object)

    the current value of particle



85
86
87
# File 'lib/namae/name.rb', line 85

def particle
  @particle
end

#suffixObject

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



85
86
87
# File 'lib/namae/name.rb', line 85

def suffix
  @suffix
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of 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.

Parameters:

  • name (String)

    the name to be parsed

Returns:

  • (Name)

    the parsed name



114
115
116
117
118
# File 'lib/namae/name.rb', line 114

def parse(name)
  parse!(name)
rescue
  new
end

.parse!(name) ⇒ Name

Returns the parsed name.

Parameters:

  • name (String)

    the name to be parsed

Returns:

  • (Name)

    the parsed name

Raises:

  • (ArgumentError)

    if the name cannot be parsed or if the input contains more than a single name



108
109
110
# File 'lib/namae/name.rb', line 108

def parse!(name)
  Parser.instance.parse!(name)[0] || new
end

Instance Method Details

#empty?Boolean

Returns whether or not all the name components are nil.

Returns:

  • (Boolean)

    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

#inspectString

Returns a string representation of the name.

Returns:

  • (String)

    a string representation of the name



187
188
189
# File 'lib/namae/name.rb', line 187

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.

Parameters:

  • other (#each_pair)

    the other name or hash

Returns:

  • (self)

Raises:

  • (ArgumentError)


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

#normalize_initials(options = {}) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/namae/name.rb', line 178

def normalize_initials(options = {})
  return self if given.nil?

  options = Name.defaults[:initials].merge(options)
  self.given = existing_initials_of given, options
  self
end

#values_at(selector, ...) ⇒ Array

Returns the list of values.

Examples:

name.values_at(:family, :nick) #=> ['Matsumoto', 'Matz']

Returns an array containing the elements in self corresponding to the given selector(s). The selectors may be either integer indices, ranges (functionality inherited from Struct) or symbols idenifying valid keys.

Returns:

  • (Array)

    the list of values

See Also:

  • Struct#values_at


174
175
176
# File 'lib/namae/name.rb', line 174

def values_at(*arguments)
  super(*arguments.flatten.map { |k| k.is_a?(Symbol) ? Name.parts.index(k) : k })
end