Class: BibTeX::Name
- Inherits:
-
Struct
- Object
- Struct
- BibTeX::Name
- 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
-
#first ⇒ Object
(also: #given)
Returns the value of attribute first.
-
#last ⇒ Object
(also: #family)
Returns the value of attribute last.
-
#prefix ⇒ Object
(also: #von)
Returns the value of attribute prefix.
-
#suffix ⇒ Object
(also: #jr)
Returns the value of attribute suffix.
Class Method Summary collapse
-
.looks_like?(thing) ⇒ Boolean
Returns true if thing looks like a name.
- .parse(string) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #blank? ⇒ Boolean
- #convert(*filters) ⇒ Object
- #convert!(*filters) ⇒ Object
-
#display_order(options = {}) ⇒ Object
(also: #display)
call-seq: name.display_order #=> ‘Edgar Allen Poe’ name.display_order :initials => true #=> ‘E.A.
-
#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.
-
#initialize(attributes = {}) ⇒ Name
constructor
A new instance of Name.
- #initialize_copy(other) ⇒ Object
-
#initials(token = first) ⇒ Object
Returns the first name (or the passed-in string) as initials.
-
#initials? ⇒ Boolean
Returns true if the first name consists solely of initials.
- #normalize_initials(token = first) ⇒ Object
-
#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.
- #rename_unless(attributes, conditions = {}) ⇒ Object
-
#set(attributes = {}) ⇒ Object
Set the name tokens to the values defined in the passed-in hash.
-
#sort_order(options = {}) ⇒ Object
(also: #to_s)
call-seq: name.sort_order #=> ‘Poe, Edgar Allen’ name.sort_order :initials => true #=> ‘Poe, E.A.’.
- #to_citeproc(options = {}) ⇒ Object
- #to_hash ⇒ Object
- #to_xml ⇒ Object
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
#first ⇒ Object Also known as: given
Returns the value of attribute first
118 119 120 |
# File 'lib/bibtex/names.rb', line 118 def first @first end |
#last ⇒ Object Also known as: family
Returns the value of attribute last
118 119 120 |
# File 'lib/bibtex/names.rb', line 118 def last @last end |
#prefix ⇒ Object Also known as: von
Returns the value of attribute prefix
118 119 120 |
# File 'lib/bibtex/names.rb', line 118 def prefix @prefix end |
#suffix ⇒ Object Also known as: jr
Returns the value of attribute 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.
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
246 247 248 |
# File 'lib/bibtex/names.rb', line 246 def <=>(other) other.is_a?(Name) ? sort_order <=> other.sort_order : super end |
#blank? ⇒ Boolean
163 164 165 |
# File 'lib/bibtex/names.rb', line 163 def blank? to_a.compact.empty? end |
#convert(*filters) ⇒ Object
278 279 280 |
# File 'lib/bibtex/names.rb', line 278 def convert(*filters) dup.convert!(*filters) end |
#convert!(*filters) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# 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 |
#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.
229 230 231 |
# File 'lib/bibtex/names.rb', line 229 def display_order( = {}) [[: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.
187 188 189 190 191 192 193 194 195 196 |
# 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 |
#initialize_copy(other) ⇒ Object
148 149 150 151 152 |
# 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 |
#initials(token = first) ⇒ Object
Returns the first name (or the passed-in string) as initials.
168 169 170 |
# File 'lib/bibtex/names.rb', line 168 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.
181 182 183 |
# File 'lib/bibtex/names.rb', line 181 def initials? !(first.nil? || first.empty? || first.to_s =~ /[[:alpha:]]{2,}[^\.]/) end |
#normalize_initials(token = first) ⇒ Object
172 173 174 175 176 177 178 |
# 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 |
#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.
200 201 202 203 204 205 206 207 208 209 210 |
# 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 |
#rename_unless(attributes, conditions = {}) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# 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(attributes = {}) ⇒ Object
Set the name tokens to the values defined in the passed-in hash.
155 156 157 158 159 160 161 |
# 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 |
#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.
240 241 242 |
# File 'lib/bibtex/names.rb', line 240 def sort_order( = {}) [[prefix, last].compact.join(' '), suffix, [:initials] ? initials : first].compact.join(', ') end |
#to_citeproc(options = {}) ⇒ Object
296 297 298 299 300 301 302 303 |
# File 'lib/bibtex/names.rb', line 296 def to_citeproc( = {}) hash = {} hash['family'] = family unless family.nil? hash['given'] = given unless given.nil? hash['suffix'] = suffix unless suffix.nil? hash[[:particle] || 'non-dropping-particle'] = prefix unless prefix.nil? hash end |
#to_hash ⇒ Object
250 251 252 |
# File 'lib/bibtex/names.rb', line 250 def to_hash Hash[each_pair.to_a] end |
#to_xml ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# 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 |