class BibTeX::Element

The base class for BibTeX objects.

Attributes

bibliography[R]
id[W]

Public Class Methods

parse(input, options = {}) click to toggle source

Returns an array of BibTeX elements.

# File lib/bibtex/elements.rb, line 30
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

Public Instance Methods

<=>(other) click to toggle source
# File lib/bibtex/elements.rb, line 186
def <=>(other)
  return nil unless other.respond_to?(:type) && other.respond_to?(:to_s)

  [type, to_s] <=> [other.type, other.to_s]
end
===(query)
Alias for: matches?
added_to_bibliography(bibliography) click to toggle source

Called when the element was added to a bibliography.

# File lib/bibtex/elements.rb, line 174
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 = {}) click to toggle source

Returns a string containing the object's content.

# File lib/bibtex/elements.rb, line 49
def content(_options = {})
  ''
end
Also aliased as: to_s
digest(*_arguments) click to toggle source
# File lib/bibtex/elements.rb, line 58
def digest(*_arguments)
  [type, content].join('|')
end
has_type?(type) click to toggle source
# File lib/bibtex/elements.rb, line 87
def has_type?(type)
  self.type == type.intern || defined?(type) == 'constant' && is_a?(type)
end
id() click to toggle source

Returns the element's id.

# File lib/bibtex/elements.rb, line 73
def id
  @id ||= object_id.to_s
end
inspect() click to toggle source

Returns the Element as a nicely formatted string.

# File lib/bibtex/elements.rb, line 193
def inspect
  "#<#{self.class} #{content.gsub(/\n/, ' ')}>"
end
join() click to toggle source

Invokes BibTeX string joining on this element.

# File lib/bibtex/elements.rb, line 68
def join
  self
end
match?(query)
Alias for: matches?
matches?(query) click to toggle source

Returns true if the element matches the given query.

# File lib/bibtex/elements.rb, line 97
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 %r{^/(.+)/$}
    to_s.match(Regexp.new(Regexp.last_match(1)))
  when /@(\*|\w+)(?:\[([^\]]*)\])?/
    query.scan(/(!)?@(\*|\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
Also aliased as: ===, match?
meet?(conditions, op = :all?)
Alias for: meets?
meet_all?(*conditions)
Alias for: meets_all?
meet_any?(*conditions)
Alias for: meets_any?
meets?(conditions, op = :all?) click to toggle source

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

# File lib/bibtex/elements.rb, line 140
def meets?(conditions, op = :all?)
  conditions.send(op) do |condition|
    meets_condition? condition
  end
end
Also aliased as: meet?
meets_all?(*conditions) click to toggle source
# File lib/bibtex/elements.rb, line 129
def meets_all?(*conditions)
  meets? conditions.flatten, :all?
end
Also aliased as: meet_all?
meets_any?(*conditions) click to toggle source
# File lib/bibtex/elements.rb, line 134
def meets_any?(*conditions)
  meets? conditions.flatten, :any?
end
Also aliased as: meet_any?
names() click to toggle source

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

# File lib/bibtex/elements.rb, line 83
def names
  []
end
removed_from_bibliography(_bibliography) click to toggle source

Called when the element was removed from a bibliography.

# File lib/bibtex/elements.rb, line 181
def removed_from_bibliography(_bibliography)
  @bibliography = nil
  self
end
replace(*_arguments) click to toggle source

Invokes BibTeX string replacement on this element.

# File lib/bibtex/elements.rb, line 63
def replace(*_arguments)
  self
end
to_hash(_options = {}) click to toggle source
# File lib/bibtex/elements.rb, line 149
def to_hash(_options = {})
  { type => content }
end
to_json(options = {}) click to toggle source
# File lib/bibtex/elements.rb, line 158
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_s(_options = {})
Alias for: content
to_xml(_options = {}) click to toggle source
# File lib/bibtex/elements.rb, line 166
def to_xml(_options = {})
  require 'rexml/document'
  xml = REXML::Element.new(type)
  xml.text = content
  xml
end
to_yaml(_options = {}) click to toggle source
# File lib/bibtex/elements.rb, line 153
def to_yaml(_options = {})
  require 'yaml'
  to_hash.to_yaml
end
type() click to toggle source

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

# File lib/bibtex/elements.rb, line 78
def type
  self.class.name.split(/::/).last.gsub(/([[:lower:]])([[:upper:]])/) { "#{Regexp.last_match(1)}_#{Regexp.last_match(2)}" }.downcase.intern
end
values_at(*_arguments) click to toggle source

Returns a string containing the object's content.

# File lib/bibtex/elements.rb, line 54
def values_at(*_arguments)
  []
end

Private Instance Methods

meets_condition?(condition) click to toggle source
# File lib/bibtex/elements.rb, line 199
def meets_condition?(condition)
  property, operator, value = condition.split(%r{\s*([!~/^<>]?=|!~|=~|<|>)\s*})

  if property.nil?
    true
  else
    property.strip!
    value&.strip!

    if operator.nil? && value.nil?
      respond_to?(:provides?) && provides?(property)
    else

      # HACK: we need to get rid of #type returning the bibtex_type,
      # because type is a valid BibTeX property. This mitigates the
      # issue but is no fix!
      actual = if property == 'type'
                 respond_to?(:fields) ? fields[:type] : nil
               else
                 respond_to?(property) ? send(property) : nil
               end

      case operator
      when '!=', '/='
        actual.nil? || actual.to_s != value
      when '^='
        !actual.nil? && actual.to_s.match("^#{value}")
      when '~=', '=~'
        !actual.nil? && actual.to_s.match(value)
      when '!~'
        actual.nil? || !actual.to_s.match(value)
      when '<=', '>=', '<', '>'
        if actual.nil?
          false
        elsif actual =~ /^\d+$/ && value.to_s =~ /^\d+$/
          actual.to_i.send operator, value.to_i
        else
          actual.send operator, value.to_s
        end
      else
        !actual.nil? && actual.to_s == value
      end
    end
  end
end