Class: Bibliography

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(citations = nil) ⇒ Bibliography

Returns a new instance of Bibliography.



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

def initialize(citations=nil)
  if citations
    @citations = citations
  end
end

Instance Attribute Details

#citationsObject

Returns the value of attribute citations.



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

def citations
  @citations
end

Class Method Details

.from_yaml(file_or_string) ⇒ Object

if file, loads it



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bibliography.rb', line 61

def self.from_yaml(file_or_string)
  hash = 
    if File.exist? file_or_string
      YAML.load_file(file_or_string)
    else
      YAML.load(file_or_string)
    end
  # we were given a nonexistent file and the yaml is not a hash
  # in this case we need to create an empty bib object
  unless hash.is_a? Hash
    hash = {}
  end
  citations = hash.map do |id,vals|
    vals['ident'] = id 
    bibtype = vals['bibtype']
    klass = 
      if vals.key? 'pmid'
        PubMed
      else
        Citation.const_get(bibtype.capitalize)
      end
      #when 'article'
      #  else
      #    Citation::Article
      #  end
      #when 'book'
      #  Citation::Book
      #else
      #  abort "Unrecognized bibtype!"
      #end
    vals['bibtype'] = bibtype.to_sym
    cit = klass.new(vals)
    #if cit.authors =~ /Paris/
    #  p cit.authors
    #  abort
    #end
    #if cit.authors.is_a? Array
    #  cit.authors = cit.author_strings_to_objects
    #end
    cit
  end
  bib = Bibliography.new(citations)
end

Instance Method Details

#add(*citations) ⇒ Object

adds a list of citations. It will ONLY add citations whose identifiers do not already exist. Citations which already have a duplicate identifier will be returned. nil is returned if no citation objects have clashing id’s



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bibliography.rb', line 43

def add(*citations)
  clashing = []
  hsh = to_hash
  citations.each do |cit|
    if hsh.key? cit.ident
      clashing << cit
    else
      @citations.push(cit)
    end
  end
  if clashing.size > 0
    clashing
  else
    nil
  end
end

#not_uniq(other) ⇒ Object

returns an array of citations from other that are not uniq compared self



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bibliography.rb', line 15

def not_uniq(other)
  scit = self.citations
  ocit = other.citations
  pass_id = not_uniq_by(scit, ocit, :ident)
  passed = [scit, ocit].map do |ar|
    ar.select {|v| v.respond_to? :pmid}
  end
  passed.push( *(not_uniq_by(passed[0], passed[1], :ident)) )
  passed.uniq!
  passed
end

#not_uniq_by(cits1, cits2, att) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bibliography.rb', line 27

def not_uniq_by(cits1, cits2, att)
  self_by_att = cits1.group_by(&att)
  other_by_att = cits2.group_by(&att)
  not_un = [] 
  other_by_att.each do |k,v| 
    if self_by_att.key? k
      not_un.push( *v )
    end
  end
  not_un 
end

#select_by_id!(ids) ⇒ Object

selects as internal citations only those matching the array of idents returns the citations



107
108
109
110
111
112
113
114
115
# File 'lib/bibliography.rb', line 107

def select_by_id!(ids)
  # should mimic index_by
  hash = @citations.group_by(&:ident) ; hash.each {|k,v| hash[k] = v.last }
  new_cits = ids.map do |id|
    unless hash.key? id ; abort "Cannot find '#{id}' in citations!" end
    hash[id]
  end
  @citations = new_cits
end

#to_hashObject

hashes by ident



118
119
120
121
122
123
124
125
126
# File 'lib/bibliography.rb', line 118

def to_hash
  hsh = {}
  @citations.each do |cit|
     cthash = cit.to_hash
     cthash.delete('ident')
     hsh[cit.ident] = cthash
  end
  hsh
end

#to_yaml(file = nil) ⇒ Object

if given a file, writes to the file, otherwise returns the string



129
130
131
132
133
134
135
136
# File 'lib/bibliography.rb', line 129

def to_yaml(file=nil)
  hsh = to_hash
  string = hsh.to_yaml
  if file
    File.open(file, 'w') {|v| v.print string }
  end
  string
end

#write(format_obj) ⇒ Object

a format_obj can respond to the call obj.format(citation, format_type) and :header and :footer



140
141
142
# File 'lib/bibliography.rb', line 140

def write(format_obj)
  format_obj.header + format_obj.format(@citations) + format_obj.footer
end