mongocore 0.2.2 → 0.2.3

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: 28fcb4fb984516032b665605855fa978c7d8cda2
4
- data.tar.gz: ecdb708c8470c2aec2cd624bdc35457e1fedb547
3
+ metadata.gz: bc69ea47a26f1a85fd3519180999a21710800a44
4
+ data.tar.gz: 523eaba0048b6d6f887bf4b1647f2f6f1ba8ecb1
5
5
  SHA512:
6
- metadata.gz: 9bd1c5dc2e11c6d3422a1fef2ac7e6c7e7b2f169cc5016da379f6d5dbb42e60b86fb06a40a2f488bd7724dea34e19d79597be5dd1eb45fc9efe13f52e117a077
7
- data.tar.gz: ad17bff6efbe5f07828d5f0f452a53bc21af5108e878ae48fa54c34f86e59f18f80e7e34b5a5ce0a7e14f5ee2d4a9beee80636c016ca7723b4c87ad9da15d6d9
6
+ metadata.gz: 1129eb75394fa108c0c701bf2c8722c8cca9b886c4e3ff2d8eb1c9d496e98b46bb69af066489ae0f4158eb67394f2e1ea193a222544bed0f7f16831224db803d
7
+ data.tar.gz: 05f1db2a8a7077981fb1690906e8c0e328fc531ff740a2ad226b6fb3d819347e9fb4b1426edaa91da8d945656d39f1bab347d77fc447159df857962162b8259f
data/README.md CHANGED
@@ -239,6 +239,7 @@ keys:
239
239
 
240
240
  # Define the _id field for all your models. The id field (without _)
241
241
  # is an alias to _id, but always returns a string instead of a BSON::ObjectId
242
+ # Any object ids as strings will be automatically converted into ObjectIds
242
243
  # @desc: Describes the key, can be used for documentation.
243
244
  # @type: object_id, string, integer, float, boolean, time, hash, array
244
245
  # @default: the default value for the key when you call .new
@@ -39,6 +39,13 @@ keys:
39
39
  location_data:
40
40
  desc: Model location data
41
41
  type: hash
42
+ default: {}
43
+ read: all
44
+ write: user
45
+ lists:
46
+ desc: Model list
47
+ type: array
48
+ default: []
42
49
  read: all
43
50
  write: user
44
51
  reminders_sent:
@@ -221,11 +221,6 @@ module Mongocore
221
221
  @_id ? @_id.to_s : nil
222
222
  end
223
223
 
224
- # Replace _id with id, takes a hash
225
- def string_id(a)
226
- a.each{|k, v| a[k] = v.to_s if v.is_a?(BSON::ObjectId)}; a.delete(:_id); {:id => id}.merge(a)
227
- end
228
-
229
224
  # Print info about the instance
230
225
  def inspect
231
226
  "#<#{self.class} #{attributes.sort.map{|r| %{#{r[0]}: #{r[1].inspect}}}.join(', ')}>"
@@ -242,6 +237,11 @@ module Mongocore
242
237
  filter(type){one.send((@saved ? :update : :insert), attributes).ok?}
243
238
  end
244
239
 
240
+ # Replace _id with id, takes a hash
241
+ def string_id(a)
242
+ a.delete(:_id); {:id => id}.merge(a)
243
+ end
244
+
245
245
  end
246
246
 
247
247
 
@@ -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 = ids(transform(hashify(q))), o, s
29
+ @query, @options, @store = @model.schema.ids(hashify(q)), o, s
30
30
 
31
31
  # Set up cache
32
32
  @cache = Mongocore::Cache.new(self)
@@ -43,28 +43,6 @@ module Mongocore
43
43
  q.is_a?(Hash) ? q : {:_id => q}
44
44
  end
45
45
 
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)) : @model.schema.oid(r)}
56
- else
57
- # Convert to object ID if applicable
58
- h[k] = @model.schema.oid(v) if v.is_a?(String)
59
- end
60
- end
61
- end
62
-
63
- # Transform :id to :_id
64
- def transform(h)
65
- h.transform_keys!{|k| k == :id ? :_id : k}
66
- end
67
-
68
46
  # Cursor
69
47
  def cursor
70
48
  @collection.find(@query, @options).projection(@store[:fields]).skip(@store[:skip]).sort(@store[:sort]).limit(@store[:limit])
@@ -46,24 +46,41 @@ 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
- 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
- else
61
- val
49
+ case type(key)
50
+ when :string then val.to_s
51
+ when :integer then val.to_i
52
+ when :float then val.to_f
53
+ when :boolean then val.to_s.to_bool
54
+ when :object_id then oid(val)
55
+ when :array, :hash then ids(val)
56
+ else val
62
57
  end
63
58
  end
64
59
 
60
+ # Setup query, replace :id with :_id, set up object ids
61
+ def ids(h)
62
+ transform(h).each do |k, v|
63
+ case v
64
+ when Hash
65
+ # Call hashes recursively
66
+ ids(v)
67
+ when Array
68
+ # Return mapped array or recurse hashes
69
+ v.map!{|r| r.is_a?(Hash) ? ids(r) : oid(r)}
70
+ else
71
+ # Convert to object ID if applicable
72
+ h[k] = oid(v) if v.is_a?(String)
73
+ end
74
+ end
75
+ end
76
+
77
+ # Transform :id to _id or id to object id
78
+ def transform(e)
79
+ e.is_a?(Hash) ? e.transform_keys!{|k| k == :id ? :_id : k} : e.map!{|r| oid(r)}
80
+ end
81
+
65
82
  # Find type as defined in schema
66
- def find_type(key)
83
+ def type(key)
67
84
  @keys[key][:type].to_sym rescue :string
68
85
  end
69
86
 
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.2'
10
+ VERSION = '0.2.3'
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.2'
4
- s.date = '2017-10-27'
3
+ s.version = '0.2.3'
4
+ s.date = '2017-10-28'
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.2
4
+ version: 0.2.3
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-27 00:00:00.000000000 Z
11
+ date: 2017-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo