mongocore 0.4.5 → 0.5.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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +1 -1
- data/config/db/schema/model.yml +5 -0
- data/lib/mongocore.rb +1 -1
- data/lib/mongocore/document.rb +28 -23
- data/lib/mongocore/query.rb +12 -5
- data/lib/mongocore/schema.rb +11 -5
- data/mongocore.gemspec +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74fdbe4ae3a48d20051b091780897353f615973e
|
4
|
+
data.tar.gz: b608caf9da396253e7024d84dfdff74547f01058
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d42222537d1c66933894c3c527742e690db6dc6acfe82e72e2394ce9192624c66d1f0c39ba2503f573fcab92e32dcd78790a88f3bd26f179aa94820ec5e18f52
|
7
|
+
data.tar.gz: '0287ca2b41a01ca0b2ff079d5e457212690b3c5cb011ce3f4d4078a833b402b1bbebc5350e4da506d9f606c1025ec30cee610c3f5e0b1a2708f4d7aaab334157'
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -263,7 +263,7 @@ keys:
|
|
263
263
|
# is an alias to _id, but always returns a string instead of a BSON::ObjectId
|
264
264
|
# Any object ids as strings will be automatically converted into ObjectIds
|
265
265
|
# @desc: Describes the key, can be used for documentation.
|
266
|
-
# @type: object_id, string, integer, float, boolean, time, hash, array
|
266
|
+
# @type: object_id, string, integer, float, boolean, time, binary, hash, array
|
267
267
|
# @default: the default value for the key when you call .new
|
268
268
|
# @read: access level for read: all, user, owner, dev, admin, super, app
|
269
269
|
# @write: access level for write. Returns nil if no access, as on read
|
data/config/db/schema/model.yml
CHANGED
data/lib/mongocore.rb
CHANGED
data/lib/mongocore/document.rb
CHANGED
@@ -50,12 +50,12 @@ module Mongocore
|
|
50
50
|
#
|
51
51
|
def initialize(a = {})
|
52
52
|
|
53
|
+
# Storing original state for dirty tracking
|
54
|
+
@original = {}
|
55
|
+
|
53
56
|
# Store attributes.
|
54
57
|
self.attributes = @_id ? a : self.class.schema.defaults.merge(a)
|
55
58
|
|
56
|
-
# Storing original state for dirty tracking.
|
57
|
-
@original = self.attributes
|
58
|
-
|
59
59
|
# The _id is a BSON object, create new unless it exists
|
60
60
|
@_id ? @persisted = true : @_id = BSON::ObjectId.new
|
61
61
|
|
@@ -95,12 +95,12 @@ module Mongocore
|
|
95
95
|
|
96
96
|
# Reload the document from db and update attributes
|
97
97
|
def reload
|
98
|
-
one.first
|
98
|
+
m = one.first; self.attributes = m.attributes; reset!; self
|
99
99
|
end
|
100
100
|
|
101
101
|
# Set the timestamps if enabled
|
102
102
|
def timestamps
|
103
|
-
t = Time.now.utc;
|
103
|
+
t = Time.now.utc; write(:updated_at, t); write(:created_at, t) if unsaved?
|
104
104
|
end
|
105
105
|
|
106
106
|
# Reset internals
|
@@ -142,7 +142,7 @@ module Mongocore
|
|
142
142
|
@errors.clear; self.class.filters.valid?(self)
|
143
143
|
end
|
144
144
|
|
145
|
-
# Available filters are :save, :
|
145
|
+
# Available filters are :save, :delete
|
146
146
|
def run(filter, key = nil)
|
147
147
|
self.class.filters.run(self, filter, key)
|
148
148
|
end
|
@@ -177,12 +177,7 @@ module Mongocore
|
|
177
177
|
|
178
178
|
# Get attribute if access
|
179
179
|
def read(key)
|
180
|
-
self.class.access.read?((key = key.to_sym))
|
181
|
-
end
|
182
|
-
|
183
|
-
# Get attribute
|
184
|
-
def read!(key)
|
185
|
-
instance_variable_get("@#{key}")
|
180
|
+
read!(key) if self.class.access.read?((key = key.to_sym))
|
186
181
|
end
|
187
182
|
|
188
183
|
# Set attribute if access
|
@@ -190,20 +185,20 @@ module Mongocore
|
|
190
185
|
return nil unless self.class.access.write?((key = key.to_sym))
|
191
186
|
|
192
187
|
# Convert to type as in schema yml
|
193
|
-
v = self.class.schema.
|
194
|
-
|
195
|
-
|
196
|
-
|
188
|
+
v = self.class.schema.set(key, val)
|
189
|
+
|
190
|
+
if @changes
|
191
|
+
# Record change for dirty attributes
|
192
|
+
@changes[key] = [@original[key], v] if v != read!(key)
|
193
|
+
else
|
194
|
+
# Store original values for dirty tracking support
|
195
|
+
@original[key] = v
|
196
|
+
end
|
197
197
|
|
198
198
|
# Write attribute
|
199
199
|
write!(key, v)
|
200
200
|
end
|
201
201
|
|
202
|
-
# Set attribute
|
203
|
-
def write!(key, v)
|
204
|
-
instance_variable_set("@#{key}", v)
|
205
|
-
end
|
206
|
-
|
207
202
|
# Dynamically read or write attributes
|
208
203
|
def method_missing(name, *args, &block)
|
209
204
|
# Extract name and write mode
|
@@ -255,6 +250,16 @@ module Mongocore
|
|
255
250
|
a.delete(:_id); {:id => id}.merge(a)
|
256
251
|
end
|
257
252
|
|
253
|
+
# Set attribute
|
254
|
+
def write!(key, v)
|
255
|
+
instance_variable_set("@#{key}", v)
|
256
|
+
end
|
257
|
+
|
258
|
+
# Get attribute
|
259
|
+
def read!(key)
|
260
|
+
instance_variable_get("@#{key}")
|
261
|
+
end
|
262
|
+
|
258
263
|
end
|
259
264
|
|
260
265
|
|
@@ -318,7 +323,7 @@ module Mongocore
|
|
318
323
|
|
319
324
|
# Insert
|
320
325
|
def insert(a = {}, o = {})
|
321
|
-
new(a)
|
326
|
+
r = new(a); r.save(o); r
|
322
327
|
end
|
323
328
|
alias_method :create, :insert
|
324
329
|
|
@@ -347,7 +352,7 @@ module Mongocore
|
|
347
352
|
# After, before and validation filters
|
348
353
|
# Pass a method name as symbol or a block
|
349
354
|
#
|
350
|
-
# Possible events for after and before are :save, :
|
355
|
+
# Possible events for after and before are :save, :delete
|
351
356
|
#
|
352
357
|
|
353
358
|
# After
|
data/lib/mongocore/query.rb
CHANGED
@@ -170,22 +170,26 @@ module Mongocore
|
|
170
170
|
|
171
171
|
# Sort
|
172
172
|
def sort(o = {})
|
173
|
-
|
173
|
+
(@options[:sort] ||= {}).merge!(o)
|
174
|
+
find(@query, @options)
|
174
175
|
end
|
175
176
|
|
176
177
|
# Limit
|
177
178
|
def limit(n = 1)
|
178
|
-
|
179
|
+
@options[:limit] = n
|
180
|
+
find(@query, @options)
|
179
181
|
end
|
180
182
|
|
181
183
|
# Skip
|
182
184
|
def skip(n = 0)
|
183
|
-
|
185
|
+
@options[:skip] = n
|
186
|
+
find(@query, @options)
|
184
187
|
end
|
185
188
|
|
186
189
|
# Projection
|
187
190
|
def projection(o = {})
|
188
|
-
|
191
|
+
@options[:projection].merge!(o)
|
192
|
+
find(@query, @options)
|
189
193
|
end
|
190
194
|
alias_method :fields, :projection
|
191
195
|
|
@@ -201,7 +205,10 @@ module Mongocore
|
|
201
205
|
|
202
206
|
# Call and return the scope if it exists
|
203
207
|
def method_missing(name, *arguments, &block)
|
204
|
-
|
208
|
+
if @model.schema.scopes.has_key?(name)
|
209
|
+
@options[:chain] << name
|
210
|
+
return @model.send(name, @query, @options)
|
211
|
+
end
|
205
212
|
super
|
206
213
|
end
|
207
214
|
|
data/lib/mongocore/schema.rb
CHANGED
@@ -46,15 +46,20 @@ module Mongocore
|
|
46
46
|
# Get time
|
47
47
|
def time(val)
|
48
48
|
case val
|
49
|
-
when Date then val.to_time.utc
|
50
|
-
when String then Time.parse(val).utc
|
51
49
|
when Time then val.utc
|
50
|
+
when String then Time.parse(val).utc
|
51
|
+
when Date, DateTime then val.to_time.utc
|
52
52
|
else nil
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
# Convert
|
57
|
-
def
|
56
|
+
# Convert to binary
|
57
|
+
def bin(val)
|
58
|
+
val.is_a?(BSON::Binary) ? val : BSON::Binary.new(val)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Set value based on type
|
62
|
+
def set(key, val)
|
58
63
|
return nil if val.nil?
|
59
64
|
case type(key)
|
60
65
|
when :string then val.to_s
|
@@ -64,6 +69,7 @@ module Mongocore
|
|
64
69
|
when :boolean then val.to_s.to_bool
|
65
70
|
when :object_id then oid(val)
|
66
71
|
when :array, :hash then ids(val)
|
72
|
+
when :binary then bin(val)
|
67
73
|
else val
|
68
74
|
end
|
69
75
|
end
|
@@ -103,7 +109,7 @@ module Mongocore
|
|
103
109
|
end
|
104
110
|
|
105
111
|
# # # # # # # # #
|
106
|
-
# Templates for foreign key, many-associations and scopes
|
112
|
+
# Templates for foreign key, many-associations and scopes
|
107
113
|
#
|
108
114
|
|
109
115
|
# Foreign keys
|
data/mongocore.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongocore'
|
3
|
-
s.version = '0.
|
4
|
-
s.date = '2018-01-
|
3
|
+
s.version = '0.5.0'
|
4
|
+
s.date = '2018-01-24'
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fugroup Limited
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongo
|