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.
28 29 30 |
# File 'lib/bibtex/elements.rb', line 28 def bibliography @bibliography end |
#id ⇒ Object
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, = {}) 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
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( = {}) '' 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
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 |
#inspect ⇒ Object
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 |
#join ⇒ Object
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.
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(/(!)?@(\*|\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.
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?
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?
129 130 131 |
# File 'lib/bibtex/elements.rb', line 129 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.
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( = {}) { type => content } end |
#to_json(options = {}) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/bibtex/elements.rb', line 153 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
161 162 163 164 165 166 |
# File 'lib/bibtex/elements.rb', line 161 def to_xml( = {}) 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( = {}) require 'yaml' to_hash.to_yaml end |
#type ⇒ Object
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 |