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.159 lib/trackler/file_bundle.rb
trackler-2.2.1.158 lib/trackler/file_bundle.rb
trackler-2.2.1.157 lib/trackler/file_bundle.rb
trackler-2.2.1.156 lib/trackler/file_bundle.rb
trackler-2.2.1.155 lib/trackler/file_bundle.rb
trackler-2.2.1.154 lib/trackler/file_bundle.rb
trackler-2.2.1.153 lib/trackler/file_bundle.rb
trackler-2.2.1.152 lib/trackler/file_bundle.rb
trackler-2.2.1.151 lib/trackler/file_bundle.rb
trackler-2.2.1.150 lib/trackler/file_bundle.rb
trackler-2.2.1.149 lib/trackler/file_bundle.rb
trackler-2.2.1.148 lib/trackler/file_bundle.rb
trackler-2.2.1.147 lib/trackler/file_bundle.rb
trackler-2.2.1.146 lib/trackler/file_bundle.rb
trackler-2.2.1.145 lib/trackler/file_bundle.rb
trackler-2.2.1.144 lib/trackler/file_bundle.rb
trackler-2.2.1.143 lib/trackler/file_bundle.rb
trackler-2.2.1.142 lib/trackler/file_bundle.rb
trackler-2.2.1.141 lib/trackler/file_bundle.rb
trackler-2.2.1.140 lib/trackler/file_bundle.rb