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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/vcseif/utils/auxloader.rb', line 38
def self.loadFacility(subdir, class_name, facility=nil, method_existence=nil)
%{
Work the Ruby machinery to require in a Ruby file such that
a subsequent Kernel.const_get(class_name) works successfully.
The subdir parm is relative to ENV['VCS_APP_ROOT'].
The optional facility parm can "name" a specific file the class
must exist in.
Caller can also specify a string or an array of strings with method
names that must exist in the loaded class.
}
fq_facility_dir = "#{ENV['VCS_APP_ROOT']}/#{subdir}"
if not File.exists?(fq_facility_dir)
raise StandardError, 'target subdir: %s not found' % [fq_facility_dir]
end
if not File.directory?(fq_facility_dir)
raise StandardError, 'target path %s is not a directory' % [fq_facility_dir]
end
if facility.nil?
target_dir = fq_facility_dir
rbfiles = Dir.entries(target_dir).select {|fn| self.is_qualified?(target_dir, fn)}
facility_names = rbfiles.collect {|fn| fn[0...-3]} else
facility_path = '%s/%s.rb' % [fq_facility_dir, facility]
if not File.file?(facility_path)
raise StandardError, 'No such %s module: %s found' % [subdir, facility_path]
end
facility_names = [facility]
end
target_classes_found = 0
tc_facility = {} facility_names.each do |facility_name|
facility_file = '%s/%s.rb' % [fq_facility_dir, facility_name]
File.open(facility_file) do |ff|
target_class_defs = ff.readlines.select {|line| line =~ /^class #{class_name}\s*/}
if target_class_defs.length > 0
tc_facility[class_name] = "%s/%s" % [subdir, facility_name]
target_classes_found +=1
end
end
end
if target_classes_found < 1
problem = "No class: '%s' defined in any file located in the '%s' directory"
raise StandardError, problem % [class_name, fq_facility_dir]
end
if target_classes_found > 1
problem = "%s class: '%s' defined in more than one file located in the '%s' directory"
raise StandardError, problem % [subdir, class_name, fq_facility_dir]
end
begin
require "#{tc_facility[class_name]}"
rescue Exception => ex
$stderr.write("Auxloader detected exception on require of #{tc_facility[class_name]}:\n")
$stderr.write(" #{ex.message}\n")
$stderr.flush()
raise
end
begin
@@facility_class = Kernel.const_get(class_name)
rescue Exception => ex
$stderr.write("Caught exception loading #{class_name}: #{ex.message}\n")
raise
end
if method_existence != nil
if method_existence.class.name == 'String'
method_existence = method_existence.split(',').collect {|mn| mn.strip()}
elsif method_existence.class.name != 'Array'
raise Exception, "method_existence parm must be a String or an Array of Strings"
end
for method_name in method_existence do
method = @@facility_class.instance_methods.select {|m| m == method_name.to_sym}
if method == nil or method == false
puts "Unable to validate that the %s class has a %s method" % [class_name, method_name]
end
end
end
@@facility[class_name] = @@facility_class
return @@facility[class_name]
end
|