Sha256: 0996c8b08ddbbc1454928a8fca74894557b2a6c7e3ca33f90b56a531e2a08275

Contents?: true

Size: 999 Bytes

Versions: 3

Compression:

Stored size: 999 Bytes

Contents

require 'support/inflector'

module Support
  class Loader
    def initialize(pattern)
      @pattern = pattern
      @files = []
      @names = []
    end

    def reload?
      !last_scanned || !last_loaded || last_loaded < last_modified
    end

    def find
      scan!

      if reload?
        unload!
        load!
      end

      modules
    end

    private

    attr_reader :pattern, :files, :names, :modules
    attr_reader :last_scanned, :last_loaded

    def scan!
      @last_scanned = Time.now
      @files = Dir[pattern]
    end

    def last_modified
      files.map { |file| File.mtime(file) }.max || Time.at(0)
    end

    def unload!
      names.each { |name| Object.send(:remove_const, name) }
      names.clear
    end

    def load!
      @names = files.map do |file|
        load file
        Support::Inflector.modulize(File.basename(file, '.rb'))
      end

      @modules = names.map { |name| Object.const_get(name) }
      @last_loaded = last_scanned
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rail-0.1.1 lib/support/loader.rb
rail-0.1.0 lib/support/loader.rb
rail-0.0.8 lib/support/loader.rb