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

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

Constructor Details

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

Creates a new instance.

Yields:

  • (_self)

Yield Parameters:



253
254
255
256
# File 'lib/bibtex/elements.rb', line 253

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.



250
251
252
# File 'lib/bibtex/elements.rb', line 250

def key
  @key
end

Instance Method Details

#[](key) ⇒ Object

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



271
272
273
# File 'lib/bibtex/elements.rb', line 271

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

#added_to_bibliography(bibliography) ⇒ Object

Called when the element was added to a bibliography.



277
278
279
280
281
# File 'lib/bibtex/elements.rb', line 277

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

#contentObject

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



291
292
293
# File 'lib/bibtex/elements.rb', line 291

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

#removed_from_bibliography(bibliography) ⇒ Object

Called when the element was removed from a bibliography.



284
285
286
287
288
# File 'lib/bibtex/elements.rb', line 284

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

#to_hash(options = {}) ⇒ Object



300
301
302
# File 'lib/bibtex/elements.rb', line 300

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

#to_s(options = {}) ⇒ Object

Returns a string representation of the @string object.



296
297
298
# File 'lib/bibtex/elements.rb', line 296

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

#to_xml(options = {}) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/bibtex/elements.rb', line 304

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