Class: Bibtex::Bibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/bibtex/bibliography.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBibliography

Returns a new instance of Bibliography.



8
9
10
# File 'lib/bibtex/bibliography.rb', line 8

def initialize
  @entries = {}
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



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

def entries
  @entries
end

Instance Method Details

#<<(e) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/bibtex/bibliography.rb', line 12

def <<(e)
  if e.kind_of? Entry then
    @entries[e.key] = e
  else
    raise 'Cannot add non-entries to bibliography'
  end
end

#[](key) ⇒ Object



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

def [](key)
  @entries[key] or raise "No entry #{key}"
end

#mapObject

Transform the entries in some way and return a new bibliography



26
27
28
29
30
31
32
# File 'lib/bibtex/bibliography.rb', line 26

def map
  r = Bibliography.new
  @entries.each do |k, e|
    r << yield(e)
  end
  return r
end

#save(filename) ⇒ Object



34
35
36
37
38
# File 'lib/bibtex/bibliography.rb', line 34

def save(filename)
  f = File.new(filename, 'w')
  f.puts self.to_s
  f.close
end

#to_sObject



40
41
42
# File 'lib/bibtex/bibliography.rb', line 40

def to_s
  @entries.keys.sort.collect { |k| @entries[k].to_s }.join
end