Class: BibTeX::Entry
Overview
Represents a regular BibTeX entry.
Constant Summary collapse
- @@RequiredFields =
Hash containing the required fields of the standard entry types
Hash.new([])
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#key ⇒ Object
Returns the value of attribute key.
-
#type ⇒ Object
Returns the value of attribute type.
Attributes inherited from Element
Instance Method Summary collapse
-
#<<(fields) ⇒ Object
Adds all the fields contained in a given hash to the entry.
-
#[](name) ⇒ Object
Returns the value of the field with the given name.
-
#[]=(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry.
-
#add(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry.
-
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
-
#content ⇒ Object
Returns a string of all the entry’s fields.
-
#delete(name) ⇒ Object
Removes the field with a given name from the entry.
-
#empty? ⇒ Boolean
Returns true if the entry currently contains no field.
-
#initialize(type = nil, key = nil) ⇒ Entry
constructor
Creates a new instance of a given
type
(e.g., :article, :book, etc.) identified by akey
. -
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
-
#replace!(hsh) ⇒ Object
Replaces all constants in this entry’s field values which are defined in
hsh
. - #to_hash ⇒ Object
-
#to_s ⇒ Object
Returns a string representation of the entry.
- #to_xml ⇒ Object
-
#valid? ⇒ Boolean
Returns false if the entry is one of the standard entry types and does not have definitions of all the required fields for that type.
Methods inherited from Element
Constructor Details
#initialize(type = nil, key = nil) ⇒ Entry
Creates a new instance of a given type
(e.g., :article, :book, etc.) identified by a key
.
47 48 49 50 51 |
# File 'lib/bibtex/entry.rb', line 47 def initialize(type=nil, key=nil) self.key = key.to_s unless key.nil? self.type = type.to_sym unless type.nil? @fields = {} end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
24 25 26 |
# File 'lib/bibtex/entry.rb', line 24 def fields @fields end |
#key ⇒ Object
Returns the value of attribute key.
24 25 26 |
# File 'lib/bibtex/entry.rb', line 24 def key @key end |
#type ⇒ Object
Returns the value of attribute type.
24 25 26 |
# File 'lib/bibtex/entry.rb', line 24 def type @type end |
Instance Method Details
#<<(fields) ⇒ Object
Adds all the fields contained in a given hash to the entry.
91 92 93 94 95 |
# File 'lib/bibtex/entry.rb', line 91 def <<(fields) raise(ArgumentError, "BibTeX::Entry fields must be of type Hash; was: #{fields.class.name}.") unless fields.kind_of?(Hash) fields.each { |n,v| add(n,v) } self end |
#[](name) ⇒ Object
Returns the value of the field with the given name.
66 67 68 |
# File 'lib/bibtex/entry.rb', line 66 def [](name) @fields[name.to_sym] end |
#[]=(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry. Returns the new value.
72 73 74 |
# File 'lib/bibtex/entry.rb', line 72 def []=(name,value) add(name,value) end |
#add(name, value) ⇒ Object
Adds a new field (name-value pair) to the entry. Returns the new value.
78 79 80 81 82 |
# File 'lib/bibtex/entry.rb', line 78 def add(name,value) raise(ArgumentError, "BibTeX::Entry field name must be of type Symbol; was: #{name.class.name}.") unless name.kind_of?(Symbol) raise(ArgumentError, "BibTeX::Entry field value must be of type Array, Symbol, or String; was: #{value.class.name}.") unless [Array,::String,Symbol].map { |k| value.kind_of?(k) }.inject { |sum,n| sum || n } @fields[name] = value.kind_of?(Array) ? value : [value] end |
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
111 112 113 114 115 |
# File 'lib/bibtex/entry.rb', line 111 def added_to_bibliography(bibliography) super(bibliography) bibliography.entries[@key] = self self end |
#content ⇒ Object
Returns a string of all the entry’s fields.
130 131 132 |
# File 'lib/bibtex/entry.rb', line 130 def content @fields.keys.map { |k| "#{k} = #{StringReplacement.to_s(@fields[k], :delimiter => ['{','}'])}" }.join(",\n") end |
#delete(name) ⇒ Object
Removes the field with a given name from the entry. Returns the value of the deleted field; nil if the field was not set.
86 87 88 |
# File 'lib/bibtex/entry.rb', line 86 def delete(name) @fields.delete(name.to_sym) end |
#empty? ⇒ Boolean
Returns true if the entry currently contains no field.
98 99 100 |
# File 'lib/bibtex/entry.rb', line 98 def empty? @fields.empty? end |
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
118 119 120 121 122 |
# File 'lib/bibtex/entry.rb', line 118 def removed_from_bibliography(bibliography) super(bibliography) bibliography.entries[@key] = nil self end |
#replace!(hsh) ⇒ Object
Replaces all constants in this entry’s field values which are defined in hsh
.
125 126 127 |
# File 'lib/bibtex/entry.rb', line 125 def replace!(hsh) @fields.keys.each { |k| @fields[k] = StringReplacement.replace(@fields[k],hsh) } end |
#to_hash ⇒ Object
139 140 141 |
# File 'lib/bibtex/entry.rb', line 139 def to_hash { @type.to_s => @fields.keys.map { |k| { k.to_s => StringReplacement.to_s(@fields[k], :delimiter => ['{','}']) } }.inject({ 'key' => @key }) { |sum,n| sum.merge(n) } }.to_yaml end |
#to_s ⇒ Object
Returns a string representation of the entry.
135 136 137 |
# File 'lib/bibtex/entry.rb', line 135 def to_s ["@#{type}{#{key},",content.gsub(/^/,' '),"}\n"].join("\n") end |
#to_xml ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/bibtex/entry.rb', line 143 def to_xml xml = REXML::Element.new(@type.to_s) xml.attributes['key'] = @key @fields.each do |k,v| e = REXML::Element.new(k.to_s) e.text = StringReplacement.to_s(v, :delimiter => ['','']) xml.add_element(e) end xml end |
#valid? ⇒ Boolean
Returns false if the entry is one of the standard entry types and does not have definitions of all the required fields for that type.
104 105 106 107 108 |
# File 'lib/bibtex/entry.rb', line 104 def valid? !@@RequiredFields[@type].map { |f| f.kind_of?(Array) ? !(f & @fields.keys).empty? : !@fields[f].nil? }.include?(false) end |