Class: String
- Defined in:
- lib/volt/extra_core/blank.rb,
lib/volt/extra_core/string.rb
Instance Method Summary collapse
-
#blank? ⇒ Boolean
A string is blank if it’s empty or contains whitespaces only:.
-
#camelize(first_letter = :upper) ⇒ Object
Turns a string into the camel case version.
- #dasherize ⇒ Object
- #plural? ⇒ Boolean
- #pluralize ⇒ Object
- #singular? ⇒ Boolean
- #singularize ⇒ Object
- #titleize ⇒ Object
-
#underscore ⇒ Object
Returns the underscore version of a string.
Instance Method Details
#blank? ⇒ Boolean
A string is blank if it’s empty or contains whitespaces only:
''.blank? # => true
' '.blank? # => true
' '.blank? # => true
' something here '.blank? # => false
72 73 74 75 76 |
# File 'lib/volt/extra_core/blank.rb', line 72 def blank? # self !~ /[^[:space:]]/ # TODO: Opal fails with the previous regex strip == '' end |
#camelize(first_letter = :upper) ⇒ Object
Turns a string into the camel case version. If it is already camel case, it should return the same string.
9 10 11 12 13 14 |
# File 'lib/volt/extra_core/string.rb', line 9 def camelize(first_letter = :upper) new_str = gsub(/_[a-z]/) { |a| a[1].upcase } new_str = new_str[0].capitalize + new_str[1..-1] if first_letter == :upper new_str end |
#dasherize ⇒ Object
22 23 24 |
# File 'lib/volt/extra_core/string.rb', line 22 def dasherize gsub('_', '-') end |
#plural? ⇒ Boolean
38 39 40 41 |
# File 'lib/volt/extra_core/string.rb', line 38 def plural? # TODO: Temp implementation pluralize == self end |
#pluralize ⇒ Object
26 27 28 |
# File 'lib/volt/extra_core/string.rb', line 26 def pluralize Volt::Inflector.pluralize(self) end |
#singular? ⇒ Boolean
43 44 45 46 |
# File 'lib/volt/extra_core/string.rb', line 43 def singular? # TODO: Temp implementation singularize == self end |
#singularize ⇒ Object
30 31 32 |
# File 'lib/volt/extra_core/string.rb', line 30 def singularize Volt::Inflector.singularize(self) end |
#titleize ⇒ Object
34 35 36 |
# File 'lib/volt/extra_core/string.rb', line 34 def titleize gsub('_', ' ').split(' ').map(&:capitalize).join(' ') end |
#underscore ⇒ Object
Returns the underscore version of a string. If it is already underscore, it should return the same string.
18 19 20 |
# File 'lib/volt/extra_core/string.rb', line 18 def underscore gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase end |