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

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Value

Returns a new instance of Value.



56
57
58
59
60
61
# File 'lib/bibtex/value.rb', line 56

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

Instance Attribute Details

#tokensObject (readonly) Also known as: to_a

Returns the value of attribute tokens.



50
51
52
# File 'lib/bibtex/value.rb', line 50

def tokens
  @tokens
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bibtex/value.rb', line 67

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)


161
162
163
# File 'lib/bibtex/value.rb', line 161

def atomic?
  @tokens.length < 2
end

#date?Boolean Also known as: is_date?

Returns true if the Value’s content looks like a date.

Returns:

  • (Boolean)


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

def date?
end

#initialize_copy(other) ⇒ Object



63
64
65
# File 'lib/bibtex/value.rb', line 63

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

#inspectObject



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

def inspect
  '<' + @tokens.map(&:inspect).join(', ') + '>'
end

#joinObject

Returns the Value instance with all consecutive String tokens joined.

call-seq: Value.new(‘foo’, ‘bar’).join #=> <‘foobar’> Value.new(:foo, ‘bar’).join #=> <:foo, ‘bar’>



119
120
121
122
123
124
# File 'lib/bibtex/value.rb', line 119

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?, is_name?

Returns true if the value is a BibTeX name value.

Returns:

  • (Boolean)


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

def name?; false; end

#numeric?Boolean Also known as: is_numeric?

Returns true if the Value’s content is numeric.

Returns:

  • (Boolean)


189
190
191
# File 'lib/bibtex/value.rb', line 189

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

#replace(*arguments) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bibtex/value.rb', line 97

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

#symbol?Boolean Also known as: has_symbol?

Returns true if the Value contains at least one symbol.

Returns:

  • (Boolean)


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

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

#symbolsObject

Returns all symbols contained in the Value.



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

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

#to_citeproc(options = {}) ⇒ Object



195
196
197
# File 'lib/bibtex/value.rb', line 195

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

#to_dateObject

Returns the string as a citeproc date. TODO use edtf format instead.



184
185
186
# File 'lib/bibtex/value.rb', line 184

def to_date
  numeric? ? { 'date-parts' => [[to_i]] } : { 'literal' => to_s(:quotes => [])}
end

#to_nameObject Also known as: to_names



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

def to_name
  Names.parse(to_s)
end

#to_s(options = {}) ⇒ Object

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.

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"“



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

def to_s(options = {})
  return value.to_s unless options.has_key?(:quotes) && atomic?
  *q = options[:quotes]
  [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).



150
151
152
# File 'lib/bibtex/value.rb', line 150

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