Sha256: 7e0a20a14002c031efc22b57bec60da43b16574a9c492fc5addd9078d4005401

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

class String

  # Converts a string to camelcase.
  #
  # By default camelcase leaves the first character of the string as given.
  # If +first_letter+ is set to +:lower+ or +false+, then +#camelcase+ will
  # produce lowerCamelCase. If it is set to +:upper+ or +true+ it will
  # produce UpperCamelCase.
  #
  # +#camelcase+ also converts '/' to '::' which is useful for converting
  # paths to namespaces.
  #
  # Examples
  #   "camel_case".camelcase             #=> "camelCase"
  #   "camel/case".camelcase(true)       #=> "Camel::Case"
  #   "Camel_case".camelcase(false)      #=> "camelCase"
  #
  # TODO: Is this the best approach? Should lowerCamelCase be default instead?
  unless method_defined?(:camelcase)
    def camelcase(first_letter=nil)
      case first_letter
      when :upper, true
        upper_camelcase
      when :lower, false
        lower_camelcase
      else
        str = dup
        str.gsub!(/\/(.?)/){ "::#{$1.upcase}" }  # NOT SO SURE ABOUT THIS
        str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
        #str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase }
        str
      end
    end
  end

  def upper_camelcase
    str = dup
    str.gsub!(/\/(.?)/){ "::#{$1.upcase}" }  # NOT SO SURE ABOUT THIS
    str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
    str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase }
    str
  end

  def lower_camelcase
    str = dup
    str.gsub!(/\/(.?)/){ "::#{$1.upcase}" }  # NOT SO SURE ABOUT THIS
    str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
    str.gsub!(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
    str
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/string/camelcase.rb
facets-2.8.3 lib/core/facets/string/camelcase.rb
facets-2.8.2 lib/core/facets/string/camelcase.rb
facets-2.8.1 lib/core/facets/string/camelcase.rb
facets-2.8.0 lib/core/facets/string/camelcase.rb