From dd50144bcd4dbd605995123ab5afc99e40e9a630 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 30 Jun 2017 19:12:58 +0200 Subject: First sketching --- lib/active_file/filename.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/active_file/filename.rb (limited to 'lib/active_file/filename.rb') diff --git a/lib/active_file/filename.rb b/lib/active_file/filename.rb new file mode 100644 index 0000000000..b3c184e26c --- /dev/null +++ b/lib/active_file/filename.rb @@ -0,0 +1,31 @@ +class ActiveFile::Filename + include Comparable + + def initialize(filename) + @filename = filename + end + + def extname + File.extname(@filename) + end + + def extension + extname.from(1) + end + + def base + File.basename(@filename, extname) + end + + def sanitized + @filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-") + end + + def to_s + sanitized.to_s + end + + def <=>(other) + to_s.downcase <=> other.to_s.downcase + end +end -- cgit v1.2.3