Class: Randall
Overview
A random value generator.
Constant Summary collapse
- @@reparser =
The regexp parser for generating random String.
RandallRegExpParser.new
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
The
Class
for which the receiver should generate instances. -
#value ⇒ Object
readonly
The generated random value.
Instance Method Summary collapse
-
#initialize(type = Integer, opts = {}) ⇒ Randall
constructor
Creates a Randall object.
-
#next ⇒ Object
(also: #n)
Generate another value under current type and restrictions.
-
#rand(type, opts = {}) ⇒ Object
(also: #r)
Generates value for given type and restrictions.
-
#restrict(opts) ⇒ Object
Set the restriction.
-
#v ⇒ Object
Same as
self.value
.
Methods included from RandallInstanceSugar
#close_to, #from, #greater_than, #in_range, #less_than, #match, #of, #that, #to, #with_arguments, #with_size
Constructor Details
#initialize(type = Integer, opts = {}) ⇒ Randall
Creates a Randall object.
- type
-
Type of the values the Randall object should generate.
- opts
-
Optional. The restrictions the generated random values should
satisfy. See documents of +Randall+ for restrictions of each supported types, and how this arguments is specified.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/randall.rb', line 41 def initialize(type = Integer, opts = {}) @type = type self.restrict opts @worker = Fiber.new do loop do if String == @type @value = g_str elsif Integer == @type @value = g_int elsif Float == @type @value = g_float elsif Array == @type @value = g_array elsif Hash == @type @value = g_hash elsif Class === @type @value = g_any else @value = nil end Fiber.yield @value end end end |
Instance Attribute Details
#type ⇒ Object (readonly)
The Class
for which the receiver should generate instances.
29 30 31 |
# File 'lib/randall.rb', line 29 def type @type end |
#value ⇒ Object (readonly)
The generated random value. It is changed after rand
or next
are called.
27 28 29 |
# File 'lib/randall.rb', line 27 def value @value end |
Instance Method Details
#next ⇒ Object Also known as: n
Generate another value under current type and restrictions.
95 96 97 |
# File 'lib/randall.rb', line 95 def next @worker.resume end |
#rand(type, opts = {}) ⇒ Object Also known as: r
Generates value for given type and restrictions. This method is used when you want to reuse a Randall object to generate values for alternate type.
77 78 79 80 81 82 |
# File 'lib/randall.rb', line 77 def rand(type, opts = {}) @type = type self.restrict opts @worker.resume end |
#restrict(opts) ⇒ Object
Set the restriction. You use this method to change the restrictions the generated values should satisfy.
87 88 89 90 91 92 |
# File 'lib/randall.rb', line 87 def restrict(opts) @options = opts parse_regexp if @type == String self end |
#v ⇒ Object
Same as self.value
.
70 71 72 |
# File 'lib/randall.rb', line 70 def v self.value end |