mongocore 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: 0a86b7528d0b827a5b34c351f9307408f4ef5f3f
4
- data.tar.gz: d011a3b1ac8f3b2ff62bfe3ce256712c6b931dca
3
+ metadata.gz: e2b421c9156412c98e0da302d515db0283ba45ba
4
+ data.tar.gz: a14a5b1d90a91328eab16f8d2e6f083725ab8658
5
5
  SHA512:
6
- metadata.gz: 9fd4a39adb5aecb254e867455502ccf2debdace855f2a5dec3640643c70d5ea7122b1a367d291478a6c854b7d76c2673c6608df198cd9f1d02b7da165acf6e51
7
- data.tar.gz: f4e627193ae0cf71b37b975a1cd95c33c3270fd1b83ab3e72ccb11f63cfca874802f256112889f679a553c9c59076834f7fb5964a316a13a660321af5e5270d8
6
+ metadata.gz: c95e808a73109dbb45e4fb1538028c6cc453501a1fa90dcbae5ff3d0e2cc6ceb66fd9d0187a238dde05ca82df04e91ee50e438a3de73cc68198aba7eeff360b7
7
+ data.tar.gz: e904842600c7643bf831745ff487639e0b64b6221092eb650f9b7c8f657d90cbc526bbc5211ba3a75036cde2ae777d9ecf85225af4e53a6ec26940f31948e519
data/README.md CHANGED
@@ -21,7 +21,7 @@ Please read [the source code](https://github.com/fugroup/mongocore/tree/master/l
21
21
  | -------------------------------------- | ----- | ------- | ------------- |
22
22
  | [Mongoid](http://mongoid.com) | 256 | 14371 | 10590 |
23
23
  | [MongoMapper](http://mongomapper.com) | 91 | 200 | 4070 |
24
- | [Mongocore](http://mongocore.com) | 8 | 230 | 360 |
24
+ | [Mongocore](http://mongocore.com) | 8 | 256 | 454 |
25
25
 
26
26
  <br>
27
27
 
@@ -26,7 +26,7 @@ module Mongocore
26
26
 
27
27
  # Storing query and options. Sort and limit is stored in options
28
28
  s[:sort] ||= {}; s[:limit] ||= 0; s[:chain] ||= []; s[:source] ||= nil; s[:fields] ||= {}; s[:skip] ||= 0
29
- @query, @options, @store = normalize(q), o, s
29
+ @query, @options, @store = ids(transform(hashify(q))), o, s
30
30
 
31
31
  # Set up cache
32
32
  @cache = Mongocore::Cache.new(self)
@@ -34,22 +34,35 @@ module Mongocore
34
34
 
35
35
  # Find. Returns a Mongocore::Query
36
36
  def find(q = {}, o = {}, s = {})
37
- self.class.new(@model, @query.merge(q.is_a?(Hash) ? q : {:_id => q}), @options.merge(o), @store.merge(s))
37
+ self.class.new(@model, @query.merge(hashify(q)), @options.merge(o), @store.merge(s))
38
38
  end
39
39
  alias_method :where, :find
40
40
 
41
- # Normalize query
42
- def normalize(q)
43
- # Support find passing an ID
44
- q = {:_id => oid(q)} unless q.is_a?(Hash)
45
-
46
- # Support passing :id as :_id
47
- q[:_id] = q.delete(:id) if q[:id]
41
+ # Convert string query to hash
42
+ def hashify(q)
43
+ q.is_a?(Hash) ? q : {:_id => q}
44
+ end
48
45
 
49
- # Convert object ID's to BSON::ObjectIds
50
- q.each{|k, v| q[k] = @model.schema.convert(k, v) if k =~ /_id$/}
46
+ # Setup query, replace :id with :_id, set up ObjectIds
47
+ def ids(h)
48
+ h.each do |k, v|
49
+ case v
50
+ when Hash
51
+ # Call hashes recursively
52
+ ids(transform(v))
53
+ when Array
54
+ # Return mapped array or recurse hashes
55
+ h[k] = v.map{|r| r.is_a?(Hash) ? ids(transform(r)) : oid(r)}
56
+ else
57
+ # Convert to object ID if applicable
58
+ h[k] = oid(v) if v.is_a?(String)
59
+ end
60
+ end
61
+ end
51
62
 
52
- q
63
+ # Transform :id to :_id
64
+ def transform(h)
65
+ h.transform_keys!{|k| k == :id ? :_id : k}
53
66
  end
54
67
 
55
68
  # Cursor
@@ -172,11 +185,14 @@ module Mongocore
172
185
  @key ||= "#{@model}#{@query.sort}#{@options.sort}#{@store.values}"
173
186
  end
174
187
 
175
- # String id to BSON::ObjectId, or create a new by passing nothing or nil
176
- def oid(id = nil)
177
- return id if id.is_a?(BSON::ObjectId)
178
- return BSON::ObjectId.new if !id
179
- BSON::ObjectId.from_string(id) rescue id
188
+ # Schema short cut for oid
189
+ def oid(k)
190
+ @model.schema.oid(k)
191
+ end
192
+
193
+ # Schema short cut for oid?
194
+ def oid?(k)
195
+ @model.schema.oid?(k)
180
196
  end
181
197
 
182
198
  # Call and return the scope if it exists
@@ -46,22 +46,39 @@ module Mongocore
46
46
  # Convert type if val and schema type is set
47
47
  def convert(key, val)
48
48
  return nil if val.nil?
49
- type = @keys[key][:type].to_sym rescue :string
50
-
51
- # Convert to the same type as in the schema
52
- if type == :string then val.to_s
53
- elsif type == :integer then val.to_i
54
- elsif type == :float then val.to_f
55
- elsif type == :boolean then val.to_s.to_bool
56
- elsif type == :object_id
57
- # Convert string id to BSON::ObjectId
58
- val.each{|k, v| val[k] = BSON::ObjectId.from_string(v) if v.is_a?(String)} if val.is_a?(Hash)
59
- BSON::ObjectId.from_string(val) rescue val
49
+ case find_type(key)
50
+ when :string
51
+ val.to_s
52
+ when :integer
53
+ val.to_i
54
+ when :float
55
+ val.to_f
56
+ when :boolean
57
+ val.to_s.to_bool
58
+ when :object_id
59
+ oid(val)
60
60
  else
61
61
  val
62
62
  end
63
63
  end
64
64
 
65
+ # Find type as defined in schema
66
+ def find_type(key)
67
+ @keys[key][:type].to_sym rescue :string
68
+ end
69
+
70
+ # This key is a BSON::Object ID?
71
+ def oid?(key)
72
+ find_type(key) === :object_id
73
+ end
74
+
75
+ # Convert to BSON::ObjectId
76
+ def oid(id = nil)
77
+ return id if id.is_a?(BSON::ObjectId)
78
+ return BSON::ObjectId.new if !id
79
+ BSON::ObjectId.from_string(id) rescue id
80
+ end
81
+
65
82
  # # # # # # # # #
66
83
  # Templates for foreign key, many-associations and scopes.
67
84
  #
data/lib/mongocore.rb CHANGED
@@ -7,7 +7,7 @@ require 'mongo'
7
7
  require 'request_store'
8
8
 
9
9
  module Mongocore
10
- VERSION = '0.2.0'
10
+ VERSION = '0.2.1'
11
11
 
12
12
  # # # # # #
13
13
  # Mongocore Ruby Database Driver.
data/mongocore.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongocore'
3
- s.version = '0.2.0'
4
- s.date = '2017-10-25'
3
+ s.version = '0.2.1'
4
+ s.date = '2017-10-27'
5
5
  s.summary = "MongoDB ORM implementation on top of the Ruby MongoDB driver"
6
6
  s.description = "Does validations, associations, scopes, filters, pagination, counter cache, request cache, and nested queries. Using a YAML schema file, which supports default values, data types, and security levels for each key."
7
7
  s.authors = ["Fugroup Limited"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongocore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fugroup Limited
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-25 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo