Class: Quir::Loader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, dir) ⇒ Loader

Returns a new instance of Loader.



5
6
7
8
# File 'lib/quir/loader.rb', line 5

def initialize(mod, dir)
  @module = mod
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



16
17
18
# File 'lib/quir/loader.rb', line 16

def dir
  @dir
end

#moduleObject (readonly)

Returns the value of attribute module.



16
17
18
# File 'lib/quir/loader.rb', line 16

def module
  @module
end

Class Method Details

.from_binding(binding) ⇒ Object



10
11
12
13
14
# File 'lib/quir/loader.rb', line 10

def self.from_binding(binding)
  mod = binding.eval('self')
  dir = binding.eval('__FILE__').sub(/\.rb$/, '')
  self.new(mod, dir)
end

Instance Method Details

#autoload!Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/quir/loader.rb', line 18

def autoload!
  ::Dir.glob("#{dir}/*", ::File::FNM_DOTMATCH) do |f|
    next if /\/\.{1,2}$/ =~ f
    if ::File.directory?(f)
      autoload_directory f
    elsif /\.rb$/ =~ f
      autoload_ruby_file f
    end
  end
end

#autoload_directory(d) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/quir/loader.rb', line 29

def autoload_directory(d)
  return if ::File.exist?("#{d}.rb")
  name = d.split(/\//)[-1].pascalize
  return if self.module.const_defined?(name, false)
  m = define_directory_module(name, d)
  loader = self.class.new(m, d)
  loader.autoload!
end

#autoload_ruby_file(f) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/quir/loader.rb', line 45

def autoload_ruby_file(f)
  name = f.split(/\//)[-1].sub(/\.rb$/, '')
  /^([#.])?(.+?)([?!])?$/ =~ name
  s1 = {'#' => 'I', '.' => 'C'}[$1]
  s2 = {'?' => 'P', '!' => 'D'}[$3]
  name = (s1 || s2 ? "#{s1}#{s2}_" : '') + $2.pascalize
  self.module.autoload name, f unless self.module.autoload?(name)
end

#define_directory_module(name, dir) ⇒ Object



38
39
40
41
42
43
# File 'lib/quir/loader.rb', line 38

def define_directory_module(name, dir)
  m = ::Module.new
  self.module.const_set name, m
  m.name # fix module's name
  m
end