Class: String

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

Instance Method Summary collapse

Methods included from ReactiveTags

included, #reactive_method_tag

Instance Method Details

#+(val) ⇒ Object

In volt, we want a value + reactive strings to return a reactive string. So we over-ride + to check for when we are adding a reactive string to a string.



12
13
14
15
16
17
18
19
# File 'lib/volt/reactive/string_extensions.rb', line 12

def +(val)
  result = __old_plus(val.cur)
  if val.reactive? && !result.reactive?
    result = ReactiveValue.new(result)
  end

  return result
end

#<<(val) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/volt/reactive/string_extensions.rb', line 22

def <<(val)
  if val.reactive?
    raise "Cannot append a reactive string to non-reactive string.  Use + instead"
  end
  result = __old_concat(val)

  return result
end

#__old_concatObject



6
# File 'lib/volt/reactive/string_extensions.rb', line 6

alias :__old_concat :<<

#__old_plusObject



4
# File 'lib/volt/reactive/string_extensions.rb', line 4

alias :__old_plus :+

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



73
74
75
76
77
# File 'lib/volt/extra_core/blank.rb', line 73

def blank?
  # self !~ /[^[:space:]]/
  # TODO: Opal fails with the previous regex
  self.strip == ''
end

#camelizeObject

TODO: replace with better implementations NOTE: strings are currently immutable in Opal, so no ! methods



4
5
6
# File 'lib/volt/extra_core/string.rb', line 4

def camelize
  self.split("_").map {|s| s.capitalize }.join("")
end

#plural?Boolean

Returns:



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

def plural?
  # TODO: Temp implementation
  self[-1] == 's'
end

#pluralizeObject



12
13
14
15
16
17
18
19
# File 'lib/volt/extra_core/string.rb', line 12

def pluralize
  # TODO: Temp implementation
  if self[-1] != 's'
    return self + 's'
  else
    return self
  end
end

#singular?Boolean

Returns:



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

def singular?
  # TODO: Temp implementation
  self[-1] != 's'
end

#singularizeObject



21
22
23
24
25
26
27
28
# File 'lib/volt/extra_core/string.rb', line 21

def singularize
  # TODO: Temp implementation
  if self[-1] == 's'
    return self[0..-2]
  else
    return self
  end
end

#underscoreObject



8
9
10
# File 'lib/volt/extra_core/string.rb', line 8

def underscore
  self.scan(/[A-Z][a-z]*/).join("_").downcase
end