Module: FilenameCleaner

Defined in:
lib/filename_cleaner/cli.rb,
lib/filename_cleaner/version.rb,
lib/filename_cleaner/filename_cleaner.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.4.0"
DOT =
"."

Class Method Summary collapse

Class Method Details

.formatted_name(filename, opts = {}) ⇒ Object

Get formatted name for existing file

Parameters:

  • filename (String)

    the input filename

  • opts (Hash<Symbol, Object>) (defaults to: {})

    the hash value for options to be applied

Options Hash (opts):

  • :sep_char (String)

    The separator string

  • :downcase (Boolean)

    Convert each word to lower case

  • :capitalize: (Boolean)

    Capitalize each word in the name



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/filename_cleaner/filename_cleaner.rb', line 27

def formatted_name(filename, opts = {})
  sep_char = opts[:sep_char] || "."
  sanitized_name = FilenameCleaner.sanitize(filename, sep_char, true)

  # First split the two part so that only name is used!
  basename = File.basename(sanitized_name, ".*")
  extname  = File.extname(sanitized_name)
  if opts[:downcase]
    basename = basename.split(sep_char).map(&:downcase).join(sep_char)
  end
  if opts[:capitalize]
    basename = basename.split(sep_char).map(&:capitalize).join(sep_char)
  end
  "#{basename}#{extname}"
end

.sanitize(name, sep_char = ".", have_extension = false) ⇒ String

Sanitize the any name with or without extension

Parameters:

  • name (String)

    the input string

  • sep_char (String) (defaults to: ".")

    the separator char to be used

  • have_extension (Boolean) (defaults to: false)

    indicate if the the input file should be treated as having extension or not having one

Returns:

  • (String)

    output string with special chars replaced withe specified string



11
12
13
14
15
16
17
# File 'lib/filename_cleaner/filename_cleaner.rb', line 11

def sanitize(name, sep_char = ".", have_extension = false)
  if have_extension
    sanitize_name_with_extension(name, sep_char)
  else
    sanitize_name_without_extension(name, sep_char)
  end
end