Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/screenplay/datatype-extensions.rb

Overview

Adds a few extra methods to the standard String

Instance Method Summary collapse

Instance Method Details

#camel_caseObject



143
144
145
# File 'lib/screenplay/datatype-extensions.rb', line 143

def camel_case
	self.split('_').collect(&:capitalize).join
end

#normalizeObject

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.

Returns:



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_caseObject



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_quotesObject

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