Sha256: 81229c03e597197d05d8b4c8fd93fa84d223cc5dd29796ff75b2fcbdbb359284

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 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)
    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 #{method} not found")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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