Sha256: 33eb2ea4a89ff29844e69cf10f3ca677ae25ce31d295033e3f64d6d41f362c12

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module LogCabin
  ##
  # Define a collection of modules
  class Collection
    attr_reader :load_path

    def initialize(params = {})
      @load_path = params[:load_path] || fail('Load path must be provided')
      @load_path = [@load_path] if @load_path.is_a? String
      @modules = {}
    end

    ##
    # Method for finding modules to load
    def find(name)
      return @modules[name] if @modules[name]
      file = find_file(name)
      require file
      class_name = parse_class_name(name)
      @modules[name] = LogCabin::Modules.const_get(class_name)
    rescue LoadError
      raise("Error while loading #{name}")
    end

    private

    ##
    # Convert file name to class name
    # Borrowed with love from Homebrew: http://git.io/vEoDg
    def parse_class_name(name)
      class_name = name.to_s.capitalize
      class_name.gsub(/[-_.\s]([a-zA-Z0-9])/) { Regexp.last_match[1].upcase }
    end

    ##
    # Check module path for module
    def find_file(name)
      @load_path.each do |dir|
        file_path = File.join(dir, "#{name}.rb")
        return file_path if File.exist? file_path
      end
      fail("Module #{name} not found")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logcabin-0.0.3 lib/logcabin/collection.rb