Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/screenplay/datatype-extensions.rb
Overview
Adds a few extra methods to the standard String
Instance Method Summary collapse
- #camel_case ⇒ Object
-
#normalize ⇒ Object
Normalizes a string, remove diacritics (accents).
-
#numeric? ⇒ Boolean
Returns true if a string is numeric.
- #replace_vars(input) ⇒ Object
- #replace_vars!(input, sep_char = '.') ⇒ Object
- #snake_case ⇒ Object
-
#strip_quotes ⇒ Object
Strips single or double quotes at the start and end of the given string.
- #truncate(max_length, ellipses = '...') ⇒ Object
Instance Method Details
#camel_case ⇒ Object
143 144 145 |
# File 'lib/screenplay/datatype-extensions.rb', line 143 def camel_case self.split('_').collect(&:capitalize).join end |
#normalize ⇒ Object
Normalizes a string, remove diacritics (accents)
116 117 118 119 120 |
# File 'lib/screenplay/datatype-extensions.rb', line 116 def normalize tr( "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž", "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz") end |
#numeric? ⇒ Boolean
Returns true if a string is numeric.
106 107 108 |
# File 'lib/screenplay/datatype-extensions.rb', line 106 def numeric? self.to_i.to_s == self end |
#replace_vars(input) ⇒ Object
133 134 135 136 137 |
# File 'lib/screenplay/datatype-extensions.rb', line 133 def replace_vars(input) result = self.dup result.replace_vars!(input) return result end |
#replace_vars!(input, sep_char = '.') ⇒ Object
126 127 128 129 130 131 |
# File 'lib/screenplay/datatype-extensions.rb', line 126 def replace_vars!(input, sep_char = '.') input = {} unless input.is_a?(Hash) self.gsub!(/\#({[\w#{Regexp.escape(sep_char)}]+})/) { | key | input.get_value_from_path(key.delete("{}#")) } end |
#snake_case ⇒ Object
139 140 141 |
# File 'lib/screenplay/datatype-extensions.rb', line 139 def snake_case self.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr('-', '_').downcase end |
#strip_quotes ⇒ Object
Strips single or double quotes at the start and end of the given string.
111 112 113 |
# File 'lib/screenplay/datatype-extensions.rb', line 111 def strip_quotes gsub(/\A['"]+|['"]+\Z/, '') end |
#truncate(max_length, ellipses = '...') ⇒ Object
122 123 124 |
# File 'lib/screenplay/datatype-extensions.rb', line 122 def truncate(max_length, ellipses = '...') (self.length > max_length) ? self.to_s[0..max_length].gsub(/[^\w]\w+\s*$/, '...') : self.to_s end |