Class: BibSync::Bibliography

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/bibsync/bibliography.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Bibliography

Returns a new instance of Bibliography.



11
12
13
14
# File 'lib/bibsync/bibliography.rb', line 11

def initialize(file = nil)
  @entries, @save_hook = {}, nil
  load(file)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/bibsync/bibliography.rb', line 6

def file
  @file
end

#save_hookObject

Returns the value of attribute save_hook.



7
8
9
# File 'lib/bibsync/bibliography.rb', line 7

def save_hook
  @save_hook
end

Instance Method Details

#<<(entry) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/bibsync/bibliography.rb', line 67

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



24
25
26
# File 'lib/bibsync/bibliography.rb', line 24

def [](key)
  @entries[key.to_s]
end

#clearObject



36
37
38
39
40
41
# File 'lib/bibsync/bibliography.rb', line 36

def clear
  unless @entries.empty?
    @entries.clear
    dirty!
  end
end

#delete(entry) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/bibsync/bibliography.rb', line 28

def delete(entry)
  if @entries.include?(entry.key)
    @entries.delete(entry.key)
    entry.bibliography = nil
    dirty!
  end
end

#dirty!Object



20
21
22
# File 'lib/bibsync/bibliography.rb', line 20

def dirty!
  @dirty = true
end

#dirty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/bibsync/bibliography.rb', line 16

def dirty?
  @dirty
end

#load(file, check = true) ⇒ Object



75
76
77
78
79
# File 'lib/bibsync/bibliography.rb', line 75

def load(file, check = true)
  parse(File.read(file)) if !check || (file && File.exists?(file))
  @file = file
  @dirty = false
end

#parse(text) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bibsync/bibliography.rb', line 81

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



43
44
45
46
47
# File 'lib/bibsync/bibliography.rb', line 43

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bibsync/bibliography.rb', line 49

def save(file = nil)
  if file
    @file = file
    dirty!
  end

  raise 'No filename given' unless @file
  if @dirty
    @save_hook.call(self) if @save_hook
    File.open("#{@file}.tmp", 'w') {|f| f.write(self) }
    File.rename("#{@file}.tmp", @file)
    @dirty = false
    true
  else
    false
  end
end

#to_sObject



95
96
97
98
# File 'lib/bibsync/bibliography.rb', line 95

def to_s
  "% #{DateTime.now}\n% Encoding: UTF8\n\n" <<
    @entries.values.join("\n") << "\n"
end