Sha256: 7dca19279e3beb5d5b7c9ab58d577e5a27cfdae709a8790661427bff672eee7a

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

module TarvitHelpers

  class RecursiveLoader
    attr_reader :method

    def initialize(method=:require)
      raise "Method #{method} is invalid, must be :require or :load" unless [ :require, :load ].include?(method)
      @method = method
    end

    def load_modules(dir, priorities=[])
      load_ruby_files(dir)
      dirs = global_dirs(dir)
      (priorities.select{|p| dirs.include?(p)} + dirs ).each do |subdir|
        load_modules_in dir.join(subdir)
      end
    end

    def load_modules_in(dir)
      load_ruby_files(dir)
      valid_directories(dir).each do |subdir|
        load_modules_in dir.join(subdir)
      end
    end

    module Context
      def load_modules(dir, priorities=[], method=:require)
        RecursiveLoader.new(method).load_modules(dir, priorities)
      end
    end

    extend Context

    private

    def global_dirs(dir)
      (valid_directories(dir)).uniq
    end

    def valid_directories(dir)
      Dir.open(dir).entries.select do |entry|
        !%w{ . .. }.include?(entry) && File.directory?(dir.join(entry))
      end
    end

    def load_ruby_files(dir)
      ruby_files(dir).each do |rb|
        send(method, dir.join(rb))
      end
    end

    def ruby_files(dir)
      Dir.open(dir).entries.select do |entry|
        entry.ends_with? '.rb'
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tarvit-helpers-0.0.22 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.21 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.20 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.19 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.18 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.17 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.16 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.15 lib/tarvit-helpers/modules/recursive_loader.rb
tarvit-helpers-0.0.14 lib/tarvit-helpers/modules/recursive_loader.rb