Class: BibTeX::Names

Inherits:
Value
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bibtex/names.rb

Overview

A BibTeX Names value is an ordered list of name values.

Instance Attribute Summary

Attributes inherited from Value

#tokens

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#convert, #convert!, create, #date?, #include_token?, #initialize_copy, #inspect, #merge, #merge!, #method_missing, #respond_to?, #symbols, #to_date, #to_i

Constructor Details

#initialize(*arguments) ⇒ Names

Returns a new instance of Names.



36
37
38
39
40
41
# File 'lib/bibtex/names.rb', line 36

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 in the class BibTeX::Value

Class Method Details

.parse(string) ⇒ Object



29
30
31
32
33
34
# File 'lib/bibtex/names.rb', line 29

def self.parse(string)
  new(NameParser.new.parse(string))
rescue => e
  BibTeX.log.info(e.message)
  nil
end

Instance Method Details

#<=>(other) ⇒ Object



110
111
112
# File 'lib/bibtex/names.rb', line 110

def <=>(other)
  other.respond_to?(:to_a) ? to_a <=> other.to_a  : super
end

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



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bibtex/names.rb', line 88

def add(name)
  case
  when name.is_a?(Name)
    @tokens << name
  when name.respond_to?(:to_s)
    @tokens += Names.parse(name.to_s)
  else
    raise ArgumentError, "failed to add #{name.inspect}: not a name."
  end
  self
end

#atomic?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/bibtex/names.rb', line 69

def atomic?
  true
end

#joinObject



47
48
49
# File 'lib/bibtex/names.rb', line 47

def join
  self
end

#name?Boolean Also known as: names?

Returns:

  • (Boolean)


61
62
63
# File 'lib/bibtex/names.rb', line 61

def name?
  true
end

#numeric?Boolean Also known as: symbol?

Returns:

  • (Boolean)


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

def numeric?
  false
end

#replace(*arguments) ⇒ Object



43
44
45
# File 'lib/bibtex/names.rb', line 43

def replace(*arguments)
  self
end

#strip_bracesObject



84
85
86
# File 'lib/bibtex/names.rb', line 84

def strip_braces
  gsub!(/\{|\}/,'')
end

#to_citeproc(options = {}) ⇒ Object



80
81
82
# File 'lib/bibtex/names.rb', line 80

def to_citeproc(options = {})
  map { |n| n.to_citeproc(options) }
end

#to_nameObject



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

def to_name
  self
end

#to_s(options = {}) ⇒ Object



55
56
57
58
59
# File 'lib/bibtex/names.rb', line 55

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

#value(options = {}) ⇒ Object



51
52
53
# File 'lib/bibtex/names.rb', line 51

def value(options = {})
  @tokens.map { |n| n.to_s(options) }.join(' and ')
end