Class: BibTeX::Value

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/bibtex/value.rb

Overview

A BibTeX Value is something very much like a string. In BibTeX files it can appear on the right hand side of @string or @entry field assignments or as @preamble contents. In the example below [VALUE] indicates possible occurences of values in BibTeX:

@preamble{ "foo" [VALUE] }
@string{ foo = "foo" [VALUE] }
@book{id,
  author = {John Doe} [VALUE],
  title = foo # "bar" [VALUE]
}

All Values have in common that they can be simple strings in curly braces or double quotes or complex BibTeX string-concatenations (using the ‘#’ symbol).

Generally, Values try to behave as much as normal Ruby strings as possible; If you do not require any of the advanced BibTeX functionality (string replacement or concatentaion) you can simply convert them to strings using to_s. Note that BibTeX Names are special instances of Values which currently do not support string concatenation or replacement.

Direct Known Subclasses

Names

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Value

Returns a new instance of Value.



69
70
71
72
73
74
# File 'lib/bibtex/value.rb', line 69

def initialize(*arguments)
  @tokens = []
  arguments.flatten.compact.each do |argument|
    add(argument)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/bibtex/value.rb', line 242

def method_missing (name, *args)
  case
  when name.to_s =~ /^(?:convert|from)_([a-z]+)(!)?$/
    $2 ? convert!($1) : convert($1)
  else
    super
  end
end

Instance Attribute Details

#tokensObject (readonly) Also known as: to_a

Returns the value of attribute tokens.



52
53
54
# File 'lib/bibtex/value.rb', line 52

def tokens
  @tokens
end

Class Method Details

.create(*args) ⇒ Object

call-seq:

create(other) => other.dup
create(*args) => Value.new(args)

Duplicates a Value object (or an object of any subclass of Value), or initializes a new one.



65
66
67
# File 'lib/bibtex/value.rb', line 65

def self.create(*args)
  args[0].class < Value && args.size == 1 ? args[0].dup : Value.new(args)
end

Instance Method Details

#<=>(other) ⇒ Object



255
256
257
# File 'lib/bibtex/value.rb', line 255

def <=> (other)
  to_s <=> other.to_s
end

#add(argument) ⇒ Object Also known as: <<, push



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bibtex/value.rb', line 80

def add(argument)
  case argument
  when Value
    @tokens += argument.tokens.dup
  when ::String
    @tokens << argument
  when Symbol
    @tokens << argument
  else
    if argument.respond_to?(:to_s)
      @tokens << argument.to_s
    else
      raise(ArgumentError, "Failed to create Value from argument #{ argument.inspect }; expected String, Symbol or Value instance.")
    end
  end
  self
end

#atomic?Boolean

Returns true if the Value is empty or consists of a single token.

Returns:

  • (Boolean)


177
178
179
# File 'lib/bibtex/value.rb', line 177

def atomic?
  @tokens.length < 2
end

#convert(filter) ⇒ Object

Returns a new Value with all string values converted according to the given filter.



227
228
229
# File 'lib/bibtex/value.rb', line 227

def convert (filter)
  dup.convert!(filter)
end

#convert!(filter) ⇒ Object

Converts all string values according to the given filter.



232
233
234
235
236
237
238
239
240
# File 'lib/bibtex/value.rb', line 232

def convert! (filter)
  if f = Filters.resolve(filter)
    tokens.map! { |t| f.apply(t) }
  else
    raise ArgumentError, "Failed to load filter #{filter.inspect}"
  end
  
  self
end

#date?Boolean

Returns true if the Value’s content is a date.

Returns:

  • (Boolean)


193
194
195
# File 'lib/bibtex/value.rb', line 193

def date?
  !to_date.nil?
end

#initialize_copy(other) ⇒ Object



76
77
78
# File 'lib/bibtex/value.rb', line 76

def initialize_copy(other)
  @tokens = other.tokens.dup
end

#inspectObject



172
173
174
# File 'lib/bibtex/value.rb', line 172

def inspect
  "#<#{self.class} #{tokens.map(&:inspect).join(', ')}>"
end

#joinObject

call-seq:

Value.new('foo', 'bar').join #=> <'foobar'>
Value.new(:foo, 'bar').join  #=> <:foo, 'bar'>

Returns the Value instance with all consecutive String tokens joined.



131
132
133
134
135
136
# File 'lib/bibtex/value.rb', line 131

def join
  @tokens = @tokens.inject([]) do |a,b|
    a[-1].is_a?(::String) && b.is_a?(::String) ? a[-1] += b : a << b; a
  end
  self
end

#name?Boolean Also known as: names?

Returns true if the value is a BibTeX name value.

Returns:

  • (Boolean)


182
# File 'lib/bibtex/value.rb', line 182

def name?; false; end

#numeric?Boolean

Returns true if the Value’s content is numeric.

Returns:

  • (Boolean)


206
207
208
# File 'lib/bibtex/value.rb', line 206

def numeric?
  to_s =~ /^\s*[+-]?\d+[\/\.]?\d*\s*$/
end

#replace(*arguments) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bibtex/value.rb', line 110

def replace(*arguments)
  return self unless has_symbol?
  arguments.flatten.each do |argument|
    case argument
    when ::String # simulates Ruby's String#replace
      @tokens = [argument]
    when String
      @tokens = @tokens.map { |v| argument.key == v ? argument.value.tokens : v }.flatten
    when Hash
      @tokens = @tokens.map { |v| argument[v] || v }
    end
  end
  self
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/bibtex/value.rb', line 251

def respond_to? (method)
  method =~ /^(?:convert|from)_([a-z]+)(!)?$/ || super
end

#symbol?Boolean Also known as: has_symbol?

Returns true if the Value contains at least one symbol.

Returns:

  • (Boolean)


215
216
217
# File 'lib/bibtex/value.rb', line 215

def symbol?
  tokens.detect { |v| v.is_a?(Symbol) }
end

#symbolsObject

Returns all symbols contained in the Value.



222
223
224
# File 'lib/bibtex/value.rb', line 222

def symbols
  tokens.select { |v| v.is_a?(Symbol) }
end

#to_citeproc(options = {}) ⇒ Object



210
211
212
# File 'lib/bibtex/value.rb', line 210

def to_citeproc (options = {})
  to_s(options)
end

#to_dateObject

Returns the string as a date.



198
199
200
201
202
203
# File 'lib/bibtex/value.rb', line 198

def to_date
  require 'date'
  Date.parse(to_s)
rescue
  nil
end

#to_nameObject Also known as: to_names



186
187
188
# File 'lib/bibtex/value.rb', line 186

def to_name
  Names.parse(to_s)
end

#to_s(options = {}) ⇒ Object

call-seq:

Value.new('foo').to_s                       #=> "foo"
Value.new(:foo).to_s                        #=> "foo"
Value.new('foo').to_s(:quotes => '"')       #=> "\"foo\""
Value.new('foo').to_s(:quotes => ['"','"']) #=> "\"foo\""
Value.new('foo').to_s(:quotes => ['{','}']) #=> "{foo}"
Value.new(:foo, 'bar').to_s                 #=> "foo # \"bar\""
Value.new('foo', 'bar').to_s                #=> "\"foo\" # \"bar\""
Value.new('\"u').to_s(:filter => :latex)    #=> "ü"

Returns a the Value as a string. @see #value; the only difference is that single symbols are returned as String, too. If the Value is atomic and the option :quotes is given, the string will be quoted using the quote symbols specified.

If the option :filter is given, the Value will be converted using the filter(s) specified.



155
156
157
158
159
160
# File 'lib/bibtex/value.rb', line 155

def to_s(options = {})
  return convert(options.delete(:filter)).to_s(options) if options.has_key?(:filter)
  return value.to_s unless options.has_key?(:quotes) && atomic?
  q = [options[:quotes]].flatten
  [q[0], value, q[-1]].compact.join
end

#valueObject Also known as: v

Returns the Value as a string or, if it consists of a single symbol, as a Symbol instance. If the Value contains multiple tokens, they will be joined by a ‘#’, additionally, all string tokens will be turned into string literals (i.e., delimitted by quotes).



166
167
168
# File 'lib/bibtex/value.rb', line 166

def value
  atomic? ? @tokens[0] : @tokens.map { |v|  v.is_a?(::String) ? v.inspect : v }.join(' # ')
end