Class: BibTeX::String
- 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
-
#key ⇒ Object
Returns the value of attribute key.
Attributes included from Replaceable
Attributes inherited from Element
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retuns the string’s value if parameter matches the key; nil otherwise.
-
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
-
#content ⇒ Object
Returns a string representation of the @string’s content.
-
#initialize(key = nil, value = nil) {|_self| ... } ⇒ String
constructor
Creates a new instance.
-
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
- #to_hash(_options = {}) ⇒ Object
-
#to_s(_options = {}) ⇒ Object
Returns a string representation of the @string object.
- #to_xml(_options = {}) ⇒ Object
Methods included from Replaceable
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
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
259 260 261 |
# File 'lib/bibtex/elements.rb', line 259 def key @key end |
Instance Method Details
#[](key) ⇒ Object
Retuns the string’s value if parameter matches the key; nil otherwise.
281 282 283 |
# File 'lib/bibtex/elements.rb', line 281 def [](key) @key == key ? @value : nil end |
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
286 287 288 289 290 |
# File 'lib/bibtex/elements.rb', line 286 def added_to_bibliography(bibliography) super bibliography.strings[@key] = self self end |
#content ⇒ Object
Returns a string representation of the @string’s content.
300 301 302 |
# File 'lib/bibtex/elements.rb', line 300 def content "#{@key} = #{@value.to_s(quotes: '"')}" end |
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
293 294 295 296 297 |
# File 'lib/bibtex/elements.rb', line 293 def removed_from_bibliography(bibliography) super bibliography.strings[@key] = nil self end |
#to_hash(_options = {}) ⇒ Object
309 310 311 |
# File 'lib/bibtex/elements.rb', line 309 def to_hash( = {}) { string: { @key => @value.to_s(quotes: '"') } } end |
#to_s(_options = {}) ⇒ Object
Returns a string representation of the @string object.
305 306 307 |
# File 'lib/bibtex/elements.rb', line 305 def to_s( = {}) "@string{ #{content} }\n" end |
#to_xml(_options = {}) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/bibtex/elements.rb', line 313 def to_xml( = {}) require 'rexml/document' xml = REXML::Element.new(:string) k = REXML::Element.new(:key) v = 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 |