Module: AutoloadPath

Defined in:
lib/autoload_path.rb,
lib/autoload_path/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.each_file(source_path, *args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/autoload_path.rb', line 24

def each_file source_path , *args, &block
  options = args.extract_options!
  raise ArgumentError.new "Most supply a block" unless block_given?
  source_path = Pathname.new(source_path)
  prefix = options[:prefix]
  except = (options[:except] || []).map{|i| i.to_s}
  only = (options[:only] || []).map{|i| i.to_s}
  original_source = options[:original_source] ? Pathname.new(options[:original_source]) : source_path
  Dir.glob(source_path.join("*")).each do |path|
    path = Pathname.new path
    root = path
    parent = path.parent.basename
    actual = path.basename
    if path.directory? 
      each_file path, original_source: original_source , prefix: prefix, except: except, only: only, &block
    else
      if actual == source_path
        file_path = path.relative_path_from(original_source.parent)
        root = original_source.parent 
      else 
        file_path = path.relative_path_from(original_source.parent.parent)
        root = original_source.parent.parent 
      end
      extname = file_path.extname
      if prefix
        file_path = prefix + file_path.to_s 
      else
       file_path = File.join(root,file_path)
      end
      if only.length > 0 
        block.call(file_path, extname) if only.include?(file_path.to_s) && !except.include?(file_path.to_s)
      else
        block.call(file_path, extname) if (!except.include?(file_path.to_s) && !only.include?(file_path.to_s))
      end 
    end
  end          
end

.get_files(source_path, *args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/autoload_path.rb', line 16

def get_files source_path , *args
  file_paths = []
  each_file source_path , *args do |file_path|
    file_paths << file_path
  end
  file_paths
end

.load_path(source_path, *args) ⇒ Object



4
5
6
7
8
# File 'lib/autoload_path.rb', line 4

def load_path source_path , *args
  each_file source_path , *args do |file_path|
    load file_path.to_s
  end
end

.require_path(source_path, *args) ⇒ Object



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

def require_path source_path , *args
  each_file source_path , *args do |file_path,extname|
    require file_path.to_s.gsub(extname, "")
  end
end