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.



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

def initialize
  @h = {}
end

Class Method Details

.load(src) ⇒ Object



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

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



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

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

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



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

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

#extract(entry) ⇒ Object



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

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



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

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