Class: String

Inherits:
Object show all
Defined in:
lib/propr/random/string.rb,
lib/propr/shrink/string.rb

Defined Under Namespace

Modules: Characters

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random(options = {}, m = Propr::Random) ⇒ String

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/propr/random/string.rb', line 29

def random(options = {}, m = Propr::Random)
  min     = options[:min] || 0
  max     = options[:max] || 10
  options = Hash[center: min].merge(options)
  charset = Characters.of(options.fetch(:charset, :print))

  m.bind(Integer.random(options.merge(min: min, max: max))) do |size|
    m.bind(m.sequence(size.times.map { charset.random })) do |chars|
      m.unit(chars.join)
    end
  end
end

Instance Method Details

#shrinkArray<String>

Returns:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/propr/shrink/string.rb', line 3

def shrink
  case size
  when 0 then []
  when 1
    shrunken = []
    shrunken << downcase if self =~ /[[:upper:]]/
    shrunken << " " if self =~ /(?! )\s/
    shrunken << "a" if self =~ /[b-z]/
    shrunken << "A" if self =~ /[B-Z]/
    shrunken << "0" if self =~ /[1-9]/
    shrunken << ""
    shrunken
  else
    split(//).shrink.map(&:join)
  end
end