A Name comprises individual name parts (first, last, prefix and suffix), but behaves almost like an atomic string value.
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 139 def looks_like?(thing) thing.respond_to?(:to_s) && [Name.new.parse(string)].flatten.compact.empty? end
# File lib/bibtex/names.rb, line 144 def initialize(attributes = {}) set(attributes) end
# File lib/bibtex/names.rb, line 133 def parse(string) [NameParser.new.parse(string)].flatten[0] end
# File lib/bibtex/names.rb, line 246 def <=>(other) other.is_a?(Name) ? sort_order <=> other.sort_order : super end
# File lib/bibtex/names.rb, line 163 def blank? to_a.compact.empty? end
# File lib/bibtex/names.rb, line 278 def convert(*filters) dup.convert!(*filters) end
# File lib/bibtex/names.rb, line 282 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
Returns the name as a string in display order.
# File lib/bibtex/names.rb, line 229 def display_order(options = {}) [options[:initials] ? initials : first, prefix, last, suffix].compact.join(' ') end
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 187 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
# File lib/bibtex/names.rb, line 148 def initialize_copy(other) other.each_pair do |k,v| self[k] = v.dup unless v.nil? end end
Returns the first name (or the passed-in string) as initials.
# File lib/bibtex/names.rb, line 168 def initials(token = first) token.to_s.gsub(/([[:upper:]])[^[:upper:]\s-]*\s*/, '\1.') end
Returns true if the first name consists solely of initials.
# File lib/bibtex/names.rb, line 181 def initials? !(first.nil? || first.empty? || first.to_s =~ /[[:alpha:]]{2,}[^\.]/) end
# File lib/bibtex/names.rb, line 172 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
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 200 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
# File lib/bibtex/names.rb, line 212 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 the name tokens to the values defined in the passed-in hash.
# File lib/bibtex/names.rb, line 155 def set(attributes = {}) attributes.each_pair do |key, value| send("#{key}=", value) if respond_to?(key) end self end
Returns the name as a string in sort order.
# File lib/bibtex/names.rb, line 240 def sort_order(options = {}) [[prefix, last].compact.join(' '), suffix, options[:initials] ? initials : first].compact.join(', ') end
# File lib/bibtex/names.rb, line 296 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
# File lib/bibtex/names.rb, line 250 def to_hash Hash[each_pair.to_a] end
# File lib/bibtex/names.rb, line 254 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