Class: Randomizer
- Inherits:
-
Object
- Object
- Randomizer
- Includes:
- Singleton
- Defined in:
- lib/factory_randomizer.rb
Class Method Summary collapse
-
.method_missing(symbol, *args) ⇒ Object
By default all methods are defined in the creator classes, so all method_missing calls are redirected to one of them.
Instance Method Summary collapse
-
#initialize ⇒ Randomizer
constructor
A new instance of Randomizer.
-
#random(symbol, args) ⇒ Object
Generates a random symbol using args by using all of the creator classes.
-
#register_creator(klass) ⇒ Object
Registers a creator class.
Constructor Details
#initialize ⇒ Randomizer
Returns a new instance of Randomizer.
7 8 9 |
# File 'lib/factory_randomizer.rb', line 7 def initialize @creators = [] end |
Class Method Details
.method_missing(symbol, *args) ⇒ Object
By default all methods are defined in the creator classes, so all method_missing calls are redirected to one of them
If a creator class defines random_email, Randomizer.email comes available
16 17 18 19 20 21 22 |
# File 'lib/factory_randomizer.rb', line 16 def self.method_missing(symbol, *args) # *args is an array with potentially a Hash as first entry and we # would like to pass that Hash... params = args.first || {} Randomizer.instance.send("random", symbol, params) end |
Instance Method Details
#random(symbol, args) ⇒ Object
Generates a random symbol using args by using all of the creator classes
31 32 33 34 35 36 37 |
# File 'lib/factory_randomizer.rb', line 31 def random(symbol, args) @creators.select{ |creator| creator.can_generate?(symbol) }.each do |creator| return creator.send("random_#{symbol}", args) end nil end |
#register_creator(klass) ⇒ Object
Registers a creator class
25 26 27 |
# File 'lib/factory_randomizer.rb', line 25 def register_creator(klass) @creators.push klass end |