Class: BibTeX::Element
- Inherits:
-
Object
- Object
- BibTeX::Element
- Includes:
- Comparable
- Defined in:
- lib/bibtex/elements.rb
Overview
The base class for BibTeX objects.
Instance Attribute Summary collapse
-
#bibliography ⇒ Object
readonly
Returns the value of attribute bibliography.
-
#id ⇒ Object
Returns the element’s id.
Class Method Summary collapse
-
.parse(string, options = {}) ⇒ Object
Returns an array of BibTeX elements.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
-
#content(options = {}) ⇒ Object
(also: #to_s)
Returns a string containing the object’s content.
- #has_type?(type) ⇒ Boolean
-
#join ⇒ Object
Invokes BibTeX string joining on this element.
-
#matches?(query) ⇒ Boolean
(also: #===, #match?)
Returns true if the element matches the given query.
-
#meets?(*conditions) ⇒ Boolean
(also: #meet?)
Returns true if the element meets all of the given conditions.
-
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
-
#replace(*arguments) ⇒ Object
Invokes BibTeX string replacement on this element.
- #to_hash(options = {}) ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #to_yaml(options = {}) ⇒ Object
-
#type ⇒ Object
Returns the BibTeX type (if applicable) or the normalized class name.
Instance Attribute Details
#bibliography ⇒ Object (readonly)
Returns the value of attribute bibliography.
28 29 30 |
# File 'lib/bibtex/elements.rb', line 28 def bibliography @bibliography end |
#id ⇒ Object
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, = {}) elements = BibTeX::Parser.new().parse(string).data elements.each do |e| e.parse_names unless !e.respond_to?(:parse_names) || [:parse_names] == false e.parse_month unless !e.respond_to?(:parse_month) || [: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( = {}) '' end |
#has_type?(type) ⇒ 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 |
#join ⇒ Object
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.
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.
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( = {}) { type => content } end |
#to_json(options = {}) ⇒ Object
117 118 119 120 |
# File 'lib/bibtex/elements.rb', line 117 def to_json( = {}) 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( = {}) 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( = {}) require 'yaml' to_hash.to_yaml end |
#type ⇒ Object
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 |