class ActiveStorage::Filename
Encapsulates a string representing a filename to provide convenience access to parts of it and a sanitized version. This is what's returned by ActiveStorage::Blob#filename
. A
Filename
instance is comparable so it can be used for sorting.
Public Class Methods
new(filename)
click to toggle source
# File activestorage/app/models/active_storage/filename.rb, line 8 def initialize(filename) @filename = filename end
Public Instance Methods
<=>(other)
click to toggle source
# File activestorage/app/models/active_storage/filename.rb, line 62 def <=>(other) to_s.downcase <=> other.to_s.downcase end
as_json(*)
click to toggle source
# File activestorage/app/models/active_storage/filename.rb, line 54 def as_json(*) to_s end
base()
click to toggle source
Returns the basename of the filename.
ActiveStorage::Filename.new("racecar.jpg").base # => "racecar"
# File activestorage/app/models/active_storage/filename.rb, line 15 def base File.basename @filename, extension_with_delimiter end
extension_with_delimiter()
click to toggle source
Returns the extension with delimiter of the filename.
ActiveStorage::Filename.new("racecar.jpg").extension_with_delimiter # => ".jpg"
# File activestorage/app/models/active_storage/filename.rb, line 22 def extension_with_delimiter File.extname @filename end
extension_without_delimiter()
click to toggle source
Returns the extension without delimiter of the filename.
ActiveStorage::Filename.new("racecar.jpg").extension_without_delimiter # => "jpg"
# File activestorage/app/models/active_storage/filename.rb, line 29 def extension_without_delimiter extension_with_delimiter.from(1).to_s end
Also aliased as: extension
parameters()
click to toggle source
# File activestorage/app/models/active_storage/filename.rb, line 45 def parameters Parameters.new self end
sanitized()
click to toggle source
Returns the sanitized filename.
ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg" ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"
…and any other character unsafe for URLs or storage is converted or stripped.
# File activestorage/app/models/active_storage/filename.rb, line 41 def sanitized @filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-") end
to_json()
click to toggle source
# File activestorage/app/models/active_storage/filename.rb, line 58 def to_json to_s end
to_s()
click to toggle source
Returns the sanitized version of the filename.
# File activestorage/app/models/active_storage/filename.rb, line 50 def to_s sanitized.to_s end