Class: Fagin

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

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.find_children(parent, dir, recursive = false, withfile = false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fagin.rb', line 4

def self.find_children(
    parent,
    dir,
    recursive = false,
    withfile = false
)
    children = Hash.new
    path = Pathname.new(dir).expand_path
    return children if (!path.exist?)

    pattern = "\"^class +[^ ]+ *< *#{parent}\""
    cmd = "grep -EHors #{pattern} #{path}"
    cmd += "/*.rb" if (!recursive)

    %x(#{cmd}).each_line do |line|
        line.match(/^([^:]+\.rb).+class\s+(\S+)\s*</) do |match|
            file = match[1]
            next if (file.nil? || file.empty?)
            clas = match[2]
            next if (clas.nil? || clas.empty?)
            begin
                require_relative file
                child = clas.split("::").inject(Object) do |m, c|
                    m.const_get(c)
                end
                if (withfile)
                    children[clas] = [child, file]
                else
                    children[clas] = child
                end
            rescue NameError
                raise Error::UnknownChildClass.new(clas)
            end
        end
    end

    return children
end

.find_children_recursively(parent, dir) ⇒ Object



43
44
45
# File 'lib/fagin.rb', line 43

def self.find_children_recursively(parent, dir)
    return find_children(parent, dir, true)
end

.find_children_with_file(parent, dir) ⇒ Object



47
48
49
# File 'lib/fagin.rb', line 47

def self.find_children_with_file(parent, dir)
    return find_children(parent, dir, false, true)
end

.find_children_with_file_recursively(parent, dir) ⇒ Object



51
52
53
# File 'lib/fagin.rb', line 51

def self.find_children_with_file_recursively(parent, dir)
    return find_children(parent, dir, true, true)
end