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.
- #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) ⇒ Boolean
(also: #meet?)
Returns true if the element meets all of the given conditions.
-
#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.
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.
59 |
# File 'lib/bibtex/elements.rb', line 59 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
150 151 152 153 |
# File 'lib/bibtex/elements.rb', line 150 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.
138 139 140 141 142 |
# File 'lib/bibtex/elements.rb', line 138 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 |
# File 'lib/bibtex/elements.rb', line 50 def content( = {}); ''; end |
#has_type?(type) ⇒ Boolean
71 72 73 |
# File 'lib/bibtex/elements.rb', line 71 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.
156 157 158 |
# File 'lib/bibtex/elements.rb', line 156 def inspect "#<#{self.class} #{content.gsub(/\n/, ' ')}>" end |
#join ⇒ Object
Invokes BibTeX string joining on this element.
56 |
# File 'lib/bibtex/elements.rb', line 56 def join; self; end |
#matches?(query) ⇒ Boolean Also known as: ===, match?
Returns true if the element matches the given query.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bibtex/elements.rb', line 81 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 /@(\*|\w+)(?:\[([^\]]*)\])?/ query.scan(/@(\*|\w+)(?:\[([^\]]*)\])?/).any? do |type, condition| has_type?(type) && ( condition.nil? || meets?(condition.split(/,\s*/)) ) end when /^\/(.+)\/$/ to_s.match(Regexp.new($1)) else id.to_s == query end end |
#meets?(*conditions) ⇒ Boolean Also known as: meet?
Returns true if the element meets all of the given conditions.
106 107 108 109 110 111 |
# File 'lib/bibtex/elements.rb', line 106 def meets?(*conditions) conditions.flatten.all? do |condition| property, value = condition.split(/\s*=\s*/) property.nil? || (respond_to?(property) && send(property).to_s == value) end end |
#names ⇒ Object
Returns a list of names for that Element. All Elements except Entries return an empty list.
67 68 69 |
# File 'lib/bibtex/elements.rb', line 67 def names [] end |
#removed_from_bibliography(bibliography) ⇒ Object
Called when the element was removed from a bibliography.
145 146 147 148 |
# File 'lib/bibtex/elements.rb', line 145 def removed_from_bibliography(bibliography) @bibliography = nil self end |
#replace(*arguments) ⇒ Object
Invokes BibTeX string replacement on this element.
53 |
# File 'lib/bibtex/elements.rb', line 53 def replace(*arguments); self; end |
#to_hash(options = {}) ⇒ Object
117 118 119 |
# File 'lib/bibtex/elements.rb', line 117 def to_hash( = {}) { type => content } end |
#to_json(options = {}) ⇒ Object
126 127 128 |
# File 'lib/bibtex/elements.rb', line 126 def to_json( = {}) MultiJson.encode(to_hash()) end |
#to_xml(options = {}) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/bibtex/elements.rb', line 130 def to_xml( = {}) require 'rexml/document' xml = REXML::Element.new(type) xml.text = content xml end |
#to_yaml(options = {}) ⇒ Object
121 122 123 124 |
# File 'lib/bibtex/elements.rb', line 121 def to_yaml( = {}) require 'yaml' to_hash.to_yaml end |
#type ⇒ Object
Returns the BibTeX type (if applicable) or the normalized class name.
62 63 64 |
# File 'lib/bibtex/elements.rb', line 62 def type self.class.name.split(/::/).last.gsub(/([[:lower:]])([[:upper:]])/) { "#{$1}_#{$2}" }.downcase.intern end |