mongoid_fixtures 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.idea/encodings.xml +4 -0
- data/.idea/misc.xml +4 -0
- data/.idea/modules.xml +9 -0
- data/.idea/mongoid_fixtures.iml +223 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +6 -0
- data/.idea/workspace.xml +1291 -0
- data/.travis.yml +13 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +362 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/config.yml +6 -0
- data/lib/mongoid_fixtures/embed_utils.rb +36 -0
- data/lib/mongoid_fixtures/version.rb +3 -0
- data/lib/mongoid_fixtures.rb +151 -0
- data/mongoid_fixtures.gemspec +36 -0
- metadata +149 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Javier L. Velasquez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,362 @@
|
|
1
|
+
# MongoidFixtures
|
2
|
+
Fixtures for Ruby for Mongoid. No Rails needed!
|
3
|
+
|
4
|
+
[](https://codeclimate.com/github/nycjv321/mongoid_fixtures)
|
5
|
+
[](https://codeclimate.com/github/nycjv321/mongoid_fixtures/coverage)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mongoid_fixtures'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mongoid_fixtures
|
22
|
+
|
23
|
+
## Example Usage
|
24
|
+
|
25
|
+
1. Define some Mongoid Documents. The class structure is pretty silly and only meant to demonstrate the different modes.
|
26
|
+
|
27
|
+
class GeopoliticalDivision
|
28
|
+
include Mongoid::Document
|
29
|
+
field :name, type: String
|
30
|
+
field :time_zone, type: String
|
31
|
+
field :demonym, type: String
|
32
|
+
field :settled, type: Integer
|
33
|
+
field :consolidated, type: Integer
|
34
|
+
field :custom_attributes, type: Array
|
35
|
+
belongs_to :geo_uri_scheme
|
36
|
+
embeds_one :population
|
37
|
+
embeds_many :people
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
class Population
|
42
|
+
include Mongoid::Document
|
43
|
+
|
44
|
+
field :total, type: Integer
|
45
|
+
field :rank, type: Integer
|
46
|
+
field :density, type: String
|
47
|
+
field :msa, type: Integer
|
48
|
+
field :csa, type: Integer
|
49
|
+
field :source, type: String
|
50
|
+
embedded_in :geopolitical_division
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
class City < GeopoliticalDivision
|
55
|
+
include Mongoid::Document
|
56
|
+
belongs_to :state
|
57
|
+
end
|
58
|
+
|
59
|
+
class State < GeopoliticalDivision
|
60
|
+
include Mongoid::Document
|
61
|
+
|
62
|
+
field :motto, type: String
|
63
|
+
field :admission_to_union, type: String
|
64
|
+
|
65
|
+
has_many :cities
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
class GeoUriScheme
|
70
|
+
include Mongoid::Document
|
71
|
+
|
72
|
+
field :x, type: Float
|
73
|
+
field :y, type: Float
|
74
|
+
field :z, type: Float
|
75
|
+
|
76
|
+
alias :longitude :x
|
77
|
+
alias :latitude :y
|
78
|
+
alias :altitude :z
|
79
|
+
end
|
80
|
+
|
81
|
+
class Person
|
82
|
+
include Mongoid::Document
|
83
|
+
|
84
|
+
embedded_in :geopolitical_division
|
85
|
+
|
86
|
+
field :first_name, type: String
|
87
|
+
field :paternal_surname, type: String
|
88
|
+
field :born, type: Date
|
89
|
+
field :description, type: String
|
90
|
+
field :middle_name, type: String
|
91
|
+
field :suffix, type: String
|
92
|
+
field :died, type: Date
|
93
|
+
field :maternal_surname, type: String
|
94
|
+
field :nick_names, type: Array
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
2. Defin efixtures in /test/fixtures/ with a plural form of the class
|
99
|
+
|
100
|
+
/test/fixtures/cities.yml:
|
101
|
+
|
102
|
+
:new_york_city:
|
103
|
+
name: New York City
|
104
|
+
:population:
|
105
|
+
total: 9_000_000
|
106
|
+
rank: 1
|
107
|
+
density: 10,756.0/km2
|
108
|
+
msa: 20,092,883
|
109
|
+
csa: 23,632,722
|
110
|
+
source: U.S. Census (2014)
|
111
|
+
demonym: New Yorker
|
112
|
+
settled: 1624
|
113
|
+
consolidated: 1989
|
114
|
+
custom_attributes:
|
115
|
+
- boroughs: ['Manhattan', 'The Bronx', 'Brooklyn', 'Queens', 'Staten Island']
|
116
|
+
:people:
|
117
|
+
- first_name: Kareem
|
118
|
+
paternal_surname: Abdul-Jabbar
|
119
|
+
born: April 16, 1947
|
120
|
+
description: basketball player
|
121
|
+
- first_name: Robert
|
122
|
+
middle_name: John
|
123
|
+
paternal_surname: Downey
|
124
|
+
suffix: Jr.
|
125
|
+
born: April 4, 1965
|
126
|
+
description: an American actor whose career has included critical and popular success in his youth, followed by a period of substance abuse and legal troubles, and a resurgence of commercial success in middle age.
|
127
|
+
- first_name: Edward
|
128
|
+
middle_name: Kennedy
|
129
|
+
paternal_surname: Ellington
|
130
|
+
nick_names:
|
131
|
+
- Duke
|
132
|
+
born: April 29, 1899
|
133
|
+
died: May 24, 1974
|
134
|
+
description: was an American composer, pianist and bandleader of jazz orchestras. He led his orchestra from 1923 until his death, his career spanning over 50 years.
|
135
|
+
- first_name: Christopher
|
136
|
+
middle_name: George
|
137
|
+
paternal_surname: Latore
|
138
|
+
maternal_surname: Wallace
|
139
|
+
nick_names:
|
140
|
+
- The Notorious B.I.G.
|
141
|
+
- B.I.G.
|
142
|
+
- Biggie Smalls
|
143
|
+
- Big Poppa
|
144
|
+
- Frank White
|
145
|
+
- King of New York
|
146
|
+
born: May 21, 1972
|
147
|
+
died: March 9, 1997
|
148
|
+
description: was an American rapper. Wallace is consistently ranked as one of the greatest rappers ever and one of the most influential rappers of all time.
|
149
|
+
state: :new_york
|
150
|
+
geo_uri_scheme: :new_york_city
|
151
|
+
:terrytown:
|
152
|
+
name: Terrytown
|
153
|
+
state: :louisiana
|
154
|
+
:population:
|
155
|
+
total: 24_000
|
156
|
+
geo_uri_scheme: :terrytown
|
157
|
+
|
158
|
+
/test/fixtures/states.yml:
|
159
|
+
|
160
|
+
:new_york:
|
161
|
+
name: New York
|
162
|
+
:population:
|
163
|
+
total: 20_000_000
|
164
|
+
demonym: New Yorker
|
165
|
+
capital: Albany
|
166
|
+
motto: Excelsior
|
167
|
+
admission_to_union: July 26th, 1788
|
168
|
+
time_zone: "Eastern: UTC -5/-4"
|
169
|
+
:louisiana:
|
170
|
+
name: Louisiana
|
171
|
+
:population:
|
172
|
+
total: 4_700_000
|
173
|
+
demonym: Louisianian
|
174
|
+
capital: Baton Rouge
|
175
|
+
motto: Union, Justice, Confidence
|
176
|
+
admission_to_union: April 30th, 1812
|
177
|
+
time_zone: "Central: UTC −6/−5"
|
178
|
+
|
179
|
+
|
180
|
+
/test/fixtures/geourischemes.yml
|
181
|
+
|
182
|
+
:terrytown:
|
183
|
+
x: -90.029444
|
184
|
+
y: 29.902222
|
185
|
+
z: 3.9624
|
186
|
+
:new_york_city:
|
187
|
+
x: -74.0059
|
188
|
+
y: 40.7127
|
189
|
+
z: 0.6096
|
190
|
+
|
191
|
+
|
192
|
+
You may notice attributes that represent relationships are automatically converted to
|
193
|
+
the corresponding ruby objects based on the provided id. Currently, this library supports
|
194
|
+
1-N referenced, 1-1 referenced, 1-1 embedded relationships, and 1-N embedded relationships.
|
195
|
+
If a relation already exists in the db, this library will return it instead of recreating it.
|
196
|
+
|
197
|
+
3. Invoke `MongoidFixtures::load(City)`
|
198
|
+
4. The above method invocation will load all test fixture instances of City objects defined
|
199
|
+
in /test/fixtures/cities.yml as well as dependent objects
|
200
|
+
5. Use your fixtures!
|
201
|
+
|
202
|
+
cities = MongoidFixtures::load(City)
|
203
|
+
puts cities # {:new_york_city=>#<City _id: 55ebc6b2e138231068000004, name: "New York City", time_zone: nil, demonym: "New Yorker", settled: 1624, consolidated: 1989, custom_attributes: [{"boroughs"=>["Manhattan", "The Bronx", "Brooklyn", "Queens", "Staten Island"]}], geo_uri_scheme_id: BSON::ObjectId('55ebc6b2e138231068000006'), _type: "City", state_id: BSON::ObjectId('55ebc6b2e138231068000000')>, :terrytown=>#<City _id: 55ebc6b2e13823106800000c, name: "Terrytown", time_zone: nil, demonym: nil, settled: nil, consolidated: nil, custom_attributes: nil, geo_uri_scheme_id: BSON::ObjectId('55ebc6b2e138231068000005'), _type: "City", state_id: BSON::ObjectId('55ebc6b2e138231068000002')>}
|
204
|
+
|
205
|
+
In the DB:
|
206
|
+
|
207
|
+
/* 0 */
|
208
|
+
{
|
209
|
+
"_id" : ObjectId("55eb4adae1382309c5000003"),
|
210
|
+
"x" : -90.029444,
|
211
|
+
"y" : 29.902222,
|
212
|
+
"z" : 3.9624
|
213
|
+
}
|
214
|
+
|
215
|
+
/* 1 */
|
216
|
+
{
|
217
|
+
"_id" : ObjectId("55eb4adae1382309c5000004"),
|
218
|
+
"x" : -74.0059,
|
219
|
+
"y" : 40.7127,
|
220
|
+
"z" : 0.6096
|
221
|
+
}
|
222
|
+
|
223
|
+
/* 0 */
|
224
|
+
{
|
225
|
+
"_id" : ObjectId("55ebc6b2e138231068000000"),
|
226
|
+
"_type" : "State",
|
227
|
+
"name" : "New York",
|
228
|
+
"population" : {
|
229
|
+
"_id" : ObjectId("55ebc6b2e138231068000001"),
|
230
|
+
"total" : 20000000
|
231
|
+
},
|
232
|
+
"demonym" : "New Yorker",
|
233
|
+
"capital" : "Albany",
|
234
|
+
"motto" : "Excelsior",
|
235
|
+
"admission_to_union" : "July 26th, 1788",
|
236
|
+
"time_zone" : "Eastern: UTC -5/-4"
|
237
|
+
}
|
238
|
+
|
239
|
+
/* 1 */
|
240
|
+
{
|
241
|
+
"_id" : ObjectId("55ebc6b2e138231068000002"),
|
242
|
+
"_type" : "State",
|
243
|
+
"name" : "Louisiana",
|
244
|
+
"population" : {
|
245
|
+
"_id" : ObjectId("55ebc6b2e138231068000003"),
|
246
|
+
"total" : 4700000
|
247
|
+
},
|
248
|
+
"demonym" : "Louisianian",
|
249
|
+
"capital" : "Baton Rouge",
|
250
|
+
"motto" : "Union, Justice, Confidence",
|
251
|
+
"admission_to_union" : "April 30th, 1812",
|
252
|
+
"time_zone" : "Central: UTC −6/−5"
|
253
|
+
}
|
254
|
+
|
255
|
+
/* 2 */
|
256
|
+
{
|
257
|
+
"_id" : ObjectId("55ebc6b2e138231068000004"),
|
258
|
+
"_type" : "City",
|
259
|
+
"name" : "New York City",
|
260
|
+
"population" : {
|
261
|
+
"_id" : ObjectId("55ebc6b2e138231068000007"),
|
262
|
+
"total" : 9000000,
|
263
|
+
"rank" : 1,
|
264
|
+
"density" : "10,756.0/km2",
|
265
|
+
"msa" : 20092883,
|
266
|
+
"csa" : 23632722,
|
267
|
+
"source" : "U.S. Census (2014)"
|
268
|
+
},
|
269
|
+
"demonym" : "New Yorker",
|
270
|
+
"settled" : 1624,
|
271
|
+
"consolidated" : 1989,
|
272
|
+
"custom_attributes" : [
|
273
|
+
{
|
274
|
+
"boroughs" : [
|
275
|
+
"Manhattan",
|
276
|
+
"The Bronx",
|
277
|
+
"Brooklyn",
|
278
|
+
"Queens",
|
279
|
+
"Staten Island"
|
280
|
+
]
|
281
|
+
}
|
282
|
+
],
|
283
|
+
"people" : [
|
284
|
+
{
|
285
|
+
"_id" : ObjectId("55ebc6b2e138231068000008"),
|
286
|
+
"first_name" : "Kareem",
|
287
|
+
"paternal_surname" : "Abdul-Jabbar",
|
288
|
+
"born" : ISODate("1947-04-16T00:00:00.000Z"),
|
289
|
+
"description" : "basketball player"
|
290
|
+
},
|
291
|
+
{
|
292
|
+
"_id" : ObjectId("55ebc6b2e138231068000009"),
|
293
|
+
"first_name" : "Robert",
|
294
|
+
"middle_name" : "John",
|
295
|
+
"paternal_surname" : "Downey",
|
296
|
+
"suffix" : "Jr.",
|
297
|
+
"born" : ISODate("1965-04-04T00:00:00.000Z"),
|
298
|
+
"description" : "an American actor whose career has included critical and popular success in his youth, followed by a period of substance abuse and legal troubles, and a resurgence of commercial success in middle age."
|
299
|
+
},
|
300
|
+
{
|
301
|
+
"_id" : ObjectId("55ebc6b2e13823106800000a"),
|
302
|
+
"first_name" : "Edward",
|
303
|
+
"middle_name" : "Kennedy",
|
304
|
+
"paternal_surname" : "Ellington",
|
305
|
+
"nick_names" : [
|
306
|
+
"Duke"
|
307
|
+
],
|
308
|
+
"born" : ISODate("1899-04-29T00:00:00.000Z"),
|
309
|
+
"died" : ISODate("1974-05-24T00:00:00.000Z"),
|
310
|
+
"description" : "was an American composer, pianist and bandleader of jazz orchestras. He led his orchestra from 1923 until his death, his career spanning over 50 years."
|
311
|
+
},
|
312
|
+
{
|
313
|
+
"_id" : ObjectId("55ebc6b2e13823106800000b"),
|
314
|
+
"first_name" : "Christopher",
|
315
|
+
"middle_name" : "George",
|
316
|
+
"paternal_surname" : "Latore",
|
317
|
+
"maternal_surname" : "Wallace",
|
318
|
+
"nick_names" : [
|
319
|
+
"The Notorious B.I.G.",
|
320
|
+
"B.I.G.",
|
321
|
+
"Biggie Smalls",
|
322
|
+
"Big Poppa",
|
323
|
+
"Frank White",
|
324
|
+
"King of New York"
|
325
|
+
],
|
326
|
+
"born" : ISODate("1972-05-21T00:00:00.000Z"),
|
327
|
+
"died" : ISODate("1997-03-09T00:00:00.000Z"),
|
328
|
+
"description" : "was an American rapper. Wallace is consistently ranked as one of the greatest rappers ever and one of the most influential rappers of all time."
|
329
|
+
}
|
330
|
+
],
|
331
|
+
"state_id" : ObjectId("55ebc6b2e138231068000000"),
|
332
|
+
"geo_uri_scheme_id" : ObjectId("55ebc6b2e138231068000006")
|
333
|
+
}
|
334
|
+
|
335
|
+
/* 3 */
|
336
|
+
{
|
337
|
+
"_id" : ObjectId("55ebc6b2e13823106800000c"),
|
338
|
+
"_type" : "City",
|
339
|
+
"name" : "Terrytown",
|
340
|
+
"state_id" : ObjectId("55ebc6b2e138231068000002"),
|
341
|
+
"population" : {
|
342
|
+
"_id" : ObjectId("55ebc6b2e13823106800000d"),
|
343
|
+
"total" : 24000
|
344
|
+
},
|
345
|
+
"geo_uri_scheme_id" : ObjectId("55ebc6b2e138231068000005")
|
346
|
+
}
|
347
|
+
|
348
|
+
## Development
|
349
|
+
|
350
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
351
|
+
|
352
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
353
|
+
|
354
|
+
## Contributing
|
355
|
+
|
356
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nycjv321/mongoid_fixtures.
|
357
|
+
|
358
|
+
|
359
|
+
## License
|
360
|
+
|
361
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
362
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mongoid_fixtures"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/config.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module EmbedUtils
|
2
|
+
def self.create_embedded_instance(clazz, hash, instance)
|
3
|
+
embed = clazz.new
|
4
|
+
hash.each do |key, value|
|
5
|
+
embed.send("#{key}=", value)
|
6
|
+
end
|
7
|
+
embed.send("#{find_embed_parent_class(embed)}=", instance)
|
8
|
+
embed
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find_embed_parent_class(embed)
|
12
|
+
relations = embed.relations
|
13
|
+
|
14
|
+
relations.each do |name, relation|
|
15
|
+
if relation.relation.eql? Mongoid::Relations::Embedded::In
|
16
|
+
return name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
raise 'Unable to find parent class'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.insert_embedded_ids(instance)
|
23
|
+
attributes = instance.attributes.select { |key, value| !key.to_s.eql?('_id') }
|
24
|
+
|
25
|
+
attributes.each do |key, value|
|
26
|
+
if attributes[key].is_a? Hash
|
27
|
+
unless instance.send(key)._id.nil?
|
28
|
+
attributes[key]['_id'] = instance.send(key)._id
|
29
|
+
end
|
30
|
+
else
|
31
|
+
attributes[key] = value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
attributes
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require_relative '../lib/mongoid_fixtures/version'
|
2
|
+
require 'yaml'
|
3
|
+
require 'singleton'
|
4
|
+
require 'linguistics'
|
5
|
+
require 'active_support/inflector'
|
6
|
+
require 'monkey_patches/module'
|
7
|
+
require_relative 'mongoid_fixtures/embed_utils'
|
8
|
+
|
9
|
+
module MongoidFixtures
|
10
|
+
|
11
|
+
class Loader
|
12
|
+
include Singleton
|
13
|
+
|
14
|
+
attr_accessor :fixtures, :path
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@fixtures = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.load
|
21
|
+
load_fixtures Dir[File.join(File.expand_path('../..', __FILE__), "#{path}/*.yml")]
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.load_fixtures(fixture_names)
|
25
|
+
fix = MongoidFixtures::Loader.instance
|
26
|
+
fixture_names.each do |fixture|
|
27
|
+
fix.fixtures[File.basename(fixture, '.*')] = YAML.load_file(fixture)
|
28
|
+
end
|
29
|
+
fix
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.path=(var)
|
33
|
+
Loader.instance.path = var
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.path
|
37
|
+
Loader.instance.path
|
38
|
+
end
|
39
|
+
end
|
40
|
+
:private
|
41
|
+
|
42
|
+
Linguistics.use(:en)
|
43
|
+
Loader::path = 'test/fixtures'
|
44
|
+
Loader::load
|
45
|
+
|
46
|
+
def self.load(clazz)
|
47
|
+
fixture_instances = Loader.instance.fixtures[clazz.to_s.downcase.en.plural] # get class name
|
48
|
+
instances = {}
|
49
|
+
if fixture_instances.nil?
|
50
|
+
raise "Could not find instances for #{clazz}"
|
51
|
+
end
|
52
|
+
fixture_instances.each do |key, fixture_instance|
|
53
|
+
instance = clazz.new
|
54
|
+
fields = fixture_instance.keys
|
55
|
+
fields.each do |field|
|
56
|
+
|
57
|
+
value = fixture_instance[field]
|
58
|
+
field_label = field.to_s.capitalize
|
59
|
+
field_clazz = Module.resolve_class_ignore_plurality(field_label)
|
60
|
+
|
61
|
+
# If the current value is a symbol then it represents another fixture.
|
62
|
+
# Find it and store its id
|
63
|
+
if value.is_a? Symbol or value.nil?
|
64
|
+
relations = instance.relations
|
65
|
+
if relations.include? field
|
66
|
+
if relations[field].relation.eql? Mongoid::Relations::Referenced::In or relations[field].relation.eql? Mongoid::Relations::Referenced::One
|
67
|
+
instance.send("#{field}=", self.load(field_clazz)[value])
|
68
|
+
else
|
69
|
+
# instance[field] = self.load(field_clazz)[value].id # embedded fields?
|
70
|
+
raise "#{instance} relationship not defined: #{relations[field].relation}"
|
71
|
+
end
|
72
|
+
else
|
73
|
+
raise "Symbol (#{value.nil? ? value : 'nil'}) doesn't reference relationship"
|
74
|
+
end
|
75
|
+
|
76
|
+
elsif value.is_a? Array
|
77
|
+
values = []
|
78
|
+
value.each do |v|
|
79
|
+
if field_clazz.nil?
|
80
|
+
values << v
|
81
|
+
else
|
82
|
+
values << EmbedUtils.create_embedded_instance(field_clazz, v, instance)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
if instance[field].nil?
|
86
|
+
instance[field] = []
|
87
|
+
end
|
88
|
+
instance[field].concat(values)
|
89
|
+
elsif value.is_a? Hash
|
90
|
+
# take hash convert it to object and serialize it
|
91
|
+
instance[field] = EmbedUtils.create_embedded_instance(field_clazz, value, instance)
|
92
|
+
# else just set the field
|
93
|
+
else
|
94
|
+
instance[field] = value
|
95
|
+
end
|
96
|
+
end
|
97
|
+
instances[key] = create_or_save_instance(instance) # store it based on its key name
|
98
|
+
end
|
99
|
+
instances
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.flatten_attributes(attributes)
|
103
|
+
flattened_attributes = {}
|
104
|
+
if attributes.is_a? String
|
105
|
+
return attributes
|
106
|
+
end
|
107
|
+
if attributes.is_a? Mongoid::Document
|
108
|
+
attributes.attributes.each do |name, attribute|
|
109
|
+
unless name.eql? '_id'
|
110
|
+
flattened_attributes["#{attributes.class.to_s.downcase}.#{name}"] = attribute
|
111
|
+
end
|
112
|
+
end
|
113
|
+
else
|
114
|
+
|
115
|
+
attributes.each do |key, values|
|
116
|
+
if values.is_a? Hash
|
117
|
+
values.each do |value, inner_value|
|
118
|
+
flattened_attributes["#{key}.#{value}"] = inner_value
|
119
|
+
end
|
120
|
+
elsif values.is_a? Mongoid::Document
|
121
|
+
values.attributes.each do |name, attribute|
|
122
|
+
unless name.eql? '_id'
|
123
|
+
flattened_attributes["#{values.class.to_s.downcase}.#{name}"] = values.send(name)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
elsif values.is_a? Array # Don't do anything
|
127
|
+
else
|
128
|
+
flattened_attributes[key] = values
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
flattened_attributes
|
133
|
+
end
|
134
|
+
:private
|
135
|
+
|
136
|
+
def self.create_or_save_instance(instance)
|
137
|
+
attributes = instance.attributes.select { |key, value| !key.to_s.eql?('_id') }
|
138
|
+
flattened_attributes = flatten_attributes(attributes)
|
139
|
+
if instance.class.where(flattened_attributes).exists?
|
140
|
+
instance = instance.class.where(flattened_attributes).first
|
141
|
+
else
|
142
|
+
EmbedUtils.insert_embedded_ids(instance)
|
143
|
+
instance.save! # auto serialize the document
|
144
|
+
end
|
145
|
+
instance
|
146
|
+
end
|
147
|
+
:private
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_fixtures/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'mongoid_fixtures'
|
8
|
+
spec.version = MongoidFixtures::VERSION
|
9
|
+
spec.authors = ['Javier L. Velasquez']
|
10
|
+
spec.email = ['nycjv321@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Provides mechanism to easily create fixtures with Mongoid'
|
13
|
+
spec.description = 'Provides mechanism to easily create fixtures with Mongoid'
|
14
|
+
spec.homepage = 'https://github.com/nycjv321/mongoid_fixtures'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
else
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rspec'
|
33
|
+
spec.add_dependency 'linguistics', '~> 2.0.4'
|
34
|
+
spec.add_dependency 'mongoid', '~> 4.0.2'
|
35
|
+
spec.add_dependency 'monkey_patches', '~> 0.0.3'
|
36
|
+
end
|