Class: StringRandom

Inherits:
Object
  • Object
show all
Defined in:
lib/strrand.rb

Overview

strrand.rb: Generates a random string from a pattern

Author

tama <[email protected]>

StringRandom is derived from the String::Random written in Perl. See search.cpan.org/~steve/String-Random-0.22/

Example

string_random = StringRandom.new
string_random.random_pattern('CCcc!ccn')  #=> ZIop$ab1

refer to test/test_stringrandom.rb

Format

Regular expression syntax

*_regex methods use this rule.

The following regular expression elements are supported.

w

Alphanumeric + “_”.

d

Digits.

W

Printable characters other than those in w.

D

Printable characters other than those in d.

.

Printable characters.

[]

Character classes.

{}

Repetition.

*

Same as 0,.

+

Same as 1,

?

Same as 0,1.

Patterns

random_pattern and random_string methods use this rule.

The following patterns are pre-defined.

c

Any lowercase character [a-z]

C

Any uppercase character [A-Z]

n

Any digit [0-9]

!

A punctuation character [~‘!@$%^&*()-_+=

.

Any of the above

s

A “salt” character [A-Za-z0-9./]

b

Any binary data

Pattern can modify and add as bellow.

string_random['C'] = ['n']
string_random['A'] = Array('A'..'Z') | Array('a'..'z')

Pattern must be a flattened array that elements are one character. Other types cause undefined behavior(raise exception, success, etc…).

Constant Summary collapse

Upper =
Array('A'..'Z')
Lower =
Array('a'..'z')
Digit =
Array('0'..'9')
Punct =
[33..47, 58..64, 91..96, 123..126].map { |r| r.map { |val| val.chr } }.flatten
Any =
Upper | Lower | Digit | Punct
Salt =
Upper | Lower | Digit | ['.', '/']
Binary =
(0..255).map { |val| val.chr }
Pattern =

These are the regex-based patterns.

{
  # These are the regex-equivalents.
  '.'  => Any,
  '\d' => Digit,
  '\D' => Upper | Lower | Punct,
  '\w' => Upper | Lower | Digit | ['_'],
  '\W' => Punct.reject { |val| val == '_' },
  '\s' => [' ', "\t"],
  '\S' => Upper | Lower | Digit | Punct,

  # These are translated to their double quoted equivalents.
  '\t' => ["\t"],
  '\n' => ["\n"],
  '\r' => ["\r"],
  '\f' => ["\f"],
  '\a' => ["\a"],
  '\e' => ["\e"]
}
OldPattern =

These are the old patterns for random_pattern.

{
  'C' => Upper,
  'c' => Lower,
  'n' => Digit,
  '!' => Punct,
  '.' => Any,
  's' => Salt,
  'b' => Binary
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max = 10) ⇒ StringRandom

max is default length for creating random string



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/strrand.rb', line 122

def initialize(max = 10)
  @max   = max
  @map   = OldPattern.clone
  @regch = {
    "\\" => method(:regch_slash),
    '.'  => method(:regch_dot),
    '['  => method(:regch_bracket),
    '*'  => method(:regch_asterisk),
    '+'  => method(:regch_plus),
    '?'  => method(:regch_question),
    '|'  => method(:regch_pipe),
    '{'  => method(:regch_brace)
  }
end

Class Method Details

.random_regex(patterns) ⇒ Object

Singleton method version of random_regex.



99
100
101
# File 'lib/strrand.rb', line 99

def self.random_regex(patterns)
  StringRandom.new.random_regex(patterns)
end

.random_string(pattern, *pattern_list) ⇒ Object

Same as StringRandom#random_pattern if single argument. Optionally, references to lists containing other patterns can be passed to the function. Those lists will be used for 0 through 9 in the pattern.



109
110
111
112
113
114
115
116
117
# File 'lib/strrand.rb', line 109

def self.random_string(pattern, *pattern_list)
  string_random = StringRandom.new

  pattern_list.each_with_index do |new_pattern, i|
    string_random[i.to_s] = new_pattern
  end

  string_random.random_pattern(pattern)
end

Instance Method Details

#[](key) ⇒ Object

Returns a random string pattern



168
169
170
# File 'lib/strrand.rb', line 168

def [](key)
  @map[key]
end

#[]=(key, pattern) ⇒ Object

Adds a random string pattern

pattern must be flattened array



177
178
179
# File 'lib/strrand.rb', line 177

def []=(key, pattern)
  @map[key] = pattern
end

#random_pattern(patterns) ⇒ Object

Returns a random string based on the concatenation of all the pattern strings in the list.



155
156
157
158
159
160
161
162
163
# File 'lib/strrand.rb', line 155

def random_pattern(patterns)
  return _random_pattern(patterns) unless patterns.instance_of?(Array)

  result = []
  patterns.each do |pattern|
    result << _random_pattern(pattern)
  end
  result
end

#random_regex(patterns) ⇒ Object

Returns a random string that will match the regular expression passed in the list argument.



141
142
143
144
145
146
147
148
149
# File 'lib/strrand.rb', line 141

def random_regex(patterns)
  return _random_regex(patterns) unless patterns.instance_of?(Array)

  result = []
  patterns.each do |pattern|
    result << _random_regex(pattern)
  end
  result
end