Sha256: bbc97b1b7a9f0191ddbb2ca0c71a84cdffb4d2bedc541c48c998bc7c50e9efdc

Contents?: true

Size: 1.16 KB

Versions: 373

Compression:

Stored size: 1.16 KB

Contents

require 'zip'

module Trackler
  # A FileBundle is a collection of files from within an exercise directory
  # It contains all the files that will be provided by the `exercism fetch` command
  # EXCEPT for those whose names match any of the ignore patterns.
  class FileBundle
    def initialize(base_directory, ignore_patterns = [])
      @base_directory = base_directory
      @ignore_patterns = ignore_patterns
    end

    def zip
      Zip::OutputStream.write_buffer do |io|
        paths.each do |path|
          io.put_next_entry(path.relative_path_from(base_directory))
          io.print IO.read(path)
        end
        yield io if block_given?
      end
    end

    def paths
      all_files_below(base_directory).reject { |file| ignored? file }.sort
    end

    private

    attr_reader :base_directory, :ignore_patterns

    def all_files_below(dir)
      Pathname.glob("#{dir}/**/*", File::FNM_DOTMATCH)
    end

    def ignored?(file)
      ignored_by_name?(file) || ignored_by_type?(file)
    end

    def ignored_by_name?(file)
      ignore_patterns.any? { |pattern| file.to_s =~ pattern }
    end

    def ignored_by_type?(file)
      file.directory?
    end
  end
end

Version data entries

373 entries across 373 versions & 1 rubygems

Version Path
trackler-2.2.1.180 lib/trackler/file_bundle.rb
trackler-2.2.1.179 lib/trackler/file_bundle.rb
trackler-2.2.1.178 lib/trackler/file_bundle.rb
trackler-2.2.1.177 lib/trackler/file_bundle.rb
trackler-2.2.1.176 lib/trackler/file_bundle.rb
trackler-2.2.1.175 lib/trackler/file_bundle.rb
trackler-2.2.1.174 lib/trackler/file_bundle.rb
trackler-2.2.1.173 lib/trackler/file_bundle.rb
trackler-2.2.1.172 lib/trackler/file_bundle.rb
trackler-2.2.1.171 lib/trackler/file_bundle.rb
trackler-2.2.1.170 lib/trackler/file_bundle.rb
trackler-2.2.1.169 lib/trackler/file_bundle.rb
trackler-2.2.1.167 lib/trackler/file_bundle.rb
trackler-2.2.1.166 lib/trackler/file_bundle.rb
trackler-2.2.1.165 lib/trackler/file_bundle.rb
trackler-2.2.1.164 lib/trackler/file_bundle.rb
trackler-2.2.1.163 lib/trackler/file_bundle.rb
trackler-2.2.1.162 lib/trackler/file_bundle.rb
trackler-2.2.1.161 lib/trackler/file_bundle.rb
trackler-2.2.1.160 lib/trackler/file_bundle.rb