module Faker::String
Constants
- BACKSLASH
- ESCAPEABLE_CHARS
- LETTERS
- LOWERS
- NUMBERS
- SPACES
- UPPERS
- WORD_CHARS
Public Instance Methods
from_regexp(exp)
click to toggle source
# File lib/ffakerer/string.rb, line 18 def from_regexp(exp) result = '' @last_token = nil # Drop surrounding /'s and split into characters tokens = exp.inspect[1...-1].split(//) until tokens.empty? result << process_token(tokens) end result end
Private Instance Methods
generate_token(token, tokens)
click to toggle source
# File lib/ffakerer/string.rb, line 64 def generate_token(token, tokens) case token when /\w/ then @last_token = [token] token when BACKSLASH then token = tokens.shift @last_token = ['\\', token] special(token) when '[' then set = [] while (ch = tokens.shift) != ']' set << ch end @last_token = ['['] + set + [']'] process_token([ArrayUtils.rand(join_escapes(set))]) end end
join_escapes(tokens)
click to toggle source
# File lib/ffakerer/string.rb, line 33 def join_escapes(tokens) result = [] while tokens.any? token = tokens.shift token << tokens.shift if token == BACKSLASH result << token end result end
process_token(tokens)
click to toggle source
# File lib/ffakerer/string.rb, line 43 def process_token(tokens) return '' if tokens.empty? token = tokens.shift case token when '?' then # TODO: Let ? generate nothong return '' # We already printed its target when '+' then tokens.unshift(token) if rand(2) == 1 # Leave the `+` on to run again return process_token(@last_token) # Run the last one at least once when '*' then tokens.unshift(token) if rand(2) == 1 # Leave the `*` on to run again return '' if rand(2) == 1 # Or maybe do nothing return process_token(@last_token) # Else run the last one again end generate_token token, tokens end
special(token)
click to toggle source
# File lib/ffakerer/string.rb, line 83 def special(token) case token when 'w' then WORD_CHARS.rand when 'd' then NUMBERS.rand when 's' then SPACES.rand when *ESCAPEABLE_CHARS then token end end