Sha256: 34daa0160c0e8abc0ee0e5ace63468e9f80d0b31cfbda24de41dac6b4caad994

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 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 = "*"
        glob(pattern).select(&:directory?).sort
      end

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

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

      def relative_parent_from root
        relative_path_from(root).parent
      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

2 entries across 2 versions & 1 rubygems

Version Path
refinements-7.6.0 lib/refinements/pathnames.rb
refinements-7.5.0 lib/refinements/pathnames.rb