Sha256: 859af4df3d4bbe01ad8b0a98f3b05861e4285b70ecfe5ef4c7519abcce140191

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

module Cycromatic
  class FileEnumerator
    attr_reader :paths

    def initialize(paths:)
      @paths = paths
    end

    def each(&block)
      if block_given?
        paths.each do |path|
          case
          when path.file?
            yield path
          when path.directory?
            enumerate_files_in_dir(path, &block)
          end
        end
      else
        self.enum_for :each
      end
    end

    private

    def enumerate_files_in_dir(path, &block)
      if path.basename.to_s =~ /\A\.[^\.]+/
        # skip hidden paths
        return
      end

      case
      when path.directory?
        path.children.each do |child|
          enumerate_files_in_dir child, &block
        end
      when path.file?
        if is_ruby_file?(path)
          yield path
        end
      end
    end

    def is_ruby_file?(path)
      case
      when path.extname == ".rb"
        true
      when path.extname == ".gemspec"
        true
      when path.basename.to_s == "Rakefile"
        true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cycromatic-0.1.3 lib/cycromatic/file_enumerator.rb
cycromatic-0.1.2 lib/cycromatic/file_enumerator.rb
cycromatic-0.1.1 lib/cycromatic/file_enumerator.rb
cycromatic-0.1.0 lib/cycromatic/file_enumerator.rb