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.



144
145
146
# File 'lib/bibtex/names.rb', line 144

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



118
119
120
# File 'lib/bibtex/names.rb', line 118

def first
  @first
end

#lastObject Also known as: family

Returns the value of attribute last

Returns:

  • (Object)

    the current value of last



118
119
120
# File 'lib/bibtex/names.rb', line 118

def last
  @last
end

#prefixObject Also known as: von

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



118
119
120
# File 'lib/bibtex/names.rb', line 118

def prefix
  @prefix
end

#suffixObject Also known as: jr

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



118
119
120
# File 'lib/bibtex/names.rb', line 118

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)


139
140
141
# File 'lib/bibtex/names.rb', line 139

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

.parse(string) ⇒ Object



133
134
135
# File 'lib/bibtex/names.rb', line 133

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

Instance Method Details

#<=>(other) ⇒ Object



231
232
233
# File 'lib/bibtex/names.rb', line 231

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



263
264
265
# File 'lib/bibtex/names.rb', line 263

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

#convert!(*filters) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/bibtex/names.rb', line 267

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.



214
215
216
# File 'lib/bibtex/names.rb', line 214

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.



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

def extend_initials(with_first, for_last)
  rename_if :first => with_first do |name|
    name.last == for_last && name.initials.gsub(/\s+/, '') == initials(with_first).gsub(/\s+/, '')
  end
end

#initalize_copy(other) ⇒ Object



148
149
150
# File 'lib/bibtex/names.rb', line 148

def initalize_copy(other)
  each_pair { |k,v| self[k] = v.dup }
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:]])[[:lower:]]+\s*/, '\1.')
end

#initials?Boolean

Returns true if the first name consists solely of initials.

Returns:

  • (Boolean)


171
172
173
# File 'lib/bibtex/names.rb', line 171

def initials?
  !(first.nil? || first.empty? || first.to_s =~ /[[:alpha:]]{2,}[^\.]/)
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.



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bibtex/names.rb', line 185

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



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/bibtex/names.rb', line 197

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.



225
226
227
# File 'lib/bibtex/names.rb', line 225

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

#to_citeproc(options = {}) ⇒ Object



281
282
283
284
285
286
287
288
# File 'lib/bibtex/names.rb', line 281

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



235
236
237
# File 'lib/bibtex/names.rb', line 235

def to_hash
  Hash[each_pair.to_a]
end

#to_xmlObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/bibtex/names.rb', line 239

def to_xml
  require 'rexml/document'      
  xml = REXML::Element.new('bibtex:person')

  each_pair do |part, text|
    unless text.nil?
      element = REXML::Element.new("bibtex:#{BibTeXML[part]}")
      element.text = text
      xml.add_element(element)
    end
  end
  
  xml
end