Module: Cargobull::Initialize

Defined in:
lib/cargobull/initialize.rb

Class Method Summary collapse

Class Method Details

.dir(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cargobull/initialize.rb', line 10

def self.dir(*args)
  sanitized_args = args.map{ |d| sanitize_file_name(d).sub(/\/$/, '') }.
    select{ |d| File.directory?(d) }

  sanitized_args.each do |dir|
    ruby_files = Dir["#{dir}/**/*.rb"]
    @file_map = ruby_files.each do |f|
      full_klass = f.sub(/^#{dir}\/?/, '').sub(/\.rb$/, '').camelize
      register_file(full_klass, f)
    end
  end
end

.file(klass_str, file_name) ⇒ Object



23
24
25
26
27
# File 'lib/cargobull/initialize.rb', line 23

def self.file(klass_str, file_name)
  file_name = sanitize_file_name(file_name)
  return unless File.file?(file_name)
  register_file(klass_str, file_name)
end

.init_allObject



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

def self.init_all
  @file_map.each{ |file| require file }
end

.register_file(full_klass, fname) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cargobull/initialize.rb', line 29

def self.register_file(full_klass, fname)
  *mods, klass_name = full_klass.split('::')
  mod = mods.reduce(Object) do |acc, mod_str|
    unless acc.const_defined?(mod_str)
      acc.const_set(mod_str, Module.new)
    end
    next acc.const_get(mod_str)
  end

  mod.autoload(klass_name, fname)
  @file_map << fname
  Service.register(full_klass)
end

.sanitize_file_name(file_name) ⇒ Object



6
7
8
# File 'lib/cargobull/initialize.rb', line 6

def self.sanitize_file_name(file_name)
  "./#{File.basename(file_name)}"
end