Module: Cameltoe::String

Extended by:
String
Included in:
String
Defined in:
lib/cameltoe.rb

Overview

Cameltoe string helpers

Use Cameltoe’s string functions to give your strings that signature hump-in-the-middle.

Instance Method Summary collapse

Instance Method Details

#cameltoeize(string, inverse_mode = false) ⇒ Object

By default, cameltoeize converts strings to CameLtoEcasE. If the argument to camelize is set to “:inverse” then camelize produces iNVERSECaMeLTOECASe.

Examples

cameltoeize("Lindsay Lohan") #=> "LindSayLohaN"
cameltoeize("Jessica Simpson") #=> "JessiCasImpsoN"
cameltoeize("Britney Spears", :inverse) #=> "bRITNeYsPEARs"
cameltoeize("Anna Kournikova", :inverse) #=> "aNNAKoURnIKOVa"


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cameltoe.rb', line 17

def cameltoeize(string, inverse_mode = false)
  string = string.dup if string.frozen?
  
  # preserve number of padding spaces at the left and right of the original string
  padding = {
    :left => string.index(/[^ ]/),
    :right => string.reverse.index(/[^ ]/)
  }
  
  # remove all spaces from the string
  string.gsub!(/[ _]/, '')

  # find the boundaries at which word characters start
  word_boundaries = {
    :left => string.index(/\w/),
    :right => string.rindex(/\w/)
  }
  
  # lowercase the string in preparation for camel toe casing
  string.downcase!

  # uppercase the first and last character
  word_boundaries.each_value {|i| string[i] = string[i..i].upcase }
  
  # Don't generate the inner hump if the string is too short for camel toe casing
  len = word_boundaries[:right] - word_boundaries[:left] + 1
  unless len < 5
    # generate the camel toe
    even = (len & 1 == 0 ? true : false)
    [
      word_boundaries[:left] +
      (even ? (len / 2) - 2 : (len / 2) - 0.5),
    
      word_boundaries[:left] +
      (even ? (len / 2) + 1 : (len / 2) + 1.5)
    ].each {|i| string[i] = string[i..i].upcase }
  end
  
  # swap the case if cameltoeize was called in inverse mode
  string.swapcase! if inverse_mode == :inverse
  
  # replace the padding spaces that were present in the original string
  padding[:left].times { string = ' ' << string }
  padding[:right].times { string << ' ' }
  
  string
end