diff --git a/CHANGELOG b/CHANGELOG index 1adf34a..0b11386 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* adding Populate.words to fetch some random words + *0.1.0* (August 27th, 2008) * initial release diff --git a/README b/README index 35ac46c..727f7e6 100644 --- a/README +++ b/README @@ -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 diff --git a/TODO b/TODO index f3e1c15..a728800 100644 --- a/TODO +++ b/TODO @@ -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 @@ -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 diff --git a/lib/populator/factory.rb b/lib/populator/factory.rb index f6a9270..007a555 100644 --- a/lib/populator/factory.rb +++ b/lib/populator/factory.rb @@ -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 diff --git a/lib/populator/random.rb b/lib/populator/random.rb index c535a85..6c14bad 100644 --- a/lib/populator/random.rb +++ b/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) @@ -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) diff --git a/lib/populator/record.rb b/lib/populator/record.rb index 05f5d92..b45a468 100644 --- a/lib/populator/record.rb +++ b/lib/populator/record.rb @@ -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} @@ -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 diff --git a/spec/populator/random_spec.rb b/spec/populator/random_spec.rb index 2a43fa0..58648cf 100644 --- a/spec/populator/random_spec.rb +++ b/spec/populator/random_spec.rb @@ -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