Class: TextUtil
Class Method Summary collapse
-
.camel_case(s) ⇒ Object
TextUtil.camel_case(“hi there”) # -> HiThere.
- .camel_case!(s) ⇒ Object
- .case_choices ⇒ Object
-
.hyphen_case(s) ⇒ Object
TextUtil.hyphen_case(“hi there”) # -> hi-there.
- .menu ⇒ Object
-
.plus_case(s) ⇒ Object
TextUtil.plus_case(“hi there”) # -> hi+there.
-
.snake_case(s) ⇒ Object
TextUtil.snake_case(“hi there”) # -> hi_there.
- .snake_case!(s) ⇒ Object
-
.title_case(s, options = {}) ⇒ Object
TextUtil.title_case(“hi there”) # -> Hi There.
- .title_case!(s) ⇒ Object
- .unindent(txt, options = {}) ⇒ Object
Class Method Details
.camel_case(s) ⇒ Object
TextUtil.camel_case(“hi there”) # -> HiThere
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/xiki/text_util.rb', line 77 def self.camel_case s # If it's all capitals, make subsequent copitals lowercase if s =~ /^[A-Z_-]+$/ s = s.gsub(/([A-Z][A-Z]+)/) {"#{$1.capitalize}"} end s.gsub(/[ -]/, '_'). gsub(/_([a-z]+)/) {"#{$1.capitalize}"}. sub(/(.)/) {$1.upcase}. gsub("_", ""). gsub(/[^\w]/, "") end |
.camel_case!(s) ⇒ Object
90 91 92 |
# File 'lib/xiki/text_util.rb', line 90 def self.camel_case! s s.replace self.camel_case(s) end |
.case_choices ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/xiki/text_util.rb', line 23 def self.case_choices [ ['upper', lambda {|o| o.upcase}], ['lower', lambda {|o| o.downcase}], ['title', lambda {|o| TextUtil.title_case(o)}], ['camel', lambda {|o| TextUtil.camel_case(o)}], ['snake', lambda {|o| TextUtil.snake_case(o)}], ['plus', lambda {|o| TextUtil.plus_case(o)}], ['hyphen', lambda {|o| TextUtil.hyphen_case(o)}], ] end |
.hyphen_case(s) ⇒ Object
TextUtil.hyphen_case(“hi there”) # -> hi-there
70 71 72 73 74 |
# File 'lib/xiki/text_util.rb', line 70 def self.hyphen_case s s.gsub(/[ _]/, '-'). gsub(/([a-z])([A-Z0-9])/) {"#{$1}-#{$2}"}.downcase. gsub(/--+/, "-") end |
.menu ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/xiki/text_util.rb', line 6 def self. %` > Change case @ TextUtil.camel_case "hey you" @ TextUtil.hyphen_case "hey you" @ TextUtil.snake_case "hey you" @ TextUtil.title_case "hey you" > Modify vars in-place You can also use bang versions, like: @ s = "hey you"; TextUtil.camel_case! s; p s > Unindent @ TextUtil.unindent "hey you" ` end |
.plus_case(s) ⇒ Object
TextUtil.plus_case(“hi there”) # -> hi+there
61 62 63 |
# File 'lib/xiki/text_util.rb', line 61 def self.plus_case s self.snake_case(s).gsub('_', '+') end |
.snake_case(s) ⇒ Object
TextUtil.snake_case(“hi there”) # -> hi_there
53 54 55 56 57 58 |
# File 'lib/xiki/text_util.rb', line 53 def self.snake_case s s.gsub(/[ -]/, '_'). gsub(/([a-z0-9])([A-Z])/) {"#{$1}_#{$2}"}.downcase. gsub(/[^\w]/, ""). gsub(/__+/, "_") end |
.snake_case!(s) ⇒ Object
65 66 67 |
# File 'lib/xiki/text_util.rb', line 65 def self.snake_case! s s.replace self.snake_case(s) end |
.title_case(s, options = {}) ⇒ Object
TextUtil.title_case(“hi there”) # -> Hi There
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/xiki/text_util.rb', line 95 def self.title_case s, ={} s = s.gsub(/[ -]/, '_'). gsub(/([a-z])([A-Z0-9])/) {"#{$1}_#{$2}"}.downcase. gsub(/([a-z]+)/) {"#{$1.capitalize}"}. gsub(/__*/, " ") s.gsub! " ", "_" if [:underscores] s end |
.title_case!(s) ⇒ Object
106 107 108 |
# File 'lib/xiki/text_util.rb', line 106 def self.title_case! s s.replace self.title_case(s) end |
.unindent(txt, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/xiki/text_util.rb', line 35 def self.unindent txt, ={} txt = txt.sub(/\A\n/, '')# if ! options[:no_strip] # Delete initial blank line if there txt.gsub!(/^\s+/) { |t| t.gsub("\t", ' ') } # Untab indent # If 1st line has no indent and 2nd line has indent (at least 3 spaces) if txt !~ /\A / and txt =~ /\A.+\n( +)/ indent = $1 # Indent left by 2nd indent return txt.gsub(/^#{indent}/, '') end old_indent = Line.indent(txt) # Get indent of first line txt.gsub!(/^#{old_indent}/, '') # Delete current indent "#{txt.strip}\n" end |