class BibTeX::Name

A Name comprises individual name parts (first, last, prefix and suffix), but behaves almost like an atomic string value.

Constants

BibTeXML

Public Class Methods

looks_like?(thing) click to toggle source

Returns true if thing looks like a name. Actually converts thing to a string and tries to parse it.

# File lib/bibtex/names.rb, line 137
def looks_like?(thing)
  thing.respond_to?(:to_s) && [Name.new.parse(string)].flatten.compact.empty?
end
new(attributes = {}) click to toggle source
# File lib/bibtex/names.rb, line 142
def initialize(attributes = {})
  set(attributes)
end
parse(string) click to toggle source
# File lib/bibtex/names.rb, line 131
def parse(string)
  [NameParser.new.parse(string)].flatten[0]
end

Public Instance Methods

<=>(other) click to toggle source
Calls superclass method
# File lib/bibtex/names.rb, line 244
def <=>(other)
  other.is_a?(Name) ? sort_order <=> other.sort_order : super
end
blank?() click to toggle source
# File lib/bibtex/names.rb, line 161
def blank?
  to_a.compact.empty?
end
convert(*filters) click to toggle source
# File lib/bibtex/names.rb, line 276
def convert(*filters)
  dup.convert!(*filters)
end
convert!(*filters) click to toggle source
# File lib/bibtex/names.rb, line 280
def convert!(*filters)
  filters.flatten.each do |filter|
    f = Filters.resolve(filter) ||
        raise(ArgumentError, "Failed to load filter #{filter.inspect}")

    each_pair do |k, v|
      self[k] = f.apply(v) unless v.nil?
    end
  end

  self
end
display(options = {})
Alias for: display_order
display_order #→ 'Edgar Allen Poe' click to toggle source
display_order :initials => true #→ 'E.A. Poe'

Returns the name as a string in display order.

# File lib/bibtex/names.rb, line 227
def display_order(options = {})
  [options[:initials] ? initials : first, prefix, last, suffix].compact.join(' ')
end
Also aliased as: display
extend_initials(with_first, for_last) click to toggle source

Sets the name’s first name to the passed-in name if the last name equals for_last and the current first name has the same initials as with_first.

# File lib/bibtex/names.rb, line 185
def extend_initials(with_first, for_last)
  rename_if first: with_first do |name|
    if name.last == for_last
      mine = name.initials.split(/\.[^[:alpha:]]*/)
      other = initials(with_first).split(/\.[^[:alpha:]]*/)

      mine == other || mine.length < other.length && mine == other[0, mine.length]
    end
  end
end
initialize_copy(other) click to toggle source
# File lib/bibtex/names.rb, line 146
def initialize_copy(other)
  other.each_pair do |k, v|
    self[k] = v.dup unless v.nil?
  end
end
initials(token = first) click to toggle source

Returns the first name (or the passed-in string) as initials.

# File lib/bibtex/names.rb, line 166
def initials(token = first)
  token.to_s.gsub(/([[:upper:]])[^[:upper:]\s-]*\s*/, '\1.')
end
initials?() click to toggle source

Returns true if the first name consists solely of initials.

# File lib/bibtex/names.rb, line 179
def initials?
  !(first.nil? || first.empty? || first.to_s =~ /[[:alpha:]]{2,}[^\.]/)
end
normalize_initials(token = first) click to toggle source
# File lib/bibtex/names.rb, line 170
def normalize_initials(token = first)
  token = token.dup.to_s
  token.gsub!(/([[:upper:]])([[:upper:]])/, '\1 \2')
  token.gsub!(/\b([[:upper:]])\b[^[:alpha:]-]*/, '\1.')
  token.gsub!(/\b([[:upper:]]\.)([[:upper:]][[:lower:]]+)/, '\1 \2')
  token
end
rename_if(attributes, conditions = {}) { |self| ... } click to toggle source

Renames the tokens according to the passed-in attributes if all of the conditions match or if the given block returns true.

# File lib/bibtex/names.rb, line 198
def rename_if(attributes, conditions = {})
  if block_given?
    set(attributes) if yield self
  else
    set(attributes) if conditions.all? do |key, value|
      respond_to?(key) && send(key) == value
    end
  end

  self
end
rename_unless(attributes, conditions = {}) { |self| ... } click to toggle source
# File lib/bibtex/names.rb, line 210
def rename_unless(attributes, conditions = {})
  if block_given?
    set(attributes) unless yield self
  else
    set(attributes) unless conditions.all? do |key, value|
      respond_to?(key) && send(key) == value
    end
  end

  self
end
set(attributes = {}) click to toggle source

Set the name tokens to the values defined in the passed-in hash.

# File lib/bibtex/names.rb, line 153
def set(attributes = {})
  attributes.each_pair do |key, value|
    send("#{key}=", value) if respond_to?(key)
  end

  self
end
sort_order #→ 'Poe, Edgar Allen' click to toggle source
sort_order :initials => true #→ 'Poe, E.A.'

Returns the name as a string in sort order.

# File lib/bibtex/names.rb, line 238
def sort_order(options = {})
  [[prefix, last].compact.join(' '), suffix, options[:initials] ? initials : first].compact.join(', ')
end
Also aliased as: to_s
to_citeproc(options = {}) click to toggle source
# File lib/bibtex/names.rb, line 293
def to_citeproc(options = {})
  hash = {}
  hash['family'] = family unless family.nil?
  hash['given'] = given unless given.nil?
  hash['suffix'] = suffix unless suffix.nil?
  hash[options[:particle] || 'non-dropping-particle'] = prefix unless prefix.nil?
  hash
end
to_hash() click to toggle source
# File lib/bibtex/names.rb, line 248
def to_hash
  Hash[each_pair.to_a]
end
to_s(options = {})
Alias for: sort_order
to_xml() click to toggle source
# File lib/bibtex/names.rb, line 252
def to_xml
  require 'rexml/document'
  xml = REXML::Element.new('bibtex:person')

  each_pair do |part, text|
    next if text.nil?

    element = REXML::Element.new("bibtex:#{BibTeXML[part]}")
    element.text = text
    xml.add_element(element)
  end

  xml
end