Sha256: ea3ea71fe04a5a98bc5104d510c1ed50694b51249ee9f467845cf792c6746d24

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Coco

  # I retrieve the .rb files from a list of directories.
  class SourceLister
  
    # config - Hash.
    def initialize(config)
      @exclude_files = config[:excludes]
      dirs = config[:directories]
      unless dirs.is_a? Array
        @folders = [dirs]
      else
        @folders = dirs
      end
      @folders.each do |folder|
        unless File.directory?(folder)
          raise ArgumentError, "Not a folder: #{folder}"
        end
      end
      @list = []
    end
    
    # Returns Array of String, that is a list of all `.rb` files from
    # the directories found in configuration.
    def list
      look_for_sources
      @list.map! {|file| File.expand_path(file) }
      exclude_files_user_dont_want
      @list
    end
    
    private
    
    def look_for_sources
      @folders.each {|folder| @list += Helpers.rb_files_from folder }
    end
    
    def exclude_files_user_dont_want
      return if @exclude_files.nil?
      
      @exclude_files.each do |filename|
        full_path = File.expand_path(filename)
        if File.file?(full_path)
          @list.delete full_path 
        elsif File.directory?(full_path)
          exclude_all_from_dir full_path
        end
      end
    end
    
    def exclude_all_from_dir(full_path)
      Helpers.rb_files_from(full_path).each do |file|
        @list.delete File.expand_path(file)
      end
    end
    
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coco-0.13.0 lib/coco/lister/source_lister.rb