Class: RD::RBLFile

Inherits:
Object
  • Object
show all
Includes:
SearchFile
Defined in:
lib/rd/rbl-file.rb

Constant Summary collapse

SUFFIX =
"rbl"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchFile

#search_file

Constructor Details

#initialize(filename) ⇒ RBLFile

Returns a new instance of RBLFile.



11
12
13
14
# File 'lib/rd/rbl-file.rb', line 11

def initialize(filename)
  @filename = RBLFile.basename(filename)
  @labels = []
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/rd/rbl-file.rb', line 9

def filename
  @filename
end

#labelsObject (readonly)

Returns the value of attribute labels.



8
9
10
# File 'lib/rd/rbl-file.rb', line 8

def labels
  @labels
end

Class Method Details

.basename(path) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rd/rbl-file.rb', line 26

def RBLFile.basename(path)
  if /\.(rd|rb)$/ === path
	$`
  else
	path
  end
end

.create_rbl_file(filename, resolver) ⇒ Object



16
17
18
19
20
# File 'lib/rd/rbl-file.rb', line 16

def RBLFile.create_rbl_file(filename, resolver)
  file = File.open(RBLFile.rbl_file_path(filename), "w")
  file.print(RBLFile.labels_to_string(resolver))
  file.close
end

.labels_to_string(resolver) ⇒ Object



34
35
36
37
38
# File 'lib/rd/rbl-file.rb', line 34

def RBLFile.labels_to_string(resolver)
  (resolver.collect do |i|
	 i.to_label + " => " + resolver.get_anchor(i)
   end).join("\n")
end

.rbl_file_path(filename) ⇒ Object



22
23
24
# File 'lib/rd/rbl-file.rb', line 22

def RBLFile.rbl_file_path(filename)
  basename(filename) + "." + SUFFIX
end

Instance Method Details

#load_rbl_file(search_paths) ⇒ Object



40
41
42
43
44
45
# File 'lib/rd/rbl-file.rb', line 40

def load_rbl_file(search_paths)
  f = search_file(@filename, search_paths, [SUFFIX])
  raise "RBLFile not found." unless f
  src = File.readlines(f).join("")
  @labels = string_to_labels(src)
end

#parse_line(src) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rd/rbl-file.rb', line 55

def parse_line(src)
  col = src.rindex("=>")
  raise "RBL file parse error." unless col
  label = src[0 .. col - 1].strip
  anchor = src[col + 2 .. -1].strip
  [label, anchor]
end

#refer(label) ⇒ Object



63
64
65
66
67
# File 'lib/rd/rbl-file.rb', line 63

def refer(label)
  label = @labels.find{|i| i[0] == label}
  return nil unless label
  label[1]
end

#string_to_labels(src) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rd/rbl-file.rb', line 47

def string_to_labels(src)
  labels = []
  src.each_line do |i|
	labels << parse_line(i)
  end
  labels
end