Class: Randgen

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

Constant Summary collapse

WORDS_PER_SENTENCE =
3..20
SENTENCES_PER_PARAGRAPH =
3..8

Class Method Summary collapse

Class Method Details

.alpha_numeric(options = {}) ⇒ Object



29
30
31
# File 'lib/randexp/randgen.rb', line 29

def self.alpha_numeric(options = {})
  [char, digit].pick
end

.bool(options = {}) ⇒ Object



5
6
7
# File 'lib/randexp/randgen.rb', line 5

def self.bool(options = {})
  ['true', 'false'].pick
end

.char(options = {}) ⇒ Object



17
18
19
# File 'lib/randexp/randgen.rb', line 17

def self.char(options = {})
  [lchar, uchar].pick
end

.digit(options = {}) ⇒ Object



25
26
27
# File 'lib/randexp/randgen.rb', line 25

def self.digit(options = {})
  ('0'..'9').to_a.pick
end

.email(options = {}) ⇒ Object



58
59
60
61
# File 'lib/randexp/randgen.rb', line 58

def self.email(options = {})
  domain = options.fetch(:domain, "#{word(options)}.example.org")
  "#{word(options)}@#{domain}"
end

.first_name(options = {}) ⇒ Object



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

def self.first_name(options = {})
  RealName.first_names(options).pick
end

.lchar(options = {}) ⇒ Object



9
10
11
# File 'lib/randexp/randgen.rb', line 9

def self.lchar(options = {})
  ('a'..'z').to_a.pick
end

.name(options = {}) ⇒ Object



54
55
56
# File 'lib/randexp/randgen.rb', line 54

def self.name(options = {})
  "#{first_name(options)} #{surname(options)}"
end

.paragraph(options = {}) ⇒ Object



67
68
69
# File 'lib/randexp/randgen.rb', line 67

def self.paragraph(options = {})
  ((options[:length] || SENTENCES_PER_PARAGRAPH.pick).of { sentence } * ".  ") + "."
end

.phone_number(options = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/randexp/randgen.rb', line 71

def self.phone_number(options = {})
  case options[:length]
  when 7  then  /\d{3}-\d{4}/.gen
  when 10 then  /\d{3}-\d{3}-\d{4}/.gen
  else          /(\d{3}-)?\d{3}-\d{4}/.gen
  end
end

.sentence(options = {}) ⇒ Object



63
64
65
# File 'lib/randexp/randgen.rb', line 63

def self.sentence(options = {})
  ((options[:length] || WORDS_PER_SENTENCE.pick).of { word } * " ").capitalize
end

.surname(options = {}) ⇒ Object Also known as: last_name



46
47
48
# File 'lib/randexp/randgen.rb', line 46

def self.surname(options = {})
  RealName.surnames(options).pick
end

.uchar(options = {}) ⇒ Object



13
14
15
# File 'lib/randexp/randgen.rb', line 13

def self.uchar(options = {})
  ('A'..'Z').to_a.pick
end

.whitespace(options = {}) ⇒ Object



21
22
23
# File 'lib/randexp/randgen.rb', line 21

def self.whitespace(options = {})
  ["\t", "\n", "\r", "\f"].pick
end

.word(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/randexp/randgen.rb', line 33

def self.word(options = {})
  begin
    word = Randexp::Dictionary.words(options).pick
    word ||= options[:length].of { alpha_numeric }.join
  end until word =~ /^\w+$/

  word
end