struct
ActiveStorage::Filename
- ActiveStorage::Filename
- Struct
- Value
- Object
Overview
Wraps a string representing a filename to give access to its parts and to its sanitized form.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/filename.rb
Included Modules
- Comparable(ActiveStorage::Filename)
Defined in:
active_storage/filename.crConstant Summary
-
STRIPPED_CHARACTERS =
" \t\n\v\f\r\u0000" -
Characters trimmed from both ends by
#sanitized, matching Ruby'sString#strip. -
UNSAFE_CHARACTERS =
"\u202E%$|:;/<>?*\"\t\r\n\\" -
Characters considered unsafe for storage.
#sanitizedreplaces each of them with a dash.
Constructors
- .new(filename : String)
-
.wrap(filename : Filename) : Filename
Returns the passed filename unchanged when it is already a
Filename. -
.wrap(filename : String) : Filename
Returns a
Filenamebuilt from the passed string.
Instance Method Summary
-
#<=>(other : Filename)
The comparison operator.
-
#base : String
Returns the part of the filename preceding any extension.
-
#extension : String
Alias of
#extension_without_delimiter. -
#extension_with_delimiter : String
Returns the extension of the filename, including the dot that precedes it.
-
#extension_without_delimiter : String
Returns the extension of the filename, without the dot that precedes it.
- #filename : String
-
#sanitized : String
Returns the sanitized filename.
-
#to_s(io : IO) : Nil
Returns the sanitized version of the filename.
Constructor Detail
Instance Method Detail
The comparison operator. Returns 0 if the two objects are equal,
a negative number if this object is considered less than other,
a positive number if this object is considered greater than other,
or nil if the two objects are not comparable.
Subclasses define this method to provide class-specific ordering.
The comparison operator is usually used to sort values:
# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]
# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Returns the part of the filename preceding any extension.
ActiveStorage::Filename.new("racecar.jpg").base # => "racecar"
ActiveStorage::Filename.new(".gitignore").base # => ".gitignore"
Returns the extension of the filename, including the dot that precedes it.
ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
ActiveStorage::Filename.new("racecar").extension_with_delimiter # => ""
Returns the extension of the filename, without the dot that precedes it.
ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
ActiveStorage::Filename.new("racecar").extension_without_delimiter # => ""
Returns the sanitized filename.
ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"