Class: Quir::Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, dir) ⇒ Loader

Returns a new instance of Loader.



7
8
9
10
# File 'lib/quir/loader.rb', line 7

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

Instance Attribute Details

#bindingObject (readonly)

Returns the value of attribute binding.



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

def binding
  @binding
end

#dirObject (readonly)

Returns the value of attribute dir.



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

def dir
  @dir
end

#moduleObject (readonly)

Returns the value of attribute module.



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

def module
  @module
end

Instance Method Details

#append_directory(d) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/quir/loader.rb', line 24

def append_directory(d)
  return if ::File.exist?("#{dir}/#{d}.rb")
  name = d.split(/\//)[-1].pascalize
  return if self.module.const_defined?(name, false)
  m = ::Module.new
  self.module.const_set name, m
  m.name # fix module's name
  loader = ::Quir::Loader.new(m, d)
  loader.run
end

#append_ruby_file(f) ⇒ Object



35
36
37
38
# File 'lib/quir/loader.rb', line 35

def append_ruby_file(f)
  name = f.split(/\//)[-1].sub(/\.rb$/, '').pascalize
  self.module.autoload name, f unless self.module.autoload?(name)
end

#runObject



14
15
16
17
18
19
20
21
22
# File 'lib/quir/loader.rb', line 14

def run
  ::Dir.glob("#{dir}/*") do |f|
    if ::File.directory?(f)
      append_directory f
    elsif /\.rb$/ =~ f
      append_ruby_file f
    end
  end
end