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 =
'::'.freeze

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:



33
34
35
# File 'lib/stbaldricks/patches/string.rb', line 33

def blank?
  strip.empty?
end

#camel_caseObject



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

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



37
38
39
# File 'lib/stbaldricks/patches/string.rb', line 37

def deconstantize
  rpartition(MODULE_SEPARATOR).first
end

#demodulizeObject



41
42
43
# File 'lib/stbaldricks/patches/string.rb', line 41

def demodulize
  rpartition(MODULE_SEPARATOR).last
end

#snake_caseObject



27
28
29
30
31
# File 'lib/stbaldricks/patches/string.rb', line 27

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

#to_bObject



51
52
53
54
55
56
# File 'lib/stbaldricks/patches/string.rb', line 51

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

  nil
end

#to_classObject



45
46
47
48
49
# File 'lib/stbaldricks/patches/string.rb', line 45

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