Sha256: 422ca5382d13cd46d711b4aa36f196faa0d2474e0e26be4f89dac1f1b534584b

Contents?: true

Size: 905 Bytes

Versions: 2

Compression:

Stored size: 905 Bytes

Contents

module Cape

  # Provides utility functions.
  #
  # @api private
  module Util

    # Conditionally transforms the specified _noun_ into its plural form.
    #
    # @param [String] singular_noun  a singular noun
    # @param [Fixnum] count the quantity of _singular_noun_
    #
    # @return [String] the plural of _singular_noun_, unless _count_ is +1+
    def self.pluralize(singular_noun, count=2)
      return singular_noun if count == 1

      "#{singular_noun}s"
    end

    # Builds a list phrase from the elements of the specified _array_.
    #
    # @param [Array of String] array zero or more nouns
    #
    # @return [String] the elements of _array_, joined with commas and "and", as
    #                  appropriate
    def self.to_list_phrase(array)
      return array.join(' and ') if (array.length <= 2)

      [array[0...-1].join(', '), array[-1]].join ', and '
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cape-1.8.0 lib/cape/util.rb
cape-1.7.0 lib/cape/util.rb