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.



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

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

Class Method Details

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

Returns an array of BibTeX elements.



31
32
33
34
35
36
37
38
# File 'lib/bibtex/elements.rb', line 31

def self.parse(string, options = {})
  elements = BibTeX::Parser.new(options).parse(string).data
  elements.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
  elements
end

Instance Method Details

#<=>(other) ⇒ Object



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

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.



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

def added_to_bibliography(bibliography)
	@bibliography = bibliography
	self
end

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

Returns a string containing the object’s content.



41
42
43
# File 'lib/bibtex/elements.rb', line 41

def content(options = {})
	''
end

#has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#joinObject

Invokes BibTeX string joining on this element.



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

def join; self; end

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

Returns true if the element matches the given query.

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bibtex/elements.rb', line 72

def matches?(query)
  return true if query.nil? || query.respond_to?(:empty?) && query.empty?
  
  case query
  when Element
    self == query
  when Symbol
    id == query
  when Regexp
    to_s.match(query)
  when /^\/(.+)\/$/
    to_s.match(Regexp.new($1))
  when /@(\w+)(?:\[([^\]]*)\])?/
    query.scan(/@(\w+)(?:\[([^\]]*)\])?/).any? do |type, condition|
      has_type?(type) && ( condition.nil? || meets?(condition.split(/,\s*/)) )
    end
  else
    id == query.to_sym
  end      
end

#meets?(*conditions) ⇒ Boolean Also known as: meet?

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

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/bibtex/elements.rb', line 97

def meets?(*conditions)
  conditions.flatten.all? do |condition|
    property, value = condition.split(/\s*=\s*/)
    property.nil? || send(property).to_s == value
  end
end

#removed_from_bibliography(bibliography) ⇒ Object

Called when the element was removed from a bibliography.



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

def removed_from_bibliography(bibliography)
	@bibliography = nil
	self
end

#replace(*arguments) ⇒ Object

Invokes BibTeX string replacement on this element.



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

def replace(*arguments); self; end

#to_hash(options = {}) ⇒ Object



108
109
110
# File 'lib/bibtex/elements.rb', line 108

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

#to_json(options = {}) ⇒ Object



117
118
119
120
# File 'lib/bibtex/elements.rb', line 117

def to_json(options = {})
  require 'json'
  to_hash.to_json
end

#to_xml(options = {}) ⇒ Object



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

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

#to_yaml(options = {}) ⇒ Object



112
113
114
115
# File 'lib/bibtex/elements.rb', line 112

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

#typeObject

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



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

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