Sha256: 70842d3ebe9d3284ab21d45c3e6ec753307034612eb21eb7b0ceb7a46900a1ba

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

module CarrierWave
  module Utilities
    module FileName

      ##
      # Returns the part of the filename before the extension. So if a file is called 'test.jpeg'
      # this would return 'test'
      #
      # === Returns
      #
      # [String] the first part of the filename
      #
      def basename
        split_extension(filename)[0] if filename
      end

      ##
      # Returns the file extension
      #
      # === Returns
      #
      # [String] extension of file or "" if the file has no extension
      #
      def extension
        split_extension(filename)[1] if filename
      end

    private

      def split_extension(filename)
        # regular expressions to try for identifying extensions
        extension_matchers = [
          /\A(.+)\.(tar\.([glx]?z|bz2))\z/, # matches "something.tar.gz"
          /\A(.+)\.([^\.]+)\z/ # matches "something.jpg"
        ]

        extension_matchers.each do |regexp|
          if filename =~ regexp
            return $1, $2
          end
        end

        [filename, ""] # In case we weren't able to split the extension
      end
    end # FileName
  end # Utilities
end # CarrierWave

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
carrierwave-3.1.0.beta lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.7 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.6 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.5 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.4 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.3 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.2 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.1 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.0 lib/carrierwave/utilities/file_name.rb
carrierwave-3.0.0.rc lib/carrierwave/utilities/file_name.rb