Class: ODDB::NarcoticHandler

Inherits:
Rpdf2txt::ColumnHandler
  • Object
show all
Defined in:
src/plugin/narcotic.rb

Instance Method Summary (collapse)

Constructor Details

- (NarcoticHandler) initialize(callback)

A new instance of NarcoticHandler



14
15
16
17
# File 'src/plugin/narcotic.rb', line 14

def initialize callback
  @callback = callback
  super()
end

Instance Method Details

- (Object) send_page



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'src/plugin/narcotic.rb', line 18

def send_page
  previous = nil
  @lines.each do |line|
    name, casrn, third = line
    casrn = casrn.to_s.strip
    if casrn.empty? && (found = name.slice!(/\s+\d+-\d+-\d+\s*$/u))
      line[1] = found
    elsif /\d+-\d+-\d/u.match(third)
      line[0] = line[0].to_s + ' ' + casrn
      line.delete_at(1)
    end
    line.each do |column| column.strip! if column end
  end
  @lines.delete_if do |line|
    name = line.first.to_s
    name.empty? || /\bad\s*us\.?\s*(vet)?/iu.match(name)
  end
  @lines.collect! do |line|
    data = line[0,1]
    data.push(line.find do |item| /^\d+-\d+-\d+/u.match item end)
    pcode = line.find do |item| /^\d{7}$/u.match item end
    pcode = pcode.to_i.to_s if pcode
    data.push pcode
    data.push(line.find do |item| /^76/u.match item end)
    data.push(line.find do |item|
      /\w{2,}/u.match(item.to_s) && !data.include?(item)
    end)
    data.push(line.find do |item| /^\s*[a-d]\s*$/u.match item end)
    compact = data[1..-1].compact
    if(previous && compact.size == 1)
      previous[4] = previous[4].to_s + ' ' + compact.shift
    end
    if(compact.empty?)
=begin ## it seems impossible to get this right: this code is supposed to
    # append the following line to the first column, but since it's unclear
    # when this is desired and when it is not, I've decided to disable it.
      if previous && !/l.gende/i.match(data[0]) \
        && !previous[0].include?(data[0])
        previous[0] = previous[0].to_s + ' ' + line[0]
        previous = nil
      end
=end


    else
      previous = data
    end
    data
  end
  @lines.each do |data|
    @callback.call data
  end
end