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(input, 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.
- #digest(*_arguments) ⇒ Object
- #has_type?(type) ⇒ Boolean
-
#inspect ⇒ Object
Returns the Element as a nicely formatted string.
-
#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, op = :all?) ⇒ Boolean
(also: #meet?)
Returns true if the element meets all or any of the given conditions.
- #meets_all?(*conditions) ⇒ Boolean (also: #meet_all?)
- #meets_any?(*conditions) ⇒ Boolean (also: #meet_any?)
-
#names ⇒ Object
Returns a list of names for that Element.
-
#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.
-
#values_at(*_arguments) ⇒ Object
Returns a string containing the object’s content.
Instance Attribute Details
#bibliography ⇒ Object (readonly)
Returns the value of attribute bibliography.
27 28 29 |
# File 'lib/bibtex/elements.rb', line 27 def bibliography @bibliography end |
#id ⇒ Object
Returns the element’s id.
73 74 75 |
# File 'lib/bibtex/elements.rb', line 73 def id @id ||= object_id.to_s end |
Class Method Details
.parse(input, options = {}) ⇒ Object
Returns an array of BibTeX elements.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bibtex/elements.rb', line 30 def self.parse(input, = {}) case input when Element [input] when Hash [Entry.new(input)] when Array input.inject([]) { |s, a| s.concat(parse(a, )) } when ::String Parser.new().parse(input).data.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 else raise ArgumentError, "failed to parse Element from #{input.inspect}" end end |
Instance Method Details
#<=>(other) ⇒ Object
186 187 188 189 190 |
# 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 |
#added_to_bibliography(bibliography) ⇒ Object
Called when the element was added to a bibliography.
174 175 176 177 178 |
# 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 = {}) ⇒ Object Also known as: to_s
Returns a string containing the object’s content.
49 50 51 |
# File 'lib/bibtex/elements.rb', line 49 def content( = {}) '' end |
#digest(*_arguments) ⇒ Object
58 59 60 |
# File 'lib/bibtex/elements.rb', line 58 def digest(*_arguments) [type, content].join('|') end |
#has_type?(type) ⇒ Boolean
87 88 89 |
# File 'lib/bibtex/elements.rb', line 87 def has_type?(type) self.type == type.intern || defined?(type) == 'constant' && is_a?(type) end |
#inspect ⇒ Object
Returns the Element as a nicely formatted string.
193 194 195 |
# File 'lib/bibtex/elements.rb', line 193 def inspect "#<#{self.class} #{content.gsub(/\n/, ' ')}>" end |
#join ⇒ Object
Invokes BibTeX string joining on this element.
68 69 70 |
# File 'lib/bibtex/elements.rb', line 68 def join self end |
#matches?(query) ⇒ Boolean Also known as: ===, match?
Returns true if the element matches the given query.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# 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 |
#meets?(conditions, op = :all?) ⇒ Boolean Also known as: meet?
Returns true if the element meets all or any of the given conditions.
140 141 142 143 144 |
# File 'lib/bibtex/elements.rb', line 140 def meets?(conditions, op = :all?) conditions.send(op) do |condition| meets_condition? condition end end |
#meets_all?(*conditions) ⇒ Boolean Also known as: meet_all?
129 130 131 |
# File 'lib/bibtex/elements.rb', line 129 def meets_all?(*conditions) meets? conditions.flatten, :all? end |
#meets_any?(*conditions) ⇒ Boolean Also known as: meet_any?
134 135 136 |
# File 'lib/bibtex/elements.rb', line 134 def meets_any?(*conditions) meets? conditions.flatten, :any? end |
#names ⇒ Object
Returns a list of names for that Element. All Elements except Entries return an empty list.
83 84 85 |
# File 'lib/bibtex/elements.rb', line 83 def names [] end |
#removed_from_bibliography(_bibliography) ⇒ Object
Called when the element was removed from a bibliography.
181 182 183 184 |
# File 'lib/bibtex/elements.rb', line 181 def removed_from_bibliography(_bibliography) @bibliography = nil self end |
#replace(*_arguments) ⇒ Object
Invokes BibTeX string replacement on this element.
63 64 65 |
# File 'lib/bibtex/elements.rb', line 63 def replace(*_arguments) self end |
#to_hash(_options = {}) ⇒ Object
149 150 151 |
# File 'lib/bibtex/elements.rb', line 149 def to_hash( = {}) { type => content } end |
#to_json(options = {}) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/bibtex/elements.rb', line 158 def to_json( = {}) # Some JSON implementations pass an argument # to this method. = {} unless .is_a?(::Hash) ::JSON.dump(to_hash()) end |
#to_xml(_options = {}) ⇒ Object
166 167 168 169 170 171 |
# File 'lib/bibtex/elements.rb', line 166 def to_xml( = {}) require 'rexml/document' xml = REXML::Element.new(type) xml.text = content xml end |
#to_yaml(_options = {}) ⇒ Object
153 154 155 156 |
# File 'lib/bibtex/elements.rb', line 153 def to_yaml( = {}) require 'yaml' to_hash.to_yaml end |
#type ⇒ Object
Returns the BibTeX type (if applicable) or the normalized class name.
78 79 80 |
# 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) ⇒ Object
Returns a string containing the object’s content.
54 55 56 |
# File 'lib/bibtex/elements.rb', line 54 def values_at(*_arguments) [] end |