Class: BibTeX::Value
- Inherits:
-
Object
- Object
- BibTeX::Value
- 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
Instance Attribute Summary collapse
-
#tokens ⇒ Object
(also: #to_a)
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
-
.create(*args) ⇒ Object
call-seq: create(other) => other.dup create(*args) => Value.new(args).
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add(argument) ⇒ Object (also: #<<, #push)
-
#atomic? ⇒ Boolean
Returns true if the Value is empty or consists of a single token.
-
#convert(*filters) ⇒ Object
Returns a new Value with all string values converted according to the given filter(s).
-
#convert!(*filters) ⇒ Object
Converts all string values according to the given filter(s).
-
#date? ⇒ Boolean
Returns true if the Value’s content is a date.
- #include_token?(token) ⇒ Boolean
-
#initialize(*arguments) ⇒ Value
constructor
A new instance of Value.
- #initialize_copy(other) ⇒ Object
- #inspect ⇒ Object
-
#join(separator = '') ⇒ Value
call-seq: Value.new(‘foo’, ‘bar’).join #=> <‘foobar’> Value.new(:foo, ‘bar’).join #=> <:foo, ‘bar’>.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #method_missing(name, *args) ⇒ Object
-
#name? ⇒ Boolean
(also: #names?)
Returns true if the value is a BibTeX name value.
-
#numeric? ⇒ Boolean
Returns true if the Value’s content is numeric.
- #replace(*arguments) ⇒ Object
- #respond_to?(method, include_all = false) ⇒ Boolean
-
#symbol? ⇒ Boolean
(also: #has_symbol?)
Returns true if the Value contains at least one symbol.
-
#symbols ⇒ Object
Returns all symbols contained in the Value.
- #to_citeproc(options = {}) ⇒ Object
-
#to_date ⇒ Object
Returns the string as a date.
- #to_i ⇒ Object
- #to_name ⇒ Object (also: #to_names)
-
#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’).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) #=> “ü”.
-
#value ⇒ Object
(also: #v)
Returns the Value as a string or, if it consists of a single symbol, as a Symbol instance.
Constructor Details
#initialize(*arguments) ⇒ Value
Returns a new instance of Value.
73 74 75 76 77 78 |
# File 'lib/bibtex/value.rb', line 73 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
276 277 278 279 280 281 282 283 |
# File 'lib/bibtex/value.rb', line 276 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
#tokens ⇒ Object (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.
69 70 71 |
# File 'lib/bibtex/value.rb', line 69 def self.create(*args) args[0].class < Value && args.size == 1 ? args[0].dup : Value.new(args) end |
Instance Method Details
#<=>(other) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/bibtex/value.rb', line 289 def <=> (other) if numeric? && other.respond_to?(:numeric?) && other.numeric? to_i <=> other.to_i else to_s <=> other.to_s end end |
#add(argument) ⇒ Object Also known as: <<, push
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bibtex/value.rb', line 100 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.
208 209 210 |
# File 'lib/bibtex/value.rb', line 208 def atomic? @tokens.length < 2 end |
#convert(*filters) ⇒ Object
Returns a new Value with all string values converted according to the given filter(s).
262 263 264 |
# File 'lib/bibtex/value.rb', line 262 def convert (*filters) dup.convert!(*filters) end |
#convert!(*filters) ⇒ Object
Converts all string values according to the given filter(s).
267 268 269 270 271 272 273 274 |
# File 'lib/bibtex/value.rb', line 267 def convert! (*filters) filters.flatten.each do |filter| f = Filters.resolve!(filter) @tokens.map! { |t| f.apply(t) } end self end |
#date? ⇒ Boolean
Returns true if the Value’s content is a date.
224 225 226 |
# File 'lib/bibtex/value.rb', line 224 def date? !to_date.nil? end |
#include_token?(token) ⇒ Boolean
96 97 98 |
# File 'lib/bibtex/value.rb', line 96 def include_token?(token) @tokens.include?(token) end |
#initialize_copy(other) ⇒ Object
80 81 82 |
# File 'lib/bibtex/value.rb', line 80 def initialize_copy(other) @tokens = other.tokens.dup end |
#inspect ⇒ Object
203 204 205 |
# File 'lib/bibtex/value.rb', line 203 def inspect "#<#{self.class} #{@tokens.map(&:inspect).join(', ')}>" end |
#join(separator = '') ⇒ Value
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/bibtex/value.rb', line 153 def join(separator = '') @tokens = @tokens.inject([]) do |a,b| if a[-1].is_a?(::String) && b.is_a?(::String) a[-1] = [a[-1], b].join(separator) else a << b end a end self end |
#merge(other) ⇒ Object
84 85 86 |
# File 'lib/bibtex/value.rb', line 84 def merge(other) dup.merge!(other) end |
#merge!(other) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/bibtex/value.rb', line 88 def merge!(other) other.tokens.each do |token| add token unless include_token?(token) end self end |
#name? ⇒ Boolean Also known as: names?
Returns true if the value is a BibTeX name value.
213 |
# File 'lib/bibtex/value.rb', line 213 def name?; false; end |
#numeric? ⇒ Boolean
Returns true if the Value’s content is numeric.
237 238 239 |
# File 'lib/bibtex/value.rb', line 237 def numeric? atomic? && @tokens[0] =~ /^\s*[+-]?\d+\s*$/ end |
#replace(*arguments) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bibtex/value.rb', line 130 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, include_all = false) ⇒ Boolean
285 286 287 |
# File 'lib/bibtex/value.rb', line 285 def respond_to? (method, include_all=false) method =~ /^(?:convert|from)_([a-z]+)(!)?$/ || super end |
#symbol? ⇒ Boolean Also known as: has_symbol?
Returns true if the Value contains at least one symbol.
250 251 252 |
# File 'lib/bibtex/value.rb', line 250 def symbol? @tokens.detect { |v| v.is_a?(Symbol) } end |
#symbols ⇒ Object
Returns all symbols contained in the Value.
257 258 259 |
# File 'lib/bibtex/value.rb', line 257 def symbols @tokens.select { |v| v.is_a?(Symbol) } end |
#to_citeproc(options = {}) ⇒ Object
245 246 247 |
# File 'lib/bibtex/value.rb', line 245 def to_citeproc ( = {}) to_s() end |
#to_date ⇒ Object
Returns the string as a date.
229 230 231 232 233 234 |
# File 'lib/bibtex/value.rb', line 229 def to_date require 'date' Date.parse(to_s) rescue nil end |
#to_i ⇒ Object
241 242 243 |
# File 'lib/bibtex/value.rb', line 241 def to_i @tokens[0].to_i end |
#to_name ⇒ Object Also known as: to_names
217 218 219 |
# File 'lib/bibtex/value.rb', line 217 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').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; 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.
182 183 184 185 186 187 |
# File 'lib/bibtex/value.rb', line 182 def to_s( = {}) return convert(.delete(:filter)).to_s() if .has_key?(:filter) return value.to_s unless .has_key?(:quotes) && atomic? && !symbol? q = Array([:quotes]) [q[0], value, q[-1]].compact.join end |
#value ⇒ Object 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).
193 194 195 196 197 198 199 |
# File 'lib/bibtex/value.rb', line 193 def value if atomic? @tokens[0] else @tokens.map { |v| v.is_a?(::String) ? v.inspect : v }.join(' # ') end end |