Class: BibSync::Bibliography
- Inherits:
-
Object
- Object
- BibSync::Bibliography
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/bibsync/bibliography.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#format_hook ⇒ Object
Returns the value of attribute format_hook.
-
#transform_hook ⇒ Object
Returns the value of attribute transform_hook.
Instance Method Summary collapse
- #<<(entry) ⇒ Object
- #[](key) ⇒ Object
- #clear ⇒ Object
- #delete(entry) ⇒ Object
- #dirty! ⇒ Object
- #dirty? ⇒ Boolean
-
#initialize(file = nil) ⇒ Bibliography
constructor
A new instance of Bibliography.
- #load(file, check = true) ⇒ Object
- #parse(text) ⇒ Object
- #relative_path(file) ⇒ Object
- #save(file = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(file = nil) ⇒ Bibliography
Returns a new instance of Bibliography.
11 12 13 14 15 |
# File 'lib/bibsync/bibliography.rb', line 11 def initialize(file = nil) @entries = {} @transform_hook, @format_hook = nil, nil load(file) end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
6 7 8 |
# File 'lib/bibsync/bibliography.rb', line 6 def file @file end |
#format_hook ⇒ Object
Returns the value of attribute format_hook.
7 8 9 |
# File 'lib/bibsync/bibliography.rb', line 7 def format_hook @format_hook end |
#transform_hook ⇒ Object
Returns the value of attribute transform_hook.
7 8 9 |
# File 'lib/bibsync/bibliography.rb', line 7 def transform_hook @transform_hook end |
Instance Method Details
#<<(entry) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/bibsync/bibliography.rb', line 74 def <<(entry) raise 'Entry has no key' if !entry.key || entry.key.empty? raise 'Entry is already existing' if @entries.include?(entry.key) entry.bibliography = self @entries[entry.key] = entry dirty! end |
#[](key) ⇒ Object
25 26 27 |
# File 'lib/bibsync/bibliography.rb', line 25 def [](key) @entries[key.to_s] end |
#clear ⇒ Object
37 38 39 40 41 42 |
# File 'lib/bibsync/bibliography.rb', line 37 def clear unless @entries.empty? @entries.clear dirty! end end |
#delete(entry) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/bibsync/bibliography.rb', line 29 def delete(entry) if @entries.include?(entry.key) @entries.delete(entry.key) entry.bibliography = nil dirty! end end |
#dirty! ⇒ Object
21 22 23 |
# File 'lib/bibsync/bibliography.rb', line 21 def dirty! @dirty = true end |
#dirty? ⇒ Boolean
17 18 19 |
# File 'lib/bibsync/bibliography.rb', line 17 def dirty? @dirty end |
#load(file, check = true) ⇒ Object
82 83 84 85 86 |
# File 'lib/bibsync/bibliography.rb', line 82 def load(file, check = true) parse(File.read(file)) if !check || (file && File.exists?(file)) @file = file @dirty = false end |
#parse(text) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bibsync/bibliography.rb', line 88 def parse(text) until text.empty? case text when /\A(\s+|%[^\n]+\n)/ text = $' else entry = Entry.new text = entry.parse(text) entry.key ||= "entry#{@entries.size}" # Number of entries for comment id self << entry end end end |
#relative_path(file) ⇒ Object
44 45 46 47 48 |
# File 'lib/bibsync/bibliography.rb', line 44 def relative_path(file) raise 'No filename given' unless @file bibpath = File.absolute_path(File.dirname(@file)) Pathname.new(file).realpath.relative_path_from(Pathname.new(bibpath)).to_s end |
#save(file = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bibsync/bibliography.rb', line 50 def save(file = nil) if file @file = file dirty! end raise 'No filename given' unless @file if @dirty @transform_hook.call(self) if @transform_hook tmpfile = "#{@file}.tmp" begin File.open(tmpfile, 'w') {|f| f.write(self) } @format_hook.call(tmpfile) if @format_hook File.rename(tmpfile, @file) ensure File.unlink(tmpfile) rescue nil end @dirty = false true else false end end |
#to_s ⇒ Object
102 103 104 105 |
# File 'lib/bibsync/bibliography.rb', line 102 def to_s "% #{DateTime.now}\n% Encoding: UTF8\n\n" << @entries.values.join("\n") << "\n" end |