Class: String
Overview
Additional String functionality
Constant Summary collapse
- BLANK_RE =
/\A[[:space:]]*\z/
Instance Method Summary collapse
- #blank? ⇒ true, false
-
#present? ⇒ true, false
Object isn’t blank.
-
#strip_tags! ⇒ String
Remove tags.
-
#to_class_name ⇒ String
Convert string like ‘category/list’ to CamelCase.
-
#to_price! ⇒ String
Convert string price to preferred view.
-
#to_underscore ⇒ String
Replaces all ‘/’ to ‘_’.
-
#to_url ⇒ String
Convert string to url part.
Instance Method Details
#blank? ⇒ true, false
55 56 57 |
# File 'lib/marfa/helpers/classes/string.rb', line 55 def blank? empty? || BLANK_RE.match?(self) end |
#present? ⇒ true, false
Object isn’t blank
61 62 63 |
# File 'lib/marfa/helpers/classes/string.rb', line 61 def present? !blank? end |
#strip_tags! ⇒ String
Remove tags
38 39 40 |
# File 'lib/marfa/helpers/classes/string.rb', line 38 def self.gsub(/<\/?[^>]*>/, '') # unless self.nil? end |
#to_class_name ⇒ String
Convert string like ‘category/list’ to CamelCase
17 18 19 20 21 |
# File 'lib/marfa/helpers/classes/string.rb', line 17 def to_class_name parts = downcase.split('/') parts.each(&:capitalize!) parts.join('').gsub(%r{-}, '') end |
#to_price! ⇒ String
Convert string price to preferred view
46 47 48 49 50 51 52 |
# File 'lib/marfa/helpers/classes/string.rb', line 46 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_underscore ⇒ String
Replaces all ‘/’ to ‘_’
9 10 11 |
# File 'lib/marfa/helpers/classes/string.rb', line 9 def to_underscore downcase.gsub(%r{/}, '_') end |
#to_url ⇒ String
Convert string to url part
27 28 29 30 31 32 |
# File 'lib/marfa/helpers/classes/string.rb', line 27 def to_url val = self. val = val.gsub(/[ —_\/]/, '-')#TODO: длинный пробел не работает val = val.gsub(/[+.,!?@#$%^&*()\[\]{}:;\/\\|<>"']/, '') #TODO: больше символов! val.downcase end |