Class: BibTeX::String

Inherits:
Element show all
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 inherited from Element

#bibliography

Instance Method Summary collapse

Methods inherited from Element

#to_json, #to_yaml

Constructor Details

#initialize(key = nil, value = nil) ⇒ String

Creates a new instance.



92
93
94
95
# File 'lib/bibtex/elements.rb', line 92

def initialize(key=nil,value=nil)
	self.key = key.to_sym unless key.nil?
	self.value = value unless value.nil?
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



89
90
91
# File 'lib/bibtex/elements.rb', line 89

def key
  @key
end

#valueObject

Returns the value of attribute value.



89
90
91
# File 'lib/bibtex/elements.rb', line 89

def value
  @value
end

Instance Method Details

#<<(value) ⇒ Object

Adds either a string constant or literal to the current value. The values will be concatenated using the ‘#’ symbol.

Raises:

  • (ArgumentError)


140
141
142
143
# File 'lib/bibtex/elements.rb', line 140

def <<(value)
	raise(ArgumentError, "BibTeX::String value can contain only instances of Symbol or String; was: #{value.class.name}.") unless [::String,Symbol].map { |k| value.kind_of?(k) }.inject { |sum,n| sum || n }
	@value << value
end

#added_to_bibliography(bibliography) ⇒ Object

Called when the element was added to a bibliography.



146
147
148
149
150
# File 'lib/bibtex/elements.rb', line 146

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

#contentObject

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



160
161
162
# File 'lib/bibtex/elements.rb', line 160

def content
	[@key.to_s,' = ',StringReplacement.to_s(@value)].join
end

#removed_from_bibliography(bibliography) ⇒ Object

Called when the element was removed from a bibliography.



153
154
155
156
157
# File 'lib/bibtex/elements.rb', line 153

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

#replace(hsh) ⇒ Object

Replaces all constants in this string’s value which are defined in hsh. Returns the new value (the @string object itself remains unchanged).

call-seq: s.to_s

> “@string{ foobar = foo # ”bar“}”

s.replace(=> ‘foo’)

> [“foo”,“bar”]

s.to_s

> “@string{ foobar = foo # ”bar“}”



119
120
121
# File 'lib/bibtex/elements.rb', line 119

def replace(hsh)
	StringReplacement.replace(@value,hsh)
end

#replace!(hsh) ⇒ Object

Replaces all constants in this string’s value which are defined in hsh. Returns the new value (the @string object itself is changed as well).

call-seq: s.to_s

> “@string{ foobar = foo # ”bar“}”

s.replace(=> ‘foo’)

> [“foo”,“bar”]

s.to_s

> “@string{ foobar = ”foo“ # ”bar“}”



133
134
135
136
# File 'lib/bibtex/elements.rb', line 133

def replace!(hsh)
	@value = replace(hsh)
	@bibliography.strings[@key] = value unless @bibliography.nil?
end

#to_hashObject



169
170
171
# File 'lib/bibtex/elements.rb', line 169

def to_hash
  { 'string' => { @key.to_s => StringReplacement.to_s(@value) } }
end

#to_sObject

Returns a string representation of the @string object.



165
166
167
# File 'lib/bibtex/elements.rb', line 165

def to_s
	['@string{ ',content,'}'].join
end

#to_xmlObject



173
174
175
176
177
178
179
180
# File 'lib/bibtex/elements.rb', line 173

def to_xml
  xml = REXML::Element.new('string')
  key = REXML::Element.new('key')
  val = REXML::Element.new('value')
  key.text = @key.to_s
  val.text = @value.to_s
  xml
end