mongo_populator 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- # 0.1.1 (September 15th, 2011)
1
+ # 0.2.0 (September 14th, 2011)
2
+
3
+ * an attribute set to nil (directly or randomly via an array) will not appear in the resulting document.
4
+
5
+ # 0.1.1 (September 14th, 2011)
2
6
 
3
7
  * remove required minimum RubyGems version
4
8
 
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongo_populator (0.1.1)
5
+ mongo (~> 1.3.1)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ bson (1.3.1)
11
+ diff-lcs (1.1.3)
12
+ metaclass (0.0.1)
13
+ mocha (0.10.0)
14
+ metaclass (~> 0.0.1)
15
+ mongo (1.3.1)
16
+ bson (>= 1.3.1)
17
+ rspec (2.6.0)
18
+ rspec-core (~> 2.6.0)
19
+ rspec-expectations (~> 2.6.0)
20
+ rspec-mocks (~> 2.6.0)
21
+ rspec-core (2.6.4)
22
+ rspec-expectations (2.6.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.6.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ mocha (~> 0.10.0)
31
+ mongo
32
+ mongo_populator!
33
+ rspec (~> 2.6.0)
data/README.md CHANGED
@@ -15,7 +15,7 @@ This gem adds a "populate" method to a `Mongo::Collection`. Pass the number of d
15
15
 
16
16
  db = Mongo::Connection.new("localhost", 27017).db("test-db")
17
17
 
18
- article_collection = db.collection('article')
18
+ article_collection = db.collection('articles')
19
19
  article_collection.populate(100) do |article|
20
20
  article.title = MongoPopulator.words(4..6).capitalize
21
21
  article.slug = article.title.downcase.tr(' ','-')
@@ -24,9 +24,11 @@ This gem adds a "populate" method to a `Mongo::Collection`. Pass the number of d
24
24
  article.body = MongoPopulator.paragraphs(5..7)
25
25
  end
26
26
 
27
+ ### Relationships
28
+
27
29
  Unlike the original Populator, we are letting MongoDB set each ObjectId. This makes setting up relationships only slightly more laborious.
28
30
 
29
- article_collection = db.collection('article')
31
+ article_collection = db.collection('articles')
30
32
  article_collection.populate(100) do |article|
31
33
  ...
32
34
  end
@@ -40,14 +42,20 @@ Unlike the original Populator, we are letting MongoDB set each ObjectId. This ma
40
42
  comment.article = article_ids
41
43
  end
42
44
 
43
- That will create an average of 10 comments for each article. Embedded documents are not yet supported.
45
+ That will create an average of 10 related comments for each article.
46
+
47
+ ### Shuffle through set of values
44
48
 
45
49
  Passing a range or array of values will randomly select one.
46
50
 
51
+ ...
47
52
  person.gender = ['male', 'female']
53
+ ...
48
54
 
49
55
  This will create 1000 to 5000 men or women with the annual income between 10,000 and 200,000.
50
56
 
57
+ ### Fake values
58
+
51
59
  If you need to generate fake data, there are a few methods to do this.
52
60
 
53
61
  MongoPopulator.words(3) # generates 3 random words separated by spaces
@@ -57,12 +65,32 @@ If you need to generate fake data, there are a few methods to do this.
57
65
 
58
66
  For fancier data generation, try the [Faker gem](http://faker.rubyforge.org).
59
67
 
68
+ ### JSON-specific structures
69
+
60
70
  To persist arrays in your documents, use either #items to save a certain number of items randomly selected from a set, or #array to save a specific array.
61
71
 
62
72
  MongoPopulator.items(1..5, %w(ape bear cat dog elephant firefox)) # populates array with provided terms
63
73
  MongoPopulator.items(10..20) # populates array with random words
64
74
  MongoPopulator.array('red', 'green', 'blue') # saves `['red', 'green', 'blue']` exactly
65
75
 
76
+ Setting an attribute to nil prevents that attribute being set.
77
+
78
+ ...
79
+ address.state = nil if address.country != "United States"
80
+ ...
81
+
82
+ So, to support conditional setting of an attribute, pass it an array with one or more nils as elements.
83
+
84
+ ...
85
+ user.creds = ['M.D.', 'J.D.', 'N.D.', nil, nil]
86
+ ...
87
+
88
+ ~40% of users will not have credentials.
89
+
90
+ ## TODO
91
+
92
+ * Support singular and multiple embedded documents
93
+
66
94
  ## Development
67
95
 
68
96
  Problems or questions? Add an [issue on GitHub](https://github.com/bak/mongo_populator/issues) or fork the project and send a pull request.
@@ -19,7 +19,7 @@ module MongoPopulator
19
19
 
20
20
  def method_missing(sym, *args, &block)
21
21
  name = sym.to_s
22
- if name.include? '='
22
+ if name.include?('=') && args.first
23
23
  @attributes[name.sub('=', '').to_sym] = MongoPopulator.interpret_value(args.first)
24
24
  else
25
25
  @attributes[sym]
@@ -1,5 +1,5 @@
1
- = Running Populator Specs
1
+ # Running Populator Specs
2
2
 
3
3
  To run the specs, make sure you have mongo running on localhost port 27017, and
4
4
 
5
- rake spec
5
+ rake spec
@@ -21,6 +21,13 @@ describe MongoPopulator::CollectionAdditions do
21
21
  @collection.distinct('name').last.should == "foo"
22
22
  end
23
23
 
24
+ it "should not set an attribute if passed nil" do
25
+ @collection.populate(1) do |record|
26
+ record.monkey = nil
27
+ end
28
+ @collection.distinct('monkey').should be_empty
29
+ end
30
+
24
31
  after(:each) do
25
32
  @collection.drop
26
33
  end
metadata CHANGED
@@ -1,20 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_populator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Benjamin Cullen-Kerney
9
- - Ryan Bates
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2011-09-14 00:00:00.000000000Z
12
+ date: 2011-09-15 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: mongo
17
- requirement: &70201274816400 !ruby/object:Gem::Requirement
16
+ requirement: &70136711178880 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: 1.3.1
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *70201274816400
24
+ version_requirements: *70136711178880
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: rspec
28
- requirement: &70201274815720 !ruby/object:Gem::Requirement
27
+ requirement: &70136711178220 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ~>
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: 2.6.0
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *70201274815720
35
+ version_requirements: *70136711178220
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: mocha
39
- requirement: &70201274815040 !ruby/object:Gem::Requirement
38
+ requirement: &70136711177400 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ~>
@@ -44,7 +43,7 @@ dependencies:
44
43
  version: 0.10.0
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *70201274815040
46
+ version_requirements: *70136711177400
48
47
  description: Mass populate MongoDB.
49
48
  email: ben.kerney -> gmail.com
50
49
  executables: []
@@ -61,10 +60,11 @@ files:
61
60
  - spec/mongo_populator/factory_spec.rb
62
61
  - spec/mongo_populator/random_spec.rb
63
62
  - spec/mongo_populator/record_spec.rb
64
- - spec/README.rdoc
63
+ - spec/README.md
65
64
  - spec/spec_helper.rb
66
65
  - CHANGELOG.md
67
66
  - Gemfile
67
+ - Gemfile.lock
68
68
  - LICENSE
69
69
  - Rakefile
70
70
  - README.md
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project: mongo_populator
91
- rubygems_version: 1.8.10
91
+ rubygems_version: 1.8.6
92
92
  signing_key:
93
93
  specification_version: 3
94
94
  summary: Mass populate MongoDB.