Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/stbaldricks/patches/string.rb

Constant Summary collapse

SNAKE_CASE_REGEX =
Regexp.new('(?<!^)([[:upper:]])|([[:upper:]])')
ID_REGEX =
Regexp.new('(^|_)i_d')
UUID_REGEX =
Regexp.new('(^|_)u_u_id')
URL_REGEX =
Regexp.new('(^|_)u_r_l')
MODULE_SEPARATOR =
'::'

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:



31
32
33
# File 'lib/stbaldricks/patches/string.rb', line 31

def blank?
  strip.empty?
end

#camel_caseObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stbaldricks/patches/string.rb', line 8

def camel_case
  x = split('_').map do |e|
    case e
    when 'id', 'uuid', 'url'
      e.upcase

    else
      # capitalize first character (capitalize will lowercase the rest of the word)
      e[0] = e[0].capitalize
      e

    end
  end

  x.join
end

#deconstantizeObject



35
36
37
# File 'lib/stbaldricks/patches/string.rb', line 35

def deconstantize
  rpartition(MODULE_SEPARATOR).first
end

#demodulizeObject



39
40
41
# File 'lib/stbaldricks/patches/string.rb', line 39

def demodulize
  rpartition(MODULE_SEPARATOR).last
end

#snake_caseObject



25
26
27
28
29
# File 'lib/stbaldricks/patches/string.rb', line 25

def snake_case
  gsub(SNAKE_CASE_REGEX) {
    (Regexp.last_match[1] ? "_#{Regexp.last_match[1]}" : Regexp.last_match[2]).downcase
  }.sub(ID_REGEX, '_id').sub(UUID_REGEX, '_uuid').sub(URL_REGEX, '_url')
end

#to_bObject



49
50
51
52
53
# File 'lib/stbaldricks/patches/string.rb', line 49

def to_b
  return false if self == 'false' || self == '0'
  return true if self == 'true' || self == '1'
  nil
end

#to_classObject



43
44
45
46
47
# File 'lib/stbaldricks/patches/string.rb', line 43

def to_class
  split(MODULE_SEPARATOR).reduce(Object) { |a, e| a.const_get(e.to_sym) }
rescue
  nil
end