class FactoryHelper::String

Public Class Methods

random(length= 32) click to toggle source
# File lib/factory-helper/string.rb, line 5
def random(length= 32)
  utf8string select_a length
end

Private Class Methods

char_space_ratio() click to toggle source
# File lib/factory-helper/string.rb, line 32
def char_space_ratio
  [
    2, [3]*5, [5]*17, [7]*11, 11, 191,
  ].flatten.sample(:random => FactoryHelper::Config.random)
end
select_a(length) click to toggle source
# File lib/factory-helper/string.rb, line 11
def select_a length
  if length.class.included_modules.include? Enumerable
    select_a length.to_enum.to_a.sample(:random => FactoryHelper::Config.random)
  else
    length
  end
end
space_or_utf8_char(ratio) click to toggle source
# File lib/factory-helper/string.rb, line 26
def space_or_utf8_char ratio
  [
    32.chr(Encoding::UTF_8), [utf8character]* ratio
  ].flatten.sample(:random => FactoryHelper::Config.random)
end
utf8character() click to toggle source
# File lib/factory-helper/string.rb, line 38
def utf8character
  [
    FactoryHelper::Config.random.rand(59)+ 32,          # latin alphabet
    FactoryHelper::Config.random.rand(128),             # 7-bit ASCII
    FactoryHelper::Config.random.rand(0xd800),          # utf-8 codepoints below utf-16 surrogate halves
    FactoryHelper::Config.random.rand(0x102000)+ 0xe000 # utf-8 codepoints above utf-16 surrogate halves
  ].sample(:random => FactoryHelper::Config.random).chr(Encoding::UTF_8)
end
utf8string(length) click to toggle source
# File lib/factory-helper/string.rb, line 19
def utf8string(length)
  ratio= char_space_ratio
  (1..length.to_i).map do
    space_or_utf8_char ratio
  end.join
end