Class: String

Inherits:
Object show all
Defined in:
lib/githooks/core_ext/string/inflections.rb,
lib/githooks/core_ext/string/sanitize.rb,
lib/githooks/core_ext/string/git_option_path_split.rb

Overview

Copyright © 2013 Carl P. Corliss

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Instance Method Summary collapse

Instance Method Details

#camelizeObject



35
36
37
# File 'lib/githooks/core_ext/string/inflections.rb', line 35

def camelize
  dup.camelize!
end

#camelize!Object



39
40
41
42
43
44
45
46
# File 'lib/githooks/core_ext/string/inflections.rb', line 39

def camelize!
  tap do
    gsub!('-', '_')
    sub!(/^[a-z\d]*/, &:capitalize)
    gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
    gsub!('/', '::')
  end
end

#constantizeObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/githooks/core_ext/string/inflections.rb', line 24

def constantize
  names = split('::')
  names.shift if names.empty? || names.first.empty?

  names.inject(Object) do |obj, name|
    obj.const_defined?(name) ? obj.const_get(name) : obj.const_missing(name)
  end
rescue NameError => e
  raise unless e.message =~ /uninitialized constant/
end

#dasherizeObject



75
76
77
# File 'lib/githooks/core_ext/string/inflections.rb', line 75

def dasherize
  dup.dasherize!
end

#dasherize!Object



79
80
81
82
83
84
# File 'lib/githooks/core_ext/string/inflections.rb', line 79

def dasherize!
  tap do
    underscore!
    gsub!(/_/, '-')
  end
end

#git_option_path_splitObject



20
21
22
23
# File 'lib/githooks/core_ext/string/git_option_path_split.rb', line 20

def git_option_path_split
  section, *subsection, option = split('.')
  [section, subsection.join('.'), option]
end

#sanitize(*methods) ⇒ Object



66
67
68
# File 'lib/githooks/core_ext/string/sanitize.rb', line 66

def sanitize(*methods)
  dup.sanitize!(*methods)
end

#sanitize!(*methods) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/githooks/core_ext/string/sanitize.rb', line 46

def sanitize!(*methods)
  options = methods.extract_options!

  map = {
    strip:         :strip!,
    empty_lines:   :strip_empty_lines!,
    non_printable: :strip_non_printable!,
    colors:        :strip_colors!
  }

  methods = map.keys if methods.empty? || methods.include?(:all)
  methods -= Array(options.delete(:except)) if options[:except]

  methods.collect(&:to_sym).each do |method|
    send(map[method]) if map[method]
  end

  self
end

#strip_colorsObject



42
43
44
# File 'lib/githooks/core_ext/string/sanitize.rb', line 42

def strip_colors
  gsub(/\x1b\[\d+(?:;\d+)?m/, '')
end

#strip_colors!Object



38
39
40
# File 'lib/githooks/core_ext/string/sanitize.rb', line 38

def strip_colors!
  replace(strip_colors)
end

#strip_empty_linesObject



26
27
28
# File 'lib/githooks/core_ext/string/sanitize.rb', line 26

def strip_empty_lines
  split(/\n/).reject { |s| s !~ /\S/ }.join("\n")
end

#strip_empty_lines!Object



22
23
24
# File 'lib/githooks/core_ext/string/sanitize.rb', line 22

def strip_empty_lines!
  replace(strip_empty_lines)
end

#strip_non_printableObject



34
35
36
# File 'lib/githooks/core_ext/string/sanitize.rb', line 34

def strip_non_printable
  gsub(/[^[:print:] \n\t\x1b]/, '')
end

#strip_non_printable!Object



30
31
32
# File 'lib/githooks/core_ext/string/sanitize.rb', line 30

def strip_non_printable!
  replace(strip_non_printable)
end

#titleizeObject Also known as: titlize



61
62
63
# File 'lib/githooks/core_ext/string/inflections.rb', line 61

def titleize
  dup.titleize!
end

#titleize!Object Also known as: titlize!



66
67
68
69
70
71
72
# File 'lib/githooks/core_ext/string/inflections.rb', line 66

def titleize!
  tap do
    replace(
      split(/\b/).collect(&:capitalize).join
    )
  end
end

#underscoreObject



48
49
50
# File 'lib/githooks/core_ext/string/inflections.rb', line 48

def underscore
  dup.underscore!
end

#underscore!Object



52
53
54
55
56
57
58
59
# File 'lib/githooks/core_ext/string/inflections.rb', line 52

def underscore!
  tap do
    gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    tr!('-', '_')
    downcase!
  end
end