module ChaosDetector::Utils::StrUtil

Constants

SCORE
SPACE
STR_BLANK
STR_INDENT
STR_NS_SEP

Public Class Methods

blank?(obj) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 40
def blank?(obj)
  obj.nil? || obj.to_s.empty?
end
clamp_chars(clamp: :none) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 54
def clamp_chars(clamp: :none)
  case clamp
  when :angle, :arrow
    ['<', '>']
  when :brace
    ['{', '}']
  when :bracket
    ['[', ']']
  when :italic, :emphasize
    %w[_ _]
  when :strong, :bold, :stars
    ['**', '**']
  when :quotes, :double_quotes
    ['"', '"']
  when :ticks, :single_quotes
    ["'", "'"]
  when :none
    [STR_BLANK, STR_BLANK]
  else # :parens, :parentheses
    ['(', ')']
  end
end
d(text, clamp: :nil, prefix: nil, suffix: nil, sep: nil, indent_length: 0)
Alias for: decorate
decorate(text, clamp: :nil, prefix: nil, suffix: nil, sep: nil, indent_length: 0) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 22
def decorate(text, clamp: :nil, prefix: nil, suffix: nil, sep: nil, indent_length: 0)
  return '' if nay? text

  clamp_pre, clamp_post = clamp_chars(clamp: clamp)
  indent("#{prefix}#{sep}#{clamp_pre}#{text}#{clamp_post}#{sep}#{suffix}", indent_length)
end
Also aliased as: d
decorate_pair(source, dest, indent_length: 0, clamp: :angle, join_str: ' ') click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 13
def decorate_pair(source, dest, indent_length: 0, clamp: :angle, join_str: ' ')
  decorate("#{decorate(source)}#{decorate(dest, prefix: join_str)}", clamp: clamp, indent_length: indent_length)
end
decorate_tuple(tuple, indent_length: 0, clamp: :angle, join_str: ' ') click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 17
def decorate_tuple(tuple, indent_length: 0, clamp: :angle, join_str: ' ')
  body = tuple.map { |t| decorate(t, indent_length: indent_length)}.join(join_str)
  decorate(body, clamp: clamp, indent_length: indent_length)
end
humanize_module(mod_name, max_segments: 2, sep_token: STR_NS_SEP) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 29
def humanize_module(mod_name, max_segments: 2, sep_token: STR_NS_SEP)
  return '' if nay? mod_name
  raise ArgumentError, 'Must have at least 1 segment.' if max_segments < 1

  mod_name.split(sep_token).last(max_segments).join(sep_token)
end
indent(text, indent_length=1) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 77
def indent(text, indent_length=1)
  return '' if nay? text
  return text unless indent_length

  "#{STR_INDENT * indent_length}#{text}"
end
nay?(obj) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 84
def nay?(obj)
  ChaosDetector::Utils::CoreUtil.naught?(obj)
end
snakeize(obj) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 36
def snakeize(obj)
  obj.to_s.gsub(/[^a-zA-Z\d\s:]/, SCORE)
end
squish(str) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 44
def squish(str)
  str.to_s.strip.split.map(&:strip).join(SPACE)
end
titleize(obj) click to toggle source
# File lib/chaos_detector/utils/str_util.rb, line 48
def titleize(obj)
  obj.to_s.split(SCORE).map(&:capitalize).join(SPACE)
end