Class: BibTeX::Name
- Inherits:
-
Struct
- Object
- Struct
- BibTeX::Name
- 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
-
#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(filter) ⇒ Object
- #convert!(filter) ⇒ Object
- #display_order ⇒ Object (also: #display)
- #initalize_copy(other) ⇒ Object
-
#initialize(attributes = {}) ⇒ Name
constructor
A new instance of Name.
- #sort_order ⇒ Object (also: #to_s)
- #to_citeproc(options = {}) ⇒ Object
- #to_hash ⇒ Object
- #to_xml ⇒ Object
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
#first ⇒ Object Also known as: given
Returns the value of attribute first
100 101 102 |
# File 'lib/bibtex/names.rb', line 100 def first @first end |
#last ⇒ Object Also known as: family
Returns the value of attribute last
100 101 102 |
# File 'lib/bibtex/names.rb', line 100 def last @last end |
#prefix ⇒ Object Also known as: von
Returns the value of attribute prefix
100 101 102 |
# File 'lib/bibtex/names.rb', line 100 def prefix @prefix end |
#suffix ⇒ Object Also known as: jr
Returns the value of attribute 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.
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
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_order ⇒ Object 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_order ⇒ Object 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( = {}) hash = {} hash['family'] = family unless family.nil? hash['given'] = given unless given.nil? hash['suffix'] = suffix unless suffix.nil? hash[[:particle] || 'dropping-particle'] = prefix unless prefix.nil? hash end |
#to_hash ⇒ Object
157 158 159 |
# File 'lib/bibtex/names.rb', line 157 def to_hash Hash[each_pair.to_a] end |
#to_xml ⇒ Object
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 |