Class: BibSync::Entry
- Inherits:
-
Object
- Object
- BibSync::Entry
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/bibsync/entry.rb
Instance Attribute Summary collapse
-
#bibliography ⇒ Object
Returns the value of attribute bibliography.
-
#key ⇒ Object
Returns the value of attribute key.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #comment? ⇒ Boolean
- #delete(key) ⇒ Object
- #dirty! ⇒ Object
- #file ⇒ Object
- #file=(file) ⇒ Object
-
#initialize(fields = {}) ⇒ Entry
constructor
A new instance of Entry.
- #parse(text) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(fields = {}) ⇒ Entry
Returns a new instance of Entry.
14 15 16 17 18 19 |
# File 'lib/bibsync/entry.rb', line 14 def initialize(fields = {}) @fields = {} self.key = fields.delete(:key) if fields.include?(:key) self.type = fields.delete(:type) if fields.include?(:type) fields.each {|k,v| self[k] = v } end |
Instance Attribute Details
#bibliography ⇒ Object
Returns the value of attribute bibliography.
6 7 8 |
# File 'lib/bibsync/entry.rb', line 6 def bibliography @bibliography end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/bibsync/entry.rb', line 7 def key @key end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/bibsync/entry.rb', line 6 def type @type end |
Class Method Details
Instance Method Details
#[](key) ⇒ Object
49 50 51 |
# File 'lib/bibsync/entry.rb', line 49 def [](key) @fields[convert_key(key)] end |
#[]=(key, value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bibsync/entry.rb', line 53 def []=(key, value) if value key = convert_key(key) value = Literal === value ? Literal.new(value.to_s.strip) : value.to_s.strip if @fields[key] != value || @fields[key].class != value.class @fields[key] = value dirty! end else delete(key) end end |
#comment? ⇒ Boolean
74 75 76 |
# File 'lib/bibsync/entry.rb', line 74 def comment? type.to_s.downcase == 'comment' end |
#delete(key) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/bibsync/entry.rb', line 66 def delete(key) key = convert_key(key) if @fields.include?(key) @fields.delete(key) dirty! end end |
#dirty! ⇒ Object
78 79 80 |
# File 'lib/bibsync/entry.rb', line 78 def dirty! bibliography.dirty! if bibliography end |
#file ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/bibsync/entry.rb', line 40 def file if self[:file] raise 'No bibliography set' unless bibliography _, file, type = self[:file].split(':', 3) path = File.join(File.absolute_path(File.dirname(bibliography.file)), file) { name: File.basename(path), type: type.upcase, path: path } end end |
#file=(file) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/bibsync/entry.rb', line 33 def file=(file) raise 'No bibliography set' unless bibliography file =~ /\.(\w+)\Z/ self[:file] = ":#{bibliography.relative_path(file)}:#{$1.upcase}" # JabRef file format "description:path:type" file end |
#parse(text) ⇒ Object
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 120 121 122 123 124 |
# File 'lib/bibsync/entry.rb', line 92 def parse(text) raise 'Unexpected token' if text !~ /\A\s*@(\w+)\s*\{/ self.type = $1 text = $' if comment? text, self[:comment] = parse_field(text) else raise 'Expected entry key' if text !~ /([^,]+),\s*/ self.key = $1.strip text = $' until text.empty? case text when /\A(\s+|%[^\n]+\n)/ text = $' when /\A\s*(\w+)\s*=\s*/ text, key = $', $1 if text =~ /\A\{/ text, self[key] = parse_field(text) else text, value = parse_field(text) self[key] = Literal.new(value) end else break end end end raise 'Expected closing }' unless text =~ /\A\s*\}/ $' end |
#to_s ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/bibsync/entry.rb', line 82 def to_s s = "@#{type}{" if comment? s << self[:comment] else s << "#{key},\n" << to_a.map {|k,v| Literal === v ? " #{k} = #{v}" : " #{k} = {#{v}}" }.join(",\n") << "\n" end s << "}\n" end |