Class: AuxLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/vcseif/utils/auxloader.rb

Overview

auxload - Auxiliary functionality loader

  Contains classes to load auxiliary (plugin and extension) classes
  or the *_connection classes.
A specification for functionality looks like:
                   class_name.method_name
  or
     facility_name.class_name.method_name

Direct Known Subclasses

ClassLoader, ExtensionLoader, PluginLoader

Constant Summary collapse

@@facility_class_name =
""
@@facility_class =
nil
@@facility =

key by @@facility_class_name, value is @@facility_class object

{}

Class Method Summary collapse

Class Method Details

.getFacility(facility_type, facility_class_name, facility_name = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vcseif/utils/auxloader.rb', line 21

def self.getFacility(facility_type, facility_class_name, facility_name=nil)
    """
        Given a facility_class_name, look into @@facility for a key match.
        If found return the corresponding value, which must be the
        class object for the facility_class.
    """
    if not @@facility.include?(facility_class_name)
        loadFacility(facility_type, facility_class_name, facility_name)
    end
    return @@facility[facility_class_name] || nil
end

.is_qualified?(target_dir, fn) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/vcseif/utils/auxloader.rb', line 33

def self.is_qualified?(target_dir, fn)
    qualified = fn.end_with?('.rb') and File.file?('%s/%s' % [target_dir, fn])
    return qualified
end

.loadFacility(subdir, class_name, facility = nil, method_existence = nil) ⇒ Object



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}"  # fully qualified path to subdir
##
##    puts "  loadFacility fq_facility_dir value: |%s|" % fq_facility_dir
##    $stdout.flush()
##

    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]}  # squeeze off the '.rb' portion of the name
    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
##
##    puts "  facility_names: #{facility_names.inspect}"
##    $stdout.flush()
##

    target_classes_found = 0
    tc_facility = {}   # target class facility lookup dict
    # look in all facilities, record the facility name if the facility_class_name is defined in there
    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