Sha256: 6ff9d180dd523da8da1a5b2449ab3078d487261ac42741cbc836677c31407164

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require "pathname"

module Refinements
  module Pathnames
    refine Kernel do
      def Pathname object
        return super(String(object)) unless object

        super
      end
    end

    refine Pathname do
      def name
        basename extname
      end

      def copy to
        destination = to.directory? ? to.join(basename) : to
        read.then { |content| destination.write content }
        self
      end

      def directories pattern = "*", flag: File::FNM_SYSCASE
        glob(pattern, flag).select(&:directory?).sort
      end

      def extensions
        basename.to_s.split(/(?=\.)+/).tap(&:shift)
      end

      def files pattern = "*", flag: File::FNM_SYSCASE
        glob(pattern, flag).select(&:file?).sort
      end

      def gsub pattern, replacement
        self.class.new to_s.gsub(pattern, replacement)
      end

      def relative_parent root_dir
        relative_path_from(root_dir).parent
      end

      def relative_parent_from root_dir
        warn "[DEPRECATION]: Pathname#relative_parent_from is deprecated, " \
             "use Pathname#relative_parent instead."
        relative_parent root_dir
      end

      def make_ancestors
        dirname.mkpath
        self
      end

      def rewrite
        read.then { |content| write yield(content) if block_given? }
        self
      end

      def touch at: Time.now
        exist? ? utime(at, at) : write("")
        self
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
refinements-7.11.0 lib/refinements/pathnames.rb
refinements-7.10.0 lib/refinements/pathnames.rb
refinements-7.9.0 lib/refinements/pathnames.rb
refinements-7.8.0 lib/refinements/pathnames.rb