populate-me 0.11.0 → 0.12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: faa748585887ab4c87b9a96b0fcbc1cb819933840e5ed916a3455ea677c10ada
4
- data.tar.gz: '0548e4f13d04c8f9a5375029fc11e6cfdd00b7759b2a6b5ba171151640ee5159'
3
+ metadata.gz: b2770ba89bcfa96ad56baafdf141c67891317044051f44b117a8e6b438f7c432
4
+ data.tar.gz: f076278f23d91e5c59c141b39698c34a9083c5861ba01f41a3692d587bc0b73b
5
5
  SHA512:
6
- metadata.gz: 3244bbeb680e2d0ffea098719cc228623115666b498330651563672455c2495cfd3955270943f4b0020fc54780e876dd644a9025b0ff7f325eb016586f75cc8f
7
- data.tar.gz: 3489e40c4e16e1b75c005d09937c535de3eef8d52edf8de9b1aa54a43d71a7a8304a7e80ebaaec2486da4b344b28752b1451f82e224f2014cc00d74a0e417118
6
+ metadata.gz: f952a4160015147d54bab9a504f189613521e33342561e95ae33710cb2c3929a9a9512a96d6b0ed2812571a3faa7c972f3b550556884e44c9c0cd7df19922ed0
7
+ data.tar.gz: cd10b6ec3d88432f6255803150c6c1b129b0ffab6a6736d92243238976cfbf295413ea5a1cd9251029edfd798cdead3ac98fcbf51cd3f415a0011702ada41c5b
@@ -76,11 +76,16 @@ module PopulateMe
76
76
  end
77
77
 
78
78
  def admin_get id
79
+ return self.admin_get_multiple(id) if id.is_a?(Array)
79
80
  self.cast do
80
- documents.find{|doc| doc[id_string_key]==id }
81
+ documents.find{|doc| doc[id_string_key] == id }
81
82
  end
82
83
  end
83
84
 
85
+ def admin_get_multiple ids, o={sort: nil}
86
+ self.admin_find(o.merge(query: {id_string_key => {'$in' => ids.uniq.compact}}))
87
+ end
88
+
84
89
  def admin_find o={}
85
90
  o[:query] ||= {}
86
91
  docs = self.cast{documents}.find_all do |d|
@@ -88,11 +88,19 @@ module PopulateMe
88
88
  end
89
89
 
90
90
  def admin_get theid
91
+ return self.admin_get_multiple(theid) if theid.is_a?(Array)
91
92
  theid = string_or_object_id theid
92
93
  self.cast{ collection.find({id_string_key => theid}).first }
93
94
  end
94
95
  alias_method :[], :admin_get
95
96
 
97
+ def admin_get_multiple theids, o={sort: nil}
98
+ theids = theids.uniq.compact.map{|theid| string_or_object_id(theid) }
99
+ self.admin_find(o.merge({
100
+ query: {id_string_key => {'$in' => theids} }
101
+ }))
102
+ end
103
+
96
104
  def admin_find o={}
97
105
  query = o.delete(:query) || {}
98
106
  o[:sort] ||= @current_sort
@@ -1,4 +1,4 @@
1
1
  module PopulateMe
2
- VERSION = '0.11.0'
2
+ VERSION = '0.12.0'
3
3
  end
4
4
 
@@ -126,6 +126,19 @@ describe 'PopulateMe::Mongo' do
126
126
  assert_equal "regular", LowFish.admin_get(regular_fish_id).name
127
127
  end
128
128
 
129
+ it "Should get multiple items" do
130
+ # Order is not respected
131
+ alpha_id = LowFish.new(name: "alpha").perform_create
132
+ beta_id = LowFish.new(name: "beta").perform_create
133
+ gamma_id = LowFish.new(name: "gamma").perform_create
134
+ items = LowFish.admin_get([alpha_id, beta_id, nil, alpha_id])
135
+ assert items.is_a?(Array)
136
+ assert_equal 2, items.count
137
+ refute_nil items.find{|i| i.id == alpha_id}
138
+ refute_nil items.find{|i| i.id == beta_id}
139
+ assert_nil items.find{|i| i.id == gamma_id}
140
+ end
141
+
129
142
  it 'Should have the [] shortcut for admin_get' do
130
143
  LowFish.collection.insert_one(_id: 42, name: "H2G2")
131
144
  assert_equal LowFish[42], LowFish.admin_get(42)
@@ -138,7 +151,7 @@ describe 'PopulateMe::Mongo' do
138
151
  LowFish.collection.insert_one(_id: 40, name: "Bran")
139
152
  items = LowFish.admin_find
140
153
  assert items.is_a?(Array)
141
- assert 4, items.count
154
+ assert_equal 4, items.count
142
155
  assert_equal 10, items[0].id
143
156
  items = LowFish.admin_find query: {name: 'Bran'}
144
157
  assert items.is_a?(Array)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: populate-me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickael Riga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-09 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: web-utils