Class: BibTeX::Name

Inherits:
Struct
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/bibtex/names.rb

Overview

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

Constant Summary collapse

BibTeXML =
{
  first: :first,
  last: :last,
  prefix: :prelast,
  suffix: :lineage
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Name

Returns a new instance of Name.



142
143
144
# File 'lib/bibtex/names.rb', line 142

def initialize(attributes = {})
  set(attributes)
end

Instance Attribute Details

#firstObject Also known as: given

Returns the value of attribute first

Returns:

  • (Object)

    the current value of first



116
117
118
# File 'lib/bibtex/names.rb', line 116

def first
  @first
end

#lastObject Also known as: family

Returns the value of attribute last

Returns:

  • (Object)

    the current value of last



116
117
118
# File 'lib/bibtex/names.rb', line 116

def last
  @last
end

#prefixObject Also known as: von

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



116
117
118
# File 'lib/bibtex/names.rb', line 116

def prefix
  @prefix
end

#suffixObject Also known as: jr

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



116
117
118
# File 'lib/bibtex/names.rb', line 116

def suffix
  @suffix
end

Class Method Details

.looks_like?(thing) ⇒ Boolean

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

Returns:

  • (Boolean)


137
138
139
# File 'lib/bibtex/names.rb', line 137

def looks_like?(thing)
  thing.respond_to?(:to_s) && [Name.new.parse(string)].flatten.compact.empty?
end

.parse(string) ⇒ Object



131
132
133
# File 'lib/bibtex/names.rb', line 131

def parse(string)
  [NameParser.new.parse(string)].flatten[0]
end

Instance Method Details

#<=>(other) ⇒ Object



244
245
246
# File 'lib/bibtex/names.rb', line 244

def <=>(other)
  other.is_a?(Name) ? sort_order <=> other.sort_order : super
end

#blank?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/bibtex/names.rb', line 161

def blank?
  to_a.compact.empty?
end

#convert(*filters) ⇒ Object



276
277
278
# File 'lib/bibtex/names.rb', line 276

def convert(*filters)
  dup.convert!(*filters)
end

#convert!(*filters) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# 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_order(options = {}) ⇒ Object Also known as: display

call-seq:

name.display_order #=> 'Edgar Allen Poe'
name.display_order :initials => true #=> 'E.A. Poe'

Returns the name as a string in display order.



227
228
229
# File 'lib/bibtex/names.rb', line 227

def display_order(options = {})
  [options[:initials] ? initials : first, prefix, last, suffix].compact.join(' ')
end

#extend_initials(with_first, for_last) ⇒ Object

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.



185
186
187
188
189
190
191
192
193
194
# 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) ⇒ Object



146
147
148
149
150
# 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) ⇒ Object

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



166
167
168
# File 'lib/bibtex/names.rb', line 166

def initials(token = first)
  token.to_s.gsub(/([[:upper:]])[^[:upper:]\s-]*\s*/, '\1.')
end

#initials?Boolean

Returns true if the first name consists solely of initials.

Returns:

  • (Boolean)


179
180
181
# File 'lib/bibtex/names.rb', line 179

def initials?
  !(first.nil? || first.empty? || first.to_s =~ /[[:alpha:]]{2,}[^\.]/)
end

#normalize_initials(token = first) ⇒ Object



170
171
172
173
174
175
176
# 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 = {}) ⇒ Object

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



198
199
200
201
202
203
204
205
206
207
208
# 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 = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
# 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 = {}) ⇒ Object

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



153
154
155
156
157
158
159
# 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(options = {}) ⇒ Object Also known as: to_s

call-seq:

name.sort_order #=> 'Poe, Edgar Allen'
name.sort_order :initials => true #=> 'Poe, E.A.'

Returns the name as a string in sort order.



238
239
240
# File 'lib/bibtex/names.rb', line 238

def sort_order(options = {})
  [[prefix, last].compact.join(' '), suffix, options[:initials] ? initials : first].compact.join(', ')
end

#to_citeproc(options = {}) ⇒ Object



293
294
295
296
297
298
299
300
# 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_hashObject



248
249
250
# File 'lib/bibtex/names.rb', line 248

def to_hash
  Hash[each_pair.to_a]
end

#to_xmlObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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