Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/marfa/helpers/classes/string.rb

Overview

Additional String functionality

Instance Method Summary collapse

Instance Method Details

#strip_tags!String

Remove tags

Examples:

"<a>some/path</a>".strip_tags!

Returns:



37
38
39
# File 'lib/marfa/helpers/classes/string.rb', line 37

def strip_tags!
  self.gsub(/<\/?[^>]*>/, '') # unless self.nil?
end

#to_class_nameString

Convert string like ‘category/list’ to CamelCase

Examples:

"some/path".to_class_name

Returns:



15
16
17
18
19
# File 'lib/marfa/helpers/classes/string.rb', line 15

def to_class_name
  parts = downcase.split('/')
  parts.each(&:capitalize!)
  parts.join('').gsub(%r{-}, '')
end

#to_price!String

Convert string price to preferred view

Examples:

"1042.42".to_price!

Returns:



45
46
47
48
49
50
51
# File 'lib/marfa/helpers/classes/string.rb', line 45

def to_price!
  # self.split('.') divides string into substring with '.' delimiter and returning array of this substrings
  # .first returns the first element of the array
  # .reverse returns a new string with the characters from str in reverse order
  # .gsub(pattern, replacement) returns a copy of str with the all occurrences of pattern substituted for the second argument
  self.split('.').first.reverse.gsub(/...(?=.)/, '\&;psniht&').reverse
end

#to_underscoreString

Replaces all ‘/’ to ‘_’

Examples:

"some/path".to_underscore

Returns:



7
8
9
# File 'lib/marfa/helpers/classes/string.rb', line 7

def to_underscore
  downcase.gsub(%r{/}, '_')
end

#to_urlString

Convert string to url part

Examples:

"some/path".to_url

Returns:



25
26
27
28
29
30
31
# File 'lib/marfa/helpers/classes/string.rb', line 25

def to_url
  val = self.strip_tags!
  val = val.gsub(':', '')
  val = val.gsub(' ', '-')
  val = val.gsub('/', '-')
  val.downcase
end