mongocore 0.4.5 → 0.5.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: 2b7497f2ffc863157d942ced1f326b41a595ab1f
4
- data.tar.gz: f65ce1925bc082ffa01b603f8975752c40960b8e
3
+ metadata.gz: 74fdbe4ae3a48d20051b091780897353f615973e
4
+ data.tar.gz: b608caf9da396253e7024d84dfdff74547f01058
5
5
  SHA512:
6
- metadata.gz: d8728af7535106eb8865794093c8cca1b44d20276864267760622b03a183a818b80d63fdd92fa37b53d344b2ff21fd4e1d62aeeebe87a7d6bcfaa043622bd80c
7
- data.tar.gz: 49ba47e50f24a96ef3d3e4441145661e93c5651787fd02d9b4a4a4714a070ad2e5945815d36d9ee802a06a7a9868c6a613a5f45b0d2c025fa9e954a3075df5d8
6
+ metadata.gz: d42222537d1c66933894c3c527742e690db6dc6acfe82e72e2394ce9192624c66d1f0c39ba2503f573fcab92e32dcd78790a88f3bd26f179aa94820ec5e18f52
7
+ data.tar.gz: '0287ca2b41a01ca0b2ff079d5e457212690b3c5cb011ce3f4d4078a833b402b1bbebc5350e4da506d9f606c1025ec30cee610c3f5e0b1a2708f4d7aaab334157'
@@ -1,3 +1,9 @@
1
+ **Version 0.5.0** - *2018-01-24*
2
+
3
+ - Speed optimizations
4
+ - Support for binary type
5
+
6
+
1
7
  **Version 0.4.5** - *2018-01-23*
2
8
 
3
9
  - Fixed memory bug with Mongocore.sort option
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
@@ -56,6 +56,11 @@ keys:
56
56
  default: false
57
57
  read: user
58
58
  write: dev
59
+ zip_file:
60
+ desc: Zip file
61
+ type: binary
62
+ read: user
63
+ write: user
59
64
  votes_count:
60
65
  desc: Votes count
61
66
  type: integer
@@ -7,7 +7,7 @@ require 'mongo'
7
7
  require 'request_store'
8
8
 
9
9
  module Mongocore
10
- VERSION = '0.4.5'
10
+ VERSION = '0.5.0'
11
11
 
12
12
  # # # # # #
13
13
  # Mongocore Ruby Database Driver.
@@ -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.tap{|m| self.attributes = m.attributes; reset!}
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; @updated_at = t; @created_at = t if unsaved?
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, :update, :delete
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)) ? read!(key) : nil
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.convert(key, val)
194
-
195
- # Record change for dirty attributes
196
- read!(key).tap{|r| @changes[key] = [@original[key], v] if v != r} if @changes
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).tap{|r| r.save(o)}
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, :update, :delete
355
+ # Possible events for after and before are :save, :delete
351
356
  #
352
357
 
353
358
  # After
@@ -170,22 +170,26 @@ module Mongocore
170
170
 
171
171
  # Sort
172
172
  def sort(o = {})
173
- find(@query, @options.tap{(@options[:sort] ||= {}).merge!(o)})
173
+ (@options[:sort] ||= {}).merge!(o)
174
+ find(@query, @options)
174
175
  end
175
176
 
176
177
  # Limit
177
178
  def limit(n = 1)
178
- find(@query, @options.tap{@options[:limit] = n})
179
+ @options[:limit] = n
180
+ find(@query, @options)
179
181
  end
180
182
 
181
183
  # Skip
182
184
  def skip(n = 0)
183
- find(@query, @options.tap{@options[:skip] = n})
185
+ @options[:skip] = n
186
+ find(@query, @options)
184
187
  end
185
188
 
186
189
  # Projection
187
190
  def projection(o = {})
188
- find(@query, @options.tap{@options[:projection].merge!(o)})
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
- return @model.send(name, @query, @options.tap{|r| r[:chain] << name}) if @model.schema.scopes.has_key?(name)
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
 
@@ -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 type if val and schema type is set
57
- def convert(key, val)
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
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongocore'
3
- s.version = '0.4.5'
4
- s.date = '2018-01-23'
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.5
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-23 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo