Class: BitClust::RefsDatabase

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

Overview

Corresponds to db-x.y.z/refs file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRefsDatabase

Returns a new instance of RefsDatabase.


31
32
33
# File 'lib/bitclust/refsdatabase.rb', line 31

def initialize
  @h = {}
end

Class Method Details

.load(src) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitclust/refsdatabase.rb', line 12

def self.load(src)
  if src.respond_to?(:to_str)
    buf = fopen(src.to_str, 'r:UTF-8'){|f| f.read}
  elsif src.respond_to?(:to_io)
    buf = src.to_io.read
  else
    buf = src.read
  end

  refs = self.new
  buf.each_line{|l|
    if /((?:\\,|[^,])+),((?:\\,|[^,])+),((?:\\,|[^,])+),((?:\\,|[^,])+)\n/ =~ l
      type, id, linkid, desc = [$1, $2, $3, $4].map{|e| e.gsub(/\\(.)/){|s| $1 == ',' ? ',' : s } }
      refs[type, id, linkid] = desc
    end
  }
  refs
end

Instance Method Details

#[](type, mid, linkid) ⇒ Object


39
40
41
# File 'lib/bitclust/refsdatabase.rb', line 39

def [](type, mid, linkid)
  @h[[type.to_s, mid, linkid]]
end

#[]=(type, mid, linkid, desc) ⇒ Object


35
36
37
# File 'lib/bitclust/refsdatabase.rb', line 35

def []=(type, mid, linkid, desc)
  @h[[type.to_s, mid, linkid]] = desc
end

#extract(entry) ⇒ Object


59
60
61
62
63
64
65
66
67
# File 'lib/bitclust/refsdatabase.rb', line 59

def extract(entry)
  entry.source.each_line{|l|
    if /\A={1,6}\[a:(\w+)\] *(.*)/ =~ l
      entry.labels.each{|name|
        self[entry.class.type_id, name, $1] = $2
      }
    end
  }
end

#save(s) ⇒ Object


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

def save(s)
  if s.respond_to?(:to_str)
    path = s.to_str
    io = fopen(path, 'w:UTF-8')
  elsif s.respond_to?(:to_io)
    io = s.to_io
  else
    io = s
  end

  @h.each{|k, v|
    io.write(  [k, v].flatten.map{|e| e.gsub(/,/, '\\,') }.join(',') + "\n" )
  }
  io.close
end