Module: MongoPopulator::Random
- Included in:
- MongoPopulator
- Defined in:
- lib/mongo_populator/random.rb
Overview
This module adds several methods for generating random data which can be called directly on Populator.
Constant Summary collapse
- WORDS =
%w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat)
Instance Method Summary collapse
-
#array(*values) ⇒ Object
Simply pass the values back out as a MongoArray.
-
#dictionary(dict) ⇒ Object
Simply pass the values back out as a MongoDictionary.
-
#embed(total, template) ⇒ Object
Create n embedded documents from a template hash.
-
#interpret_value(value) ⇒ Object
If an array or range is passed, a random value will be selected to match.
-
#items(total, arr = nil) ⇒ Object
Generate a given number of items, or for a range, generate a random number of items within that range, using values in array, or random words.
-
#paragraphs(total) ⇒ Object
Generate a given number of paragraphs.
-
#sentences(total) ⇒ Object
Generate a given number of sentences.
-
#skip ⇒ Object
Because the mongo gem sets NULL for a value of ‘nil` instead of skipping the field altogether, we need a way to suppress a field from a doc so we don’t surprise anyone.
-
#value_in_range(range) ⇒ Object
Pick a random value out of a given range.
-
#words(total) ⇒ Object
Generate a given number of words.
Instance Method Details
#array(*values) ⇒ Object
Simply pass the values back out as a MongoArray
65 66 67 68 69 70 |
# File 'lib/mongo_populator/random.rb', line 65 def array(*values) if values.map {|e| e.class}.include?(MongoSkip) raise StandardError, "#skip method is not a permitted argument to #array" end MongoArray.new(values) end |
#dictionary(dict) ⇒ Object
Simply pass the values back out as a MongoDictionary
73 74 75 76 77 78 79 80 |
# File 'lib/mongo_populator/random.rb', line 73 def dictionary(dict) if dict.values.map {|e| e.class}.include?(MongoSkip) raise StandardError, "#skip method is not a permitted value in #dictionary" end md = MongoDictionary.new() dict.each {|k,v| md[k]=v} md end |
#embed(total, template) ⇒ Object
Create n embedded documents from a template hash
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongo_populator/random.rb', line 91 def (total, template) out = MongoArray.new (1..interpret_value(total)).map do md = MongoDictionary.new template.each_pair { |k,v| iv = interpret_value(v) md[k] = iv unless iv.is_a?(MongoSkip) } out << md end out end |
#interpret_value(value) ⇒ Object
If an array or range is passed, a random value will be selected to match. All other values are simply returned.
106 107 108 109 110 111 112 113 |
# File 'lib/mongo_populator/random.rb', line 106 def interpret_value(value) case value when MongoArray then value when Array then value[rand(value.size)] when Range then value_in_range(value) else value end end |
#items(total, arr = nil) ⇒ Object
Generate a given number of items, or for a range, generate a random number of items within that range, using values in array, or random words. Returns MongoArray. Resulting items should be a unique set, therefore if minimum number requested exceeds number of items available, provide fewer items.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mongo_populator/random.rb', line 44 def items(total, arr=nil) if arr && arr.map{|e| e.class}.include?(MongoSkip) raise StandardError, "#skip method is not permitted in the #items array argument" end # limit returned size to arr size if arr is not large enough min = total.is_a?(Range) ? total.first : total if arr total = (min <= arr.size) ? total : arr.size end out = MongoArray.new target = interpret_value(total) until out.size == target do out << (arr ? arr[rand(arr.size)] : words(1)) out.uniq! end out end |
#paragraphs(total) ⇒ Object
Generate a given number of paragraphs. If a range is passed, it will generate a random number of paragraphs within that range.
34 35 36 37 38 |
# File 'lib/mongo_populator/random.rb', line 34 def paragraphs(total) (1..interpret_value(total)).map do sentences(3..8).capitalize end.join("\n\n") end |
#sentences(total) ⇒ Object
Generate a given number of sentences. If a range is passed, it will generate a random number of sentences within that range.
26 27 28 29 30 |
# File 'lib/mongo_populator/random.rb', line 26 def sentences(total) (1..interpret_value(total)).map do words(5..20).capitalize end.join('. ') end |
#skip ⇒ Object
Because the mongo gem sets NULL for a value of ‘nil` instead of skipping the field altogether, we need a way to suppress a field from a doc so we don’t surprise anyone. See #build_records in factory.rb for how this is done in parent documents, and #embed in this file for how it is done in embedded documents.
86 87 88 |
# File 'lib/mongo_populator/random.rb', line 86 def skip() MongoSkip.new end |
#value_in_range(range) ⇒ Object
Pick a random value out of a given range.
9 10 11 12 13 14 15 16 |
# File 'lib/mongo_populator/random.rb', line 9 def value_in_range(range) case range.first when Integer then number_in_range(range) when Time then time_in_range(range) when Date then date_in_range(range) else range.to_a[rand(range.to_a.size)] end end |