Class: String

Inherits:
Object show all
Defined in:
lib/volt/extra_core/blank.rb,
lib/volt/extra_core/string.rb

Instance Method Summary collapse

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

Returns:



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

#dasherizeObject



22
23
24
# File 'lib/volt/extra_core/string.rb', line 22

def dasherize
  gsub('_', '-')
end

#plural?Boolean

Returns:



38
39
40
41
# File 'lib/volt/extra_core/string.rb', line 38

def plural?
  # TODO: Temp implementation
  pluralize == self
end

#pluralizeObject



26
27
28
# File 'lib/volt/extra_core/string.rb', line 26

def pluralize
  Volt::Inflector.pluralize(self)
end

#singular?Boolean

Returns:



43
44
45
46
# File 'lib/volt/extra_core/string.rb', line 43

def singular?
  # TODO: Temp implementation
  singularize == self
end

#singularizeObject



30
31
32
# File 'lib/volt/extra_core/string.rb', line 30

def singularize
  Volt::Inflector.singularize(self)
end

#titleizeObject



34
35
36
# File 'lib/volt/extra_core/string.rb', line 34

def titleize
  gsub('_', ' ').split(' ').map(&:capitalize).join(' ')
end

#underscoreObject

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