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 =
'::'
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
31 32 33 |
# File 'lib/stbaldricks/patches/string.rb', line 31 def blank? strip.empty? end |
#camel_case ⇒ Object
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 |
#deconstantize ⇒ Object
35 36 37 |
# File 'lib/stbaldricks/patches/string.rb', line 35 def deconstantize rpartition(MODULE_SEPARATOR).first end |
#demodulize ⇒ Object
39 40 41 |
# File 'lib/stbaldricks/patches/string.rb', line 39 def demodulize rpartition(MODULE_SEPARATOR).last end |
#snake_case ⇒ Object
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_b ⇒ Object
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_class ⇒ Object
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 |