mongrep 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5213c7c449784c621feca34ded1df39c9b91dc2
4
- data.tar.gz: 1250f6b7535011f3a4e2c5627dae1fc8be128928
3
+ metadata.gz: fa6b0fce568b4484935274059fdba1ce626f70cf
4
+ data.tar.gz: 7fbc40777a0d8d414b725da61913d010a1fb8a29
5
5
  SHA512:
6
- metadata.gz: 0d288ca4f5d697ae1c439de27020239f402ae85858ef77ea55ae82c58b5287a77a8e400f73403be961dc05750e84537c2ad4012c7f2aa27d2a8b6ce989f8d486
7
- data.tar.gz: 2d4f527de3dbf265920709f3ca8eeb00575bed19f88377d0fff939e6aba6143d42b195708eec96d99f4489e73554aeeee27666ee68cbf7a858c418b70a625d4d
6
+ metadata.gz: fea422283238f29c5d8e193a27e73648c4d82ce88c665b2f911d9a295f92833455743b6b71c47f6d3939d77e82a81a95723bb62909d5c76860b9b00c8e7bfdc4
7
+ data.tar.gz: 7d6e2cab262ce206e7700e1a22665cca64fa4fa0e110eb855cb936332ca47fcd3ad84ce6c93e3b1b9e584478efde94c856b98e423dd171d9afef7a9ecb87808d
data/README.md CHANGED
@@ -17,7 +17,7 @@ And then execute:
17
17
 
18
18
  ## Documentation
19
19
 
20
- API docs are available at http://rubydoc.info/github/meso-unimpressed/mongrep
20
+ API docs are available at https://rubydocs.info/mongrep
21
21
 
22
22
  ## Development
23
23
 
@@ -10,8 +10,8 @@ module Mongrep
10
10
 
11
11
  attr_reader :model_class
12
12
  # @api private
13
- def initialize(collection_view, model_class)
14
- @model_class = model_class
13
+ def initialize(collection_view, &initialize_model)
14
+ @initialize_model = initialize_model
15
15
  @collection_view = collection_view
16
16
  end
17
17
 
@@ -45,7 +45,7 @@ module Mongrep
45
45
 
46
46
  begin
47
47
  @collection_view.each do |document|
48
- yield @model_class.new(document)
48
+ yield @initialize_model.call(document)
49
49
  end
50
50
  ensure
51
51
  @collection_view.close_query
@@ -32,14 +32,19 @@ module Mongrep
32
32
  @collection = mongo_client[collection_name]
33
33
  end
34
34
 
35
- # Derives the model class from the class name, requires and returns it
36
- # @return [Model.class] The model class for this repository
35
+ # Derives the model class from the class name and instanciates a model of
36
+ # that class with the given document. This is passed to a QueryResult
37
+ # which uses it to return model instances. It should be overridden when
38
+ # implementing polymorphism based on a field in the document.
39
+ # @return [Model] A model instance
37
40
  # @example
38
41
  # repository = Shop::ShoppingCarts.new(mongo_client)
39
- # repository.model_class #=> Shop::Models::ShoppingCart
40
- def model_class
42
+ # repository.initialize_model(document)
43
+ # #=> #<Shop::Models::ShoppingCart:... >
44
+ def initialize_model(document)
41
45
  model_name = self.class.name.demodulize.singularize
42
- Mongrep.models_namespace.const_get(model_name)
46
+ model_class = Mongrep.models_namespace.const_get(model_name)
47
+ model_class.new(document)
43
48
  end
44
49
 
45
50
  # Finds documents matching the given query
@@ -179,7 +184,10 @@ module Mongrep
179
184
 
180
185
  def execute_query(query_object, options)
181
186
  check_query_type!(query_object)
182
- QueryResult.new(collection.find(query_object.to_h, options), model_class)
187
+ QueryResult.new(
188
+ collection.find(query_object.to_h, options),
189
+ &method(:initialize_model)
190
+ )
183
191
  end
184
192
 
185
193
  def check_persistence!(model)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module Mongrep
3
3
  # The current version of this gem
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongrep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joakim Reinert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-08 00:00:00.000000000 Z
11
+ date: 2017-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler