Class: BibTeX::String

Inherits:
Element show all
Includes:
Replaceable
Defined in:
lib/bibtex/elements.rb

Overview

Represents a @string object.

In BibTeX @string objects contain a single string constant assignment. For example, @string{ foo = “bar” } defines the constant ‘foo’; this constant can be used (using BibTeX’s string concatenation syntax) in susbsequent of regular entries.

Instance Attribute Summary collapse

Attributes included from Replaceable

#value

Attributes inherited from Element

#bibliography, #id

Instance Method Summary collapse

Methods included from Replaceable

#<<, #join, #replace

Methods inherited from Element

#<=>, #has_type?, #inspect, #join, #matches?, #meets?, #names, parse, #replace, #to_json, #to_yaml, #type

Constructor Details

#initialize(key = nil, value = nil) {|_self| ... } ⇒ String

Creates a new instance.

Yields:

  • (_self)

Yield Parameters:



178
179
180
181
# File 'lib/bibtex/elements.rb', line 178

def initialize(key = nil, value = nil)
  @key, @value = key.to_sym, Value.new(value)
  yield self if block_given?
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



175
176
177
# File 'lib/bibtex/elements.rb', line 175

def key
  @key
end

Instance Method Details

#[](key) ⇒ Object

Retuns the string’s value if parameter matches the key; nil otherwise.



196
197
198
# File 'lib/bibtex/elements.rb', line 196

def [](key)
  @key == key ? @value : nil
end

#added_to_bibliography(bibliography) ⇒ Object

Called when the element was added to a bibliography.



202
203
204
205
206
# File 'lib/bibtex/elements.rb', line 202

def added_to_bibliography(bibliography)
  super
  bibliography.strings[@key] = self
  self
end

#contentObject

Returns a string representation of the @string’s content.



216
217
218
# File 'lib/bibtex/elements.rb', line 216

def content
  "#@key = #{@value.to_s(:quotes => '"')}"
end

#removed_from_bibliography(bibliography) ⇒ Object

Called when the element was removed from a bibliography.



209
210
211
212
213
# File 'lib/bibtex/elements.rb', line 209

def removed_from_bibliography(bibliography)
  super
  bibliography.strings[@key] = nil
  self
end

#to_hash(options = {}) ⇒ Object



225
226
227
# File 'lib/bibtex/elements.rb', line 225

def to_hash(options = {})
  { :string => { @key => @value.to_s(:quotes => '"') } }
end

#to_s(options = {}) ⇒ Object

Returns a string representation of the @string object.



221
222
223
# File 'lib/bibtex/elements.rb', line 221

def to_s(options = {})
  "@string{ #{content} }"
end

#to_xml(options = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/bibtex/elements.rb', line 229

def to_xml(options = {})
  require 'rexml/document'
  
  xml = REXML::Element.new(:string)
  
  k, v = REXML::Element.new(:key), REXML::Element.new(:value)
  k.text = key.to_s
  v.text = value.to_s(:quotes => '"')

  xml.add_elements(k)
  xml.add_elements(v)
  
  xml
end