class String
Copyright (C) 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.
Mostly borrowed from Rails' ActiveSupport::Inflections
Public Instance Methods
# File lib/githooks/core_ext/string/inflections.rb, line 33 def camelize dup.camelize! end
# File lib/githooks/core_ext/string/inflections.rb, line 37 def camelize! tap do tr!('-', '_') sub!(/^[a-z\d]*/, &:capitalize) gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } gsub!('/', '::') end end
# File lib/githooks/core_ext/string/inflections.rb, line 22 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
# File lib/githooks/core_ext/string/inflections.rb, line 73 def dasherize dup.dasherize! end
# File lib/githooks/core_ext/string/inflections.rb, line 77 def dasherize! tap do underscore! tr!('_', '-') end end
# 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
# File lib/githooks/core_ext/string/sanitize.rb, line 66 def sanitize(*methods) dup.sanitize!(*methods) end
# 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
# File lib/githooks/core_ext/string/sanitize.rb, line 42 def strip_colors gsub(/\x1b\[\d+(?:;\d+)?m/, '') end
# File lib/githooks/core_ext/string/sanitize.rb, line 38 def strip_colors! replace(strip_colors) end
# File lib/githooks/core_ext/string/sanitize.rb, line 26 def strip_empty_lines split(/\n/).reject { |s| s !~ /\S/ }.join("\n") end
# File lib/githooks/core_ext/string/sanitize.rb, line 22 def strip_empty_lines! replace(strip_empty_lines) end
# File lib/githooks/core_ext/string/sanitize.rb, line 34 def strip_non_printable gsub(/[^[:print:] \n\t\x1b]/, '') end
# File lib/githooks/core_ext/string/sanitize.rb, line 30 def strip_non_printable! replace(strip_non_printable) end
# File lib/githooks/core_ext/string/inflections.rb, line 59 def titleize dup.titleize! end
# File lib/githooks/core_ext/string/inflections.rb, line 64 def titleize! tap do replace( split(/\b/).collect(&:capitalize).join ) end end
# File lib/githooks/core_ext/string/inflections.rb, line 46 def underscore dup.underscore! end
# File lib/githooks/core_ext/string/inflections.rb, line 50 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