Class: BibTeX::Element

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bibtex/elements.rb

Overview

The base class for BibTeX objects.

Direct Known Subclasses

Comment, Entry, Error, MetaContent, Preamble, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bibliographyObject (readonly)

Returns the value of attribute bibliography


28
29
30
# File 'lib/bibtex/elements.rb', line 28

def bibliography
  @bibliography
end

#idObject

Returns the element's id.


70
# File 'lib/bibtex/elements.rb', line 70

def id; @id ||= object_id.to_s; end

Class Method Details

.parse(input, options = {}) ⇒ Object

Returns an array of BibTeX elements.


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bibtex/elements.rb', line 31

def self.parse(input, options = {})
  case input
  when Element
    [input]
  when Hash
    [Entry.new(input)]
  when Array
    input.inject([]) { |s,a| s.concat(parse(a, options)) }
  when ::String
    Parser.new(options).parse(input).data.each do |e|
      e.parse_names unless !e.respond_to?(:parse_names) || options[:parse_names] == false
      e.parse_month unless !e.respond_to?(:parse_month) || options[:parse_months] == false
    end
  else
    raise ArgumentError, "failed to parse Element from #{input.inspect}"
  end
end

Instance Method Details

#<=>(other) ⇒ Object


181
182
183
184
# File 'lib/bibtex/elements.rb', line 181

def <=>(other)
  return nil unless other.respond_to? :type and other.respond_to? :to_s
  [type, to_s] <=> [other.type, other.to_s]
end

#added_to_bibliography(bibliography) ⇒ Object

Called when the element was added to a bibliography.


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

def added_to_bibliography(bibliography)
  # raise BibTeXError, "failed to add element to Bibliography: already registered with another Bibliography" unless @bibliography.nil?
  @bibliography = bibliography
  self
end

#content(options = {}) ⇒ Object Also known as: to_s

Returns a string containing the object's content.


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

def content(options = {})
  ''
end

#digest(*arguments) ⇒ Object


59
60
61
# File 'lib/bibtex/elements.rb', line 59

def digest(*arguments)
  [type, content].join('|')
end

#has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/bibtex/elements.rb', line 82

def has_type?(type)
  self.type == type.intern || defined?(type) == 'constant' && is_a?(type)
end

#inspectObject

Returns the Element as a nicely formatted string.


187
188
189
# File 'lib/bibtex/elements.rb', line 187

def inspect
  "#<#{self.class} #{content.gsub(/\n/, ' ')}>"
end

#joinObject

Invokes BibTeX string joining on this element.


67
# File 'lib/bibtex/elements.rb', line 67

def join; self; end

#matches?(query) ⇒ Boolean Also known as: ===, match?

Returns true if the element matches the given query.

Returns:

  • (Boolean)

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bibtex/elements.rb', line 92

def matches?(query)
  return true if query.nil? || query.respond_to?(:empty?) && query.empty?

  case query
  when Symbol
    query.to_s == id.to_s
  when Element
    query == self
  when Regexp
    to_s.match(query)
  when /^\/(.+)\/$/
    to_s.match(Regexp.new($1))
  when /@(\*|\w+)(?:\[([^\]]*)\])?/
    query.scan(/(!)[email protected](\*|\w+)(?:\[([^\]]*)\])?/).any? do |non, type, condition|
      if (non ? !has_type?(type) : has_type?(type))
        if condition.nil? || condition.empty?
          true
        else
          condition.to_s.split(/\s*\|\|\s*/).any? do |conditions|
            meets_all? conditions.split(/\s*(?:,|&&)\s*/)
          end
        end
      end
    end
  else
    id.to_s == query
  end
end

#meets?(conditions, op = :all?) ⇒ Boolean Also known as: meet?

Returns true if the element meets all or any of the given conditions.

Returns:

  • (Boolean)

135
136
137
138
139
# File 'lib/bibtex/elements.rb', line 135

def meets?(conditions, op = :all?)
  conditions.send(op) do |condition|
    meets_condition? condition
  end
end

#meets_all?(*conditions) ⇒ Boolean Also known as: meet_all?

Returns:

  • (Boolean)

124
125
126
# File 'lib/bibtex/elements.rb', line 124

def meets_all?(*conditions)
  meets? conditions.flatten, :all?
end

#meets_any?(*conditions) ⇒ Boolean Also known as: meet_any?

Returns:

  • (Boolean)

129
130
131
# File 'lib/bibtex/elements.rb', line 129

def meets_any?(*conditions)
  meets? conditions.flatten, :any?
end

#namesObject

Returns a list of names for that Element. All Elements except Entries return an empty list.


78
79
80
# File 'lib/bibtex/elements.rb', line 78

def names
  []
end

#removed_from_bibliography(bibliography) ⇒ Object

Called when the element was removed from a bibliography.


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

def removed_from_bibliography(bibliography)
  @bibliography = nil
  self
end

#replace(*arguments) ⇒ Object

Invokes BibTeX string replacement on this element.


64
# File 'lib/bibtex/elements.rb', line 64

def replace(*arguments); self; end

#to_hash(options = {}) ⇒ Object


144
145
146
# File 'lib/bibtex/elements.rb', line 144

def to_hash(options = {})
  { type => content }
end

#to_json(options = {}) ⇒ Object


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

def to_json(options = {})
  # Some JSON implementations pass an argument
  # to this method.
  options = {} unless options.is_a?(::Hash)

  ::JSON.dump(to_hash(options))
end

#to_xml(options = {}) ⇒ Object


161
162
163
164
165
166
# File 'lib/bibtex/elements.rb', line 161

def to_xml(options = {})
  require 'rexml/document'
  xml = REXML::Element.new(type)
  xml.text = content
  xml
end

#to_yaml(options = {}) ⇒ Object


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

def to_yaml(options = {})
  require 'yaml'
  to_hash.to_yaml
end

#typeObject

Returns the BibTeX type (if applicable) or the normalized class name.


73
74
75
# File 'lib/bibtex/elements.rb', line 73

def type
  self.class.name.split(/::/).last.gsub(/([[:lower:]])([[:upper:]])/) { "#{$1}_#{$2}" }.downcase.intern
end

#values_at(*arguments) ⇒ Object

Returns a string containing the object's content.


55
56
57
# File 'lib/bibtex/elements.rb', line 55

def values_at(*arguments)
  []
end