mongocore 0.1.10 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/lib/mongocore/document.rb +13 -9
- data/lib/mongocore.rb +1 -1
- data/mongocore.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a86b7528d0b827a5b34c351f9307408f4ef5f3f
|
4
|
+
data.tar.gz: d011a3b1ac8f3b2ff62bfe3ce256712c6b931dca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fd4a39adb5aecb254e867455502ccf2debdace855f2a5dec3640643c70d5ea7122b1a367d291478a6c854b7d76c2673c6608df198cd9f1d02b7da165acf6e51
|
7
|
+
data.tar.gz: f4e627193ae0cf71b37b975a1cd95c33c3270fd1b83ab3e72ccb11f63cfca874802f256112889f679a553c9c59076834f7fb5964a316a13a660321af5e5270d8
|
data/README.md
CHANGED
@@ -87,6 +87,7 @@ m.save
|
|
87
87
|
|
88
88
|
# Finding
|
89
89
|
query = Model.find
|
90
|
+
query = Model.where # Alias
|
90
91
|
|
91
92
|
# Query doesn't get executed until you call all, count, last or first
|
92
93
|
m = query.all
|
@@ -103,6 +104,14 @@ m = Model.find.paginate
|
|
103
104
|
m = Model.find.paginate(:per_page => 10, :page => 5)
|
104
105
|
m.total # => Total number of results
|
105
106
|
|
107
|
+
# Use each to fetch one by one
|
108
|
+
Model.each do |m|
|
109
|
+
puts m
|
110
|
+
end
|
111
|
+
|
112
|
+
# Works with finds, scopes and associations
|
113
|
+
Model.find(:duration => 50).each{|m| puts m}
|
114
|
+
|
106
115
|
# All of these can be used:
|
107
116
|
# https://docs.mongodb.com/manual/reference/operator/query-comparison
|
108
117
|
m = Model.find(:house => {:$ne => nil, :$eq => 'Nice'}).last
|
@@ -148,7 +157,9 @@ m.duration_changed?
|
|
148
157
|
m.duration_was
|
149
158
|
m.changes
|
150
159
|
m.saved?
|
160
|
+
m.persisted? # Alias for saved?
|
151
161
|
m.unsaved?
|
162
|
+
m.new_record? # Alias for unsaved?
|
152
163
|
|
153
164
|
# Validate
|
154
165
|
m.valid?
|
data/lib/mongocore/document.rb
CHANGED
@@ -41,7 +41,7 @@ module Mongocore
|
|
41
41
|
# @saved indicates whether this is saved or not
|
42
42
|
#
|
43
43
|
|
44
|
-
attr_accessor :errors, :changes, :saved
|
44
|
+
attr_accessor :errors, :changes, :saved, :state
|
45
45
|
|
46
46
|
# # # # # # # # # # #
|
47
47
|
# The class initializer, called when you write Model.new
|
@@ -50,8 +50,8 @@ module Mongocore
|
|
50
50
|
#
|
51
51
|
def initialize(a = {})
|
52
52
|
|
53
|
-
#
|
54
|
-
self.attributes = self.class.schema.defaults.merge(a)
|
53
|
+
# Store attributes. Storing state state for dirty tracking.
|
54
|
+
self.attributes = self.class.schema.defaults.merge(a).tap{|r| self.state = r.dup}
|
55
55
|
|
56
56
|
# The _id is a BSON object, create new unless it exists
|
57
57
|
@_id ? @saved = true : @_id = BSON::ObjectId.new
|
@@ -117,7 +117,7 @@ module Mongocore
|
|
117
117
|
|
118
118
|
# Changed?
|
119
119
|
def changed?
|
120
|
-
changes.any?
|
120
|
+
@changes.any?
|
121
121
|
end
|
122
122
|
|
123
123
|
# JSON format
|
@@ -185,7 +185,7 @@ module Mongocore
|
|
185
185
|
v = self.class.schema.convert(key, val)
|
186
186
|
|
187
187
|
# Record change for dirty attributes
|
188
|
-
read!(key).tap{|r| @changes[key] =
|
188
|
+
read!(key).tap{|r| @changes[key] = [@state[key], v] if v != r} if @changes
|
189
189
|
|
190
190
|
# Write attribute
|
191
191
|
write!(key, v)
|
@@ -203,15 +203,14 @@ module Mongocore
|
|
203
203
|
|
204
204
|
# Write or read
|
205
205
|
if self.class.schema.keys.has_key?(key = $1.to_sym)
|
206
|
-
return write(key, args.first)
|
207
|
-
return read(key)
|
206
|
+
return $2 ? write(key, args.first) : read(key)
|
208
207
|
end
|
209
208
|
|
210
209
|
# Attributes changed?
|
211
|
-
return changes.has_key?($1.to_sym) if key =~ /(.+)_changed\?/
|
210
|
+
return @changes.has_key?($1.to_sym) if key =~ /(.+)_changed\?/
|
212
211
|
|
213
212
|
# Attributes was
|
214
|
-
return
|
213
|
+
return @state[$1.to_sym] if key =~ /(.+)_was/
|
215
214
|
|
216
215
|
# Pass if nothing found
|
217
216
|
super
|
@@ -227,6 +226,11 @@ module Mongocore
|
|
227
226
|
a.each{|k, v| a[k] = v.to_s if v.is_a?(BSON::ObjectId)}; a.delete(:_id); {:id => id}.merge(a)
|
228
227
|
end
|
229
228
|
|
229
|
+
# Print info about the instance
|
230
|
+
def inspect
|
231
|
+
"#<#{self.class} #{attributes.sort.map{|r| %{#{r[0]}: #{r[1].inspect}}}.join(', ')}>"
|
232
|
+
end
|
233
|
+
|
230
234
|
private
|
231
235
|
|
232
236
|
# Persist for save and update
|
data/lib/mongocore.rb
CHANGED
data/mongocore.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongocore'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.2.0'
|
4
4
|
s.date = '2017-10-25'
|
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."
|