Class: String

Inherits:
Object show all
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

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
# 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

#dasherizeObject



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

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

#headerizeObject



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_safeObject



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

Returns:



10
11
12
# File 'lib/volt/page/bindings/html_safe/string_extension.rb', line 10

def html_safe?
  @html_safe
end

#plural?Boolean

Returns:



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

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:



49
50
51
52
# File 'lib/volt/extra_core/string.rb', line 49

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