Class: BibTexFile::Entry

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

Constant Summary collapse

FIELDS =
%w(pmid title author journal pages number volume year abstract)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, info) ⇒ Entry

Returns a new instance of Entry.



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

def initialize(name, type, info)
  @name = name
  @type = type
  @info = info
  @fields = info.keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rbbt/sources/bibtex.rb', line 19

def method_missing(name, *args)
  if name.to_s =~ /(.*)=$/
    if (FIELDS + @fields).include?($1.to_s)
      return @info[$1.to_s] = args[0].chomp
    else
      raise "No field named '#{ $1 }'"
    end
  else
    if @fields.include?(name.to_s)
      return @info[name.to_s]
    else
      raise "No field named '#{ name }'"
    end
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



11
12
13
# File 'lib/rbbt/sources/bibtex.rb', line 11

def fields
  @fields
end

#infoObject (readonly)

Returns the value of attribute info.



11
12
13
# File 'lib/rbbt/sources/bibtex.rb', line 11

def info
  @info
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/rbbt/sources/bibtex.rb', line 11

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/rbbt/sources/bibtex.rb', line 11

def type
  @type
end

Instance Method Details

#to_sObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbbt/sources/bibtex.rb', line 35

def to_s
  str = "@#{type}{#{name},\n"

  FIELDS.each do |field|
    next if field.nil?
    str += "    #{field} = {#{@info[field]}},\n"
  end

  (fields - FIELDS).sort.each do |field|
    str += "    #{field} = {#{@info[field]}},\n"
  end

  str += "}"

  str
end