Module: Wpxf::Utility::Text
- Defined in:
- lib/wpxf/utility/text.rb
Overview
Provides helper methods for text based operations.
Class Method Summary collapse
-
.alpha_ranges(casing) ⇒ Array
A range of alpha characters in the matching casing.
-
.hexify_string(value) ⇒ String
Convert each byte of a string to its hexadecimal value and concantenate them together, to provide a hexadecimal string.
-
.md5(value) ⇒ String
Generate an MD5 hash of a string.
-
.rand_alpha(length, casing = :mixed) ⇒ String
Generate a random alpha string.
-
.rand_alphanumeric(length, casing = :mixed) ⇒ String
Generate a random alphanumeric string.
-
.rand_email ⇒ String
Generate a random e-mail address.
-
.rand_month ⇒ String
Generate a random month name.
-
.rand_numeric(length, allow_leading_zero = false) ⇒ String
Generate a random numeric string.
Class Method Details
.alpha_ranges(casing) ⇒ Array
Returns a range of alpha characters in the matching casing.
41 42 43 44 45 46 47 48 49 |
# File 'lib/wpxf/utility/text.rb', line 41 def self.alpha_ranges(casing) if casing == :mixed [*'A'..'Z', *'a'..'z'] elsif casing == :upper [*'A'..'Z'] elsif casing == :lower [*'a'..'z'] end end |
.hexify_string(value) ⇒ String
Convert each byte of a string to its hexadecimal value and concantenate them together, to provide a hexadecimal string.
76 77 78 |
# File 'lib/wpxf/utility/text.rb', line 76 def self.hexify_string(value) value.each_byte.map { |b| b.to_s(16) }.join end |
.md5(value) ⇒ String
Generate an MD5 hash of a string.
54 55 56 57 58 |
# File 'lib/wpxf/utility/text.rb', line 54 def self.md5(value) digest = Digest::MD5.new digest.update(value) digest.hexdigest end |
.rand_alpha(length, casing = :mixed) ⇒ String
Generate a random alpha string.
34 35 36 |
# File 'lib/wpxf/utility/text.rb', line 34 def self.rand_alpha(length, casing = :mixed) Array.new(length) { alpha_ranges(casing).sample }.join end |
.rand_alphanumeric(length, casing = :mixed) ⇒ String
Generate a random alphanumeric string.
24 25 26 27 |
# File 'lib/wpxf/utility/text.rb', line 24 def self.rand_alphanumeric(length, casing = :mixed) range = [*'0'..'9'] + alpha_ranges(casing) Array.new(length) { range.sample }.join end |
.rand_email ⇒ String
Generate a random e-mail address.
62 63 64 |
# File 'lib/wpxf/utility/text.rb', line 62 def self.rand_email "#{rand_alpha(rand(5..10))}@#{rand_alpha(rand(5..10))}.com" end |
.rand_month ⇒ String
Generate a random month name.
68 69 70 |
# File 'lib/wpxf/utility/text.rb', line 68 def self.rand_month %w[january february march april june july august september october november december].sample end |
.rand_numeric(length, allow_leading_zero = false) ⇒ String
Generate a random numeric string.
13 14 15 16 17 |
# File 'lib/wpxf/utility/text.rb', line 13 def self.rand_numeric(length, allow_leading_zero = false) value = Array.new(length) { [*'0'..'9'].sample }.join value[0] = [*'1'..'9'].sample unless allow_leading_zero value end |