Module: Genny::String

Defined in:
lib/genny/string.rb

Constant Summary collapse

@@formats =
{}
@@hints =
[]

Class Method Summary collapse

Class Method Details

.format(format, &block) ⇒ Object



8
9
10
# File 'lib/genny/string.rb', line 8

def self.format(format, &block)
  @@formats[format] = block
end

.genny(opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/genny/string.rb', line 16

def self.genny(opts = {})
  opts = Genny.symbolize(opts)
  viable_formats = @@formats.keys & [*opts[:format]]
  return @@formats[viable_formats.sample].call(opts) unless viable_formats.empty?
  if (re = ::Regexp.new(opts[:format]) rescue false)
    begin
      return re.extend(Genny::Regexp).genny
    rescue
    end
  end
  guess = @@hints.inject(nil) { |guess, hint|
    break guess if !(guess = hint.call(opts)).nil?
  } if opts[:hint]
  return guess unless guess.nil?
  opts[:minLength] ||= 10
  opts[:maxLength] ||= 10
  length = Random.rand(opts[:maxLength] - opts[:minLength] + 1) + opts[:minLength]
  ('a'..'z').to_a.sample(length).join
end

.hint(&block) ⇒ Object



12
13
14
# File 'lib/genny/string.rb', line 12

def self.hint(&block)
  @@hints.push(block)
end