Module: Utils::Formatter

Included in:
Mp3::Disc, Mp3::Track
Defined in:
lib/utils/formatter.rb

Constant Summary collapse

FORMAT_FOR_X =
"%02d"
FORMAT_FOR_XX =
"%02d"
FORMAT_FOR_XXX =
"%03d"
FORMAT_FOR_OTHERS =
"%d"

Instance Method Summary collapse

Instance Method Details

#format(prefix, separator, suffix, number, total) ⇒ Object

This function formats two numbers (the first representing an position within a set, the second representing how many things are in the set) using a prefix, a separator, and a suffix.

Input Parameters

prefix

specifies a string that should be prepended to the result string.

separator

specifies a string that should separate the two numeric parts of the result string.

suffix

specifies a string that should be appended to the result string.

number

specifies the position of an item within a set.

total

specifies the number of items within the set.

Output Parameters

None.

Return Values

The formatted string.

Examples

  1. with a prefix of “D”, a separator of “.”, the values 3 and 9 would be formatted as D03.09 (useful for unique disc names);

  2. with a prefix of “T”, a separator of “.”, the values 16 and 99 would be formatted as T16.99 (useful for unique track names).



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/utils/formatter.rb', line 45

def format(prefix, separator, suffix, number, total)

    prefix    ||= ''
    separator ||= ''
    suffix    ||= ''

    if    total <   10; fmt = FORMAT_FOR_X
    elsif total <  100; fmt = FORMAT_FOR_XX
    elsif total < 1000; fmt = FORMAT_FOR_XXX
    else                fmt = FORMAT_FOR_OTHERS
    end

    return sprintf(prefix + fmt + separator + fmt + suffix, number, total)
end