Class: Thoom::Strandom

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

Class Method Summary collapse

Class Method Details

.hex(len: 2) ⇒ Object



6
7
8
# File 'lib/strandom.rb', line 6

def hex(len: 2)
  SecureRandom.hex(len  / 2)
end

.rand(len: 0, type: :alnum, cust: []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/strandom.rb', line 10

def rand(len: 0, type: :alnum, cust: [])
  num = (48..57).to_a
  upper = (65..90).to_a
  lower = (97..122).to_a

  case type
  when :alpha
    range = upper + lower
  when :upper
    range = upper
  when :upnum
    range = num + upper
  when :lower
    range = lower
  when :lownum
    range = num + lower
  when :num
    range = num
  when :custom
    range = cust
  else
    range = num + upper + lower
  end

  # pulled from http://codereview.stackexchange.com/questions/15958
  if type == :cust
    ([nil] * len).map { range.sample }.join
  else
    ([nil] * len).map { range.sample.chr }.join
  end
end

.uuidObject



42
43
44
# File 'lib/strandom.rb', line 42

def uuid()
  SecureRandom.uuid
end