Class: String
- Inherits:
-
Object
- Object
- String
- 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
- #blank? ⇒ Boolean
- #camel_case ⇒ Object
- #deconstantize ⇒ Object
- #demodulize ⇒ Object
- #snake_case ⇒ Object
- #to_b ⇒ Object
- #to_class ⇒ Object
Instance Method Details
#blank? ⇒ Boolean
33 34 35 |
# File 'lib/stbaldricks/patches/string.rb', line 33 def blank? strip.empty? end |
#camel_case ⇒ Object
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 |
#deconstantize ⇒ Object
37 38 39 |
# File 'lib/stbaldricks/patches/string.rb', line 37 def deconstantize rpartition(MODULE_SEPARATOR).first end |
#demodulize ⇒ Object
41 42 43 |
# File 'lib/stbaldricks/patches/string.rb', line 41 def demodulize rpartition(MODULE_SEPARATOR).last end |
#snake_case ⇒ Object
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_b ⇒ Object
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_class ⇒ Object
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 |