Class: Randgen
- Inherits:
-
Object
- Object
- Randgen
- Defined in:
- lib/randexp/randgen.rb
Constant Summary collapse
- WORDS_PER_SENTENCE =
3..20
- SENTENCES_PER_PARAGRAPH =
3..8
Class Method Summary collapse
- .alpha_numeric(options = {}) ⇒ Object
- .bool(options = {}) ⇒ Object
- .char(options = {}) ⇒ Object
- .digit(options = {}) ⇒ Object
- .email(options = {}) ⇒ Object
- .first_name(options = {}) ⇒ Object
- .lchar(options = {}) ⇒ Object
- .name(options = {}) ⇒ Object
- .paragraph(options = {}) ⇒ Object
- .phone_number(options = {}) ⇒ Object
- .sentence(options = {}) ⇒ Object
- .surname(options = {}) ⇒ Object (also: last_name)
- .uchar(options = {}) ⇒ Object
- .whitespace(options = {}) ⇒ Object
- .word(options = {}) ⇒ Object
Class Method Details
.alpha_numeric(options = {}) ⇒ Object
29 30 31 |
# File 'lib/randexp/randgen.rb', line 29 def self.alpha_numeric( = {}) [char, digit].pick end |
.bool(options = {}) ⇒ Object
5 6 7 |
# File 'lib/randexp/randgen.rb', line 5 def self.bool( = {}) ['true', 'false'].pick end |
.char(options = {}) ⇒ Object
17 18 19 |
# File 'lib/randexp/randgen.rb', line 17 def self.char( = {}) [lchar, uchar].pick end |
.digit(options = {}) ⇒ Object
25 26 27 |
# File 'lib/randexp/randgen.rb', line 25 def self.digit( = {}) ('0'..'9').to_a.pick end |
.email(options = {}) ⇒ Object
58 59 60 61 |
# File 'lib/randexp/randgen.rb', line 58 def self.email( = {}) domain = .fetch(:domain, "#{word()}.example.org") "#{word()}@#{domain}" end |
.first_name(options = {}) ⇒ Object
42 43 44 |
# File 'lib/randexp/randgen.rb', line 42 def self.first_name( = {}) RealName.first_names().pick end |
.lchar(options = {}) ⇒ Object
9 10 11 |
# File 'lib/randexp/randgen.rb', line 9 def self.lchar( = {}) ('a'..'z').to_a.pick end |
.name(options = {}) ⇒ Object
54 55 56 |
# File 'lib/randexp/randgen.rb', line 54 def self.name( = {}) "#{first_name()} #{surname()}" end |
.paragraph(options = {}) ⇒ Object
67 68 69 |
# File 'lib/randexp/randgen.rb', line 67 def self.paragraph( = {}) (([: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( = {}) case [: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( = {}) (([: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( = {}) RealName.surnames().pick end |
.uchar(options = {}) ⇒ Object
13 14 15 |
# File 'lib/randexp/randgen.rb', line 13 def self.uchar( = {}) ('A'..'Z').to_a.pick end |
.whitespace(options = {}) ⇒ Object
21 22 23 |
# File 'lib/randexp/randgen.rb', line 21 def self.whitespace( = {}) ["\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( = {}) begin word = Randexp::Dictionary.words().pick word ||= [:length].of { alpha_numeric }.join end until word =~ /^\w+$/ word end |