class String

Public Instance Methods

dedupe(str) click to toggle source

strip multiple concurrent characters

# File lib/finishing_moves/string.rb, line 62
def dedupe(str)
  raise ArgumentError, "#{str.inspect} is not a string" unless str.is_a? String
  # Yes, this is inefficient. We welcome optimizations from the regex ninjas out there.
  ret = String.new self
  str.each_char { |c| ret.gsub! /#{Regexp.escape c}{2,}/, c }
  ret
end
dedupe!(str) click to toggle source
# File lib/finishing_moves/string.rb, line 70
def dedupe!(str)
  raise ArgumentError, "#{str.inspect} is not a string" unless str.is_a? String
  # Yes, this is inefficient. We welcome optimizations from the regex ninjas out there.
  str.each_char { |c| gsub! /#{Regexp.escape c}{2,}/, c }
end
keyify() click to toggle source
# File lib/finishing_moves/string.rb, line 15
def keyify
  _prep_key_or_slug(true)
end
keyify!() click to toggle source
# File lib/finishing_moves/string.rb, line 19
def keyify!
  result = _prep_key_or_slug(true)
  raise ArgumentError, "#{self.inspect} cannot be keyified, no valid characters" if result.nil?
  result
end
lstrip_all(chars = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 45
def lstrip_all(chars = nil)
  gsub _lstrip_all_regex(_strip_all_prep(chars)), ''
end
lstrip_all!(chars = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 49
def lstrip_all!(chars = nil)
  gsub! _lstrip_all_regex(_strip_all_prep(chars)), ''
end
match?(pattern, pos = 0) click to toggle source

return true/false on regex match

# File lib/finishing_moves/string.rb, line 93
def match?(pattern, pos = 0)
  match(pattern, pos).not_nil?
end
newline_to(rep = ' ') click to toggle source
# File lib/finishing_moves/string.rb, line 7
def newline_to(rep = ' ')
  gsub _nl_gsub_regex, rep.to_s
end
newline_to!(rep = ' ') click to toggle source
# File lib/finishing_moves/string.rb, line 11
def newline_to!(rep = ' ')
  gsub! _nl_gsub_regex, rep.to_s
end
nl2br() click to toggle source
# File lib/finishing_moves/string.rb, line 3
def nl2br
  gsub _nl_gsub_regex, "<br />\n"
end
remove_whitespace(_ignored = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 76
def remove_whitespace(_ignored = nil)
  replace_whitespace('')
end
remove_whitespace!(_ignored = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 80
def remove_whitespace!(_ignored = nil)
  replace_whitespace!('')
end
replace_whitespace(replace = '') click to toggle source
# File lib/finishing_moves/string.rb, line 84
def replace_whitespace(replace = '')
  gsub /[ ]/, replace
end
replace_whitespace!(replace = '') click to toggle source
# File lib/finishing_moves/string.rb, line 88
def replace_whitespace!(replace = '')
  gsub! /[ ]/, replace
end
rstrip_all(chars = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 53
def rstrip_all(chars = nil)
  gsub _rstrip_all_regex(_strip_all_prep(chars)), ''
end
rstrip_all!(chars = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 57
def rstrip_all!(chars = nil)
  gsub! _rstrip_all_regex(_strip_all_prep(chars)), ''
end
slugify() click to toggle source
# File lib/finishing_moves/string.rb, line 25
def slugify
  _prep_key_or_slug(false)
end
slugify!() click to toggle source
# File lib/finishing_moves/string.rb, line 29
def slugify!
  result = _prep_key_or_slug(false)
  raise ArgumentError, "#{self.inspect} cannot be slugified, no valid characters" if result.nil?
  result
end
strip_all(chars = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 35
def strip_all(chars = nil)
  expr = _strip_all_prep(chars)
  gsub Regexp.union(_lstrip_all_regex(expr), _rstrip_all_regex(expr)), ''
end
strip_all!(chars = nil) click to toggle source
# File lib/finishing_moves/string.rb, line 40
def strip_all!(chars = nil)
  expr = _strip_all_prep(chars)
  gsub! Regexp.union(_lstrip_all_regex(expr), _rstrip_all_regex(expr)), ''
end
to_bool() click to toggle source
# File lib/finishing_moves/to_bool.rb, line 2
def to_bool
  return true if self == true || self =~ (/^(1|t(rue)?|y(es)?|on)$/i)
  return false if self == false || self == "" || self =~ /\A[[:space:]]*\z/ || self =~ (/^(0|f(alse)?|no?|off)$/i)
  raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
to_uuid() click to toggle source
# File lib/finishing_moves/string.rb, line 97
def to_uuid
  if length == 36 && self.match?(/^[0-9A-F-]+$/i)
    return self
  elsif length == 32 && self.match?(/^[0-9A-F]+$/i)
    str = self.dup
    return str[0,8] + '-' +str[8,4] + '-' +str[12,4] + '-' +str[16,4] + '-' +str[20,12]
  else
    raise ArgumentError, "#{self} is not a valid UUID"
  end
end

Protected Instance Methods

_lstrip_all_regex(expr) click to toggle source
# File lib/finishing_moves/string.rb, line 120
def _lstrip_all_regex(expr)
  /\A[#{expr}]+/
end
_nl_gsub_regex() click to toggle source

TODO def each_char_index(&block) how to return enumerator if no block given? blog.arkency.com/2014/01/ruby-to-enum-for-enumerator/ end

# File lib/finishing_moves/string.rb, line 116
def _nl_gsub_regex
  /(?:\n\r?|\r\n?)/
end
_prep_key_or_slug(keyified = true) click to toggle source
# File lib/finishing_moves/string.rb, line 139
def _prep_key_or_slug(keyified = true)
  result = nil_chain do
    the_key = self.dup

    # strip all non-alphanumerics
    the_key.gsub!(/[^0-9a-z]+/i, '_')

    # borrowing some logic from Rails method underscore
    # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore
    if the_key =~ /[A-Z-]|::/
      the_key.gsub!(/::/, '_')
      the_key.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
      the_key.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
    end

    # Strip any leading numbers (keyify only)
    the_key.lstrip_all!('0-9') if keyified
    # Strip any leading or trailing underscores
    the_key.strip_all!('_')
    # Combine multiple concurrent underscores
    the_key.dedupe!('_')
    # lowercase
    the_key.downcase!
    # replace underscore with dashes (slugify only)
    the_key.gsub!('_', '-') unless keyified
    next the_key
  end
  case
    when result.nil? || result == ''
      nil
    when keyified
      result.to_sym
    else result
  end
end
_rstrip_all_regex(expr) click to toggle source
# File lib/finishing_moves/string.rb, line 124
def _rstrip_all_regex(expr)
  /[#{expr}]+\Z/
end
_strip_all_prep(chars) click to toggle source
# File lib/finishing_moves/string.rb, line 128
def _strip_all_prep(chars)
  chars = '-_' if chars.nil?
  raise ArgumentError, "#{chars.inspect} is not a string" unless chars.is_a? String

  expr = Regexp.escape( chars.gsub(/(0-9)+|(a-z)+|(A-Z)+|\s+/, '').strip )
  ['0-9', 'a-z', 'A-Z'].each do |range|
    expr << range if chars.include? range
  end
  expr << ' '
end