Encapsulates a string representing a filename to provide convenient access to parts of it and sanitization. A Filename
instance is returned by ActiveStorage::Blob#filename
, and is comparable so it can be used for sorting.
- #
- A
- B
- E
- N
- S
- T
- W
- Comparable
Class Public methods
new(filename) Link
wrap(filename) Link
Returns a Filename
instance based on the given filename. If the filename is a Filename
, it is returned unmodified. If it is a String
, it is passed to ActiveStorage::Filename.new
.
Instance Public methods
<=>(other) Link
as_json(*) Link
base() Link
Returns the part of the filename preceding any extension.
ActiveStorage::Filename.new("racecar.jpg").base # => "racecar"
ActiveStorage::Filename.new("racecar").base # => "racecar"
ActiveStorage::Filename.new(".gitignore").base # => ".gitignore"
extension_with_delimiter() Link
Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at the beginning) with the dot that precedes it. If the filename has no extension, an empty string is returned.
ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
ActiveStorage::Filename.new("racecar").extension_with_delimiter # => ""
ActiveStorage::Filename.new(".gitignore").extension_with_delimiter # => ""
extension_without_delimiter() Link
Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at the beginning). If the filename has no extension, an empty string is returned.
ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
ActiveStorage::Filename.new("racecar").extension_without_delimiter # => ""
ActiveStorage::Filename.new(".gitignore").extension_without_delimiter # => ""
sanitized() Link
Returns the sanitized filename.
ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"
Characters considered unsafe for storage (e.g. , $, and the RTL override character) are replaced with a dash.