Sha256: 6890a580b2a923e7338e97b9b8420bdefcd9b30187f0e4b9ba5234d0336c3a13

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Writer
  class OverwritePrevention
    class << self
      def adjust(filename)
        name = fix_standard(filename)
        name ||= fix_no_extension(filename)
        name ||= fix_no_base(filename)
        name ||= fix_no_dots(filename)
        name
      end

      def fix_standard(name)
        if /.+\..+/ =~ name
          parts = name.split('.')
          basename = parts[0..-2].join('.')
          extension = parts.last

          fix_name(name, basename, extension)
        end
      end

      def fix_no_extension(name)
        if /\.$/ =~ name
          basename = name.gsub('.', '')

          fix_name(name, basename)
        end
      end

      def fix_no_base(name)
        if /^\./ =~ name
          fix_name(name)
        end
      end

      def fix_no_dots(name)
        unless name.include?('.')
          fix_name(name)
        end
      end

      private
      def fix_name(name, base = nil, ext = nil)
        to_fix = base || name
        count = 1

        while File.exists?(name)
          name = append_count(to_fix, count += 1)
          name << ".#{ext}" if base
        end

        name
      end

      def append_count(name, count)
        name = name.gsub(/--\d*$/, '')
        [name, "--", count].join
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
writer-0.1.1 lib/writer/overwrite_prevention.rb