Skip to content

Commit

Permalink
adding Populator.words to randomly generate words
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Aug 29, 2008
1 parent bb0a8c1 commit d05b29a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* adding Populate.words to fetch some random words

*0.1.0* (August 27th, 2008)

* initial release
9 changes: 9 additions & 0 deletions README
Expand Up @@ -56,6 +56,15 @@ Passing an range or array of values will randomly select one.
This will create 1000 to 5000 men or women with the annual income
between 10,000 and 200,000.

There are a few methods to generate random data as well:

Populator.words(3) # generates 3 random words separated by spaces
Populator.words(10..20) # generates between 10 and 20 random words

If you want fancier data, try the Faker gem.

http://faker.rubyforge.org


== Development

Expand Down
2 changes: 2 additions & 0 deletions TODO
Expand Up @@ -2,6 +2,7 @@ Features
- add random_foo method to record for randomly generating content
- randomly fill every column if no block is passed to populate
- add "words" method to generate a number of words
- limit number of queries per insert

Possible
- iterate through array instead of selecting at random to ensure all values are chosen
Expand All @@ -12,6 +13,7 @@ Performance
- ruby-prof to see where the bottlenecks are
- benchmark to find faster solutions
- ensure instatiating the model is really that much of a performance hit.
- don't use an insert for every nested factory - do it all at once at the end

Support Environments
- sqlite 2
Expand Down
2 changes: 1 addition & 1 deletion lib/populator/factory.rb
Expand Up @@ -2,7 +2,7 @@ module Populator
class Factory
def initialize(model_class, amount)
@model_class = model_class
@amount = amount.kind_of?(Integer) ? amount : Populator.value_in_range(amount)
@amount = Populator.interpret_value(amount)
@records = []
end

Expand Down
14 changes: 14 additions & 0 deletions lib/populator/random.rb
@@ -1,5 +1,7 @@
module Populator
module Random
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 cum 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)

def value_in_range(range)
case range.first
when Integer then number_in_range(range)
Expand All @@ -9,6 +11,18 @@ def value_in_range(range)
end
end

def words(total)
(1..interpret_value(total)).map { WORDS.rand }.join(' ')
end

def interpret_value(value)
case value
when Array then value.rand
when Range then value_in_range(value)
else value
end
end

private

def time_in_range(range)
Expand Down
12 changes: 1 addition & 11 deletions lib/populator/record.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(model_class, id)
@columns.each do |column|
self.instance_eval <<-EOS
def #{column}=(value)
@attributes[:#{column}] = interpret_value(value)
@attributes[:#{column}] = Populator.interpret_value(value)
end
def #{column}
Expand All @@ -23,15 +23,5 @@ def attribute_values
@attributes[column.to_sym]
end
end

private

def interpret_value(value)
case value
when Array then value.rand
when Range then Populator.value_in_range(value)
else value
end
end
end
end
9 changes: 9 additions & 0 deletions spec/populator/random_spec.rb
Expand Up @@ -29,4 +29,13 @@
Kernel.expects(:rand).with(5).returns(2)
Populator.value_in_range('a'..'e').should == 'c'
end

it "should pick 3 random words" do
Populator.words(3).split.should have(3).records
end

it "should pick a random number of random words" do
Populator.expects(:rand).with(5).returns(3)
Populator.words(10...15).split.should have(13).records
end
end

0 comments on commit d05b29a

Please sign in to comment.