Class: String
- Defined in:
- lib/volt/extra_core/blank.rb,
lib/volt/extra_core/string.rb,
lib/volt/page/bindings/html_safe/string_extension.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
- #headerize ⇒ Object
- #html_safe ⇒ Object
- #html_safe? ⇒ Boolean
- #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 |
# File 'lib/volt/extra_core/blank.rb', line 72 def blank? self !~ /\S/ 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 |
#headerize ⇒ Object
38 39 40 41 42 |
# File 'lib/volt/extra_core/string.rb', line 38 def headerize split(/[_-]/) .map { |new_str| new_str[0].capitalize + new_str[1..-1] } .join('-') end |
#html_safe ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/volt/page/bindings/html_safe/string_extension.rb', line 2 def html_safe # Convert to a real string (opal uses native strings normally, so wrap so we can # use instance variables) str = String.new(self) str.instance_variable_set('@html_safe', true) str end |
#html_safe? ⇒ Boolean
10 11 12 |
# File 'lib/volt/page/bindings/html_safe/string_extension.rb', line 10 def html_safe? @html_safe end |
#plural? ⇒ Boolean
44 45 46 47 |
# File 'lib/volt/extra_core/string.rb', line 44 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
49 50 51 52 |
# File 'lib/volt/extra_core/string.rb', line 49 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 |