class BibTeX::String

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 @string and @preamble objects, as well as in field values of regular entries.

Attributes

key[R]

Public Class Methods

new(key = nil, value = nil) { |self| ... } click to toggle source

Creates a new instance.

# 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

Public Instance Methods

[](key) click to toggle source

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

# File lib/bibtex/elements.rb, line 271
def [](key)
  @key == key ? @value : nil
end
added_to_bibliography(bibliography) click to toggle source

Called when the element was added to a bibliography.

Calls superclass method BibTeX::Element#added_to_bibliography
# File lib/bibtex/elements.rb, line 277
def added_to_bibliography(bibliography)
  super
  bibliography.strings[@key] = self
  self
end
content() click to toggle source

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

# File lib/bibtex/elements.rb, line 291
def content
  "#@key = #{@value.to_s(:quotes => '"')}"
end
key=(key) click to toggle source

Sets the string's key (i.e., the symbol identifying the constant).

# File lib/bibtex/elements.rb, line 259
def key=(key)
  raise(ArgumentError, "keys must be convertible to Symbol; was: #{type.class.name}.") unless type.respond_to?(:to_sym)

  unless bibliography.nil?
    bibliography.strings.delete(@key)
    bibliography.strings[key.to_sym] = self
  end

  @key = key.to_sym
end
removed_from_bibliography(bibliography) click to toggle source

Called when the element was removed from a bibliography.

# File lib/bibtex/elements.rb, line 284
def removed_from_bibliography(bibliography)
  super
  bibliography.strings[@key] = nil
  self
end
to_hash(options = {}) click to toggle source
# File lib/bibtex/elements.rb, line 300
def to_hash(options = {})
  { :string => { @key => @value.to_s(:quotes => '"') } }
end
to_s(options = {}) click to toggle source

Returns a string representation of the @string object.

# File lib/bibtex/elements.rb, line 296
def to_s(options = {})
  "@string{ #{content} }\n"
end
to_xml(options = {}) click to toggle source
# 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