Class: BibTeX::Name

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

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.



127
128
129
130
131
# File 'lib/bibtex/names.rb', line 127

def initialize(attributes = {})
  attributes.each do |key,value|
    send("#{key}=", value) if respond_to?(key)
  end
end

Instance Attribute Details

#firstObject Also known as: given

Returns the value of attribute first

Returns:

  • (Object)

    the current value of first



100
101
102
# File 'lib/bibtex/names.rb', line 100

def first
  @first
end

#lastObject Also known as: family

Returns the value of attribute last

Returns:

  • (Object)

    the current value of last



100
101
102
# File 'lib/bibtex/names.rb', line 100

def last
  @last
end

#prefixObject Also known as: von

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



100
101
102
# File 'lib/bibtex/names.rb', line 100

def prefix
  @prefix
end

#suffixObject Also known as: jr

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



100
101
102
# File 'lib/bibtex/names.rb', line 100

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)


122
123
124
# File 'lib/bibtex/names.rb', line 122

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

.parse(string) ⇒ Object



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

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

Instance Method Details

#<=>(other) ⇒ Object



153
154
155
# File 'lib/bibtex/names.rb', line 153

def <=>(other)
  other.is_a?(Name) ? [last, prefix, first, suffix].compact.join(' ') <=> [other.last, other.prefix, other.first, other.suffix].compact.join(' ') : super
end

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  to_a.compact.empty?
end

#convert(filter) ⇒ Object



185
186
187
# File 'lib/bibtex/names.rb', line 185

def convert(filter)
	dup.convert!(filter)
end

#convert!(filter) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/bibtex/names.rb', line 189

def convert!(filter)
	if f = Filters.resolve(filter)
		each_pair { |k,v| self[k] = f.apply(v) unless v.nil? }
	else
		raise ArgumentError, "Failed to load filter #{filter.inspect}"
	end

	self
end

#display_orderObject Also known as: display



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

def display_order
  [prefix, last, first, suffix].compact.join(' ')
end

#initalize_copy(other) ⇒ Object



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

def initalize_copy(other)
  each_pair { |k,v| self[k] = v }
end

#sort_orderObject Also known as: to_s



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

def sort_order
  [[prefix,last].compact.join(' '), suffix, first].compact.join(', ')
end

#to_citeproc(options = {}) ⇒ Object



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

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] || 'dropping-particle'] = prefix unless prefix.nil?
  hash
end

#to_hashObject



157
158
159
# File 'lib/bibtex/names.rb', line 157

def to_hash
  Hash[each_pair.to_a]
end

#to_xmlObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/bibtex/names.rb', line 161

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