Sha256: b306ed34d9fd26801faa136e452be70b54d7f87646e61cda7ab71c04168ed165

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'jinx/active_support/inflector'

class String
  # @param [Numeric] quantity the amount qualifier
  # @return [String] this String qualified by a plural if the quantity is not 1
  # @example
  #   "rose".quantify(3) #=> "roses"
  #   "rose".quantify(1 #=> "rose"
  def quantify(quantity)
    Jinx.fail(ArgumentError, "Missing quantity argument") if quantity.nil?
    "#{quantity} #{quantity == 1 ? self : pluralize}"
  end
  
  # @return [String] this String with the first letter capitalized and other letters preserved
  # @example
  #   "rosesAreRed".capitalize_first #=> "RosesAreRed"
  def capitalize_first
    sub(/(?:^)(.)/) { $1.upcase }
  end
  
  # @return [String] this String with a leading indefinite article
  # @example
  #   "rose".indefinitize #=> "a rose"
  #   "eagle".indefinitize #=> "an eagle"
  def indefinitize
    article = self =~ /^[aeiou]/i ? 'an' : 'a'
    "#{article} #{self}"
  end
  
  # @return [String] this String with the first letter decapitalized and other letters preserved
  # @example
  #   "RosesAreRed".decapitalize #=> "rosesAreRed"
  def decapitalize
    sub(/(?:^)(.)/) { $1.downcase }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jinx-2.1.1 lib/jinx/helpers/inflector.rb