Module: Wpxf::Utility::Text

Defined in:
lib/wpxf/utility/text.rb

Overview

Provides helper methods for text based operations.

Class Method Summary collapse

Class Method Details

.alpha_ranges(casing) ⇒ Array

Returns a range of alpha characters in the matching casing.

Parameters:

  • casing (Symbol)

    the casing to use for the alpha characters. Possible values are :mixed, :upper and :lower.

Returns:

  • (Array)

    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.

Parameters:

  • value (String)

    the string to hexify.

Returns:

  • (String)

    the 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.

Parameters:

  • value (String)

    the value to hash.

Returns:

  • (String)

    the MD5 hash.



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.

Parameters:

  • length (Integer)

    the number of characters to include.

  • casing (Symbol) (defaults to: :mixed)

    the casing to use for the alpha characters. Possible values are :mixed, :upper and :lower.

Returns:

  • (String)

    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.

Parameters:

  • length (Integer)

    the number of characters to include.

  • casing (Symbol) (defaults to: :mixed)

    the casing to use for the alpha characters. Possible values are :mixed, :upper and :lower.

Returns:

  • (String)

    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_emailString

Generate a random e-mail address.

Returns:

  • (String)

    the 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_monthString

Generate a random month name.

Returns:

  • (String)

    the 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.

Parameters:

  • length (Integer)

    the number of characters to include.

  • allow_leading_zero (Boolean) (defaults to: false)

    if set to true, will allow a number starting with zero.

Returns:

  • (String)

    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