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
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

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#format_hookObject

Returns the value of attribute format_hook.



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

def format_hook
  @format_hook
end

#transform_hookObject

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

#clearObject



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

Returns:

  • (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_sObject



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