mongocore 0.3.0 → 0.3.1

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: fb6147e53bf5b8b125cb237a5eaa5345f75391f4
4
- data.tar.gz: 378a53e4425a375587dcce8c16a0e6f660f6d248
3
+ metadata.gz: b41d351659b6eb32abc203fcf4bfca71475fa310
4
+ data.tar.gz: 0f62bcc88e7c58aa4574fd0bd2c78366bbba886a
5
5
  SHA512:
6
- metadata.gz: e438ad2a1d641ee29777e116dec35187f8a113e1fd4233eba36cb6ac3ce924f6d1a877673ada4762000b0a20a4addb2ddc6d7774fd89df2f5ec90a7727fb497e
7
- data.tar.gz: 8c29834f2ec9975eec10f65beeda434fd8a9d7d141ab08bda548ce82e7768d40577205348e11541065cba1ebd52892a15eec199ac9882d54bad3da831835114e
6
+ metadata.gz: fdf761b37d0e415e7806bfa5e908737160ae2a9f59d6f0315c58b05cb8dad87448bd3d79049fe9ab224b3c9baf5164b430de8be51ffadfff914182ca0c911055
7
+ data.tar.gz: 91d1fc13533117f0ddf899d6fd29b18a5110c490731f35a9914e2ced321c0658d29ac066ab8a5dc9ad8c436246acac6d5603d99ebffd7dff2364f916e2d63b8d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ **Version 0.3.1** - *2017-11-25*
2
+
3
+ - Fixed bug where error didn't get reset on validation
4
+ - Can set association = nil
5
+ - Added default sort option
6
+ - Renamed a few internal variables
7
+
1
8
  **Version 0.3.0** - *2017-10-30*
2
9
 
3
10
  - Removed support for dependent destroy
data/README.md CHANGED
@@ -56,6 +56,9 @@ Mongocore.access = true
56
56
  # Enable timestamps, auto-save created_at and updated_at keys
57
57
  Mongocore.timestamps = true
58
58
 
59
+ # Default sorting, last will be opposite. Should be indexed.
60
+ Mongocore.sort = {:_id => 1}
61
+
59
62
  # Pagination results per page
60
63
  Mogocore.per_page = 20
61
64
 
@@ -3,7 +3,7 @@ module Mongocore
3
3
 
4
4
  # # # # # # # #
5
5
  # The Access class is responsible for checking if an attribute
6
- # can be read or written. It uses 6 access levels and the
6
+ # can be read or written. It uses 7 access levels and the
7
7
  # read and write attributes in the schema yml file for the model.
8
8
  #
9
9
  # If your current access level is above the key level, then you
@@ -24,17 +24,17 @@ module Mongocore
24
24
 
25
25
  # Key readable?
26
26
  def read?(key)
27
- ok?(keys[key][:read] || :all) rescue false
27
+ ok?(@keys[key][:read] || :all) rescue false
28
28
  end
29
29
 
30
30
  # Key writable?
31
31
  def write?(key)
32
- ok?(keys[key][:write] || :all) rescue false
32
+ ok?(@keys[key][:write] || :all) rescue false
33
33
  end
34
34
 
35
35
  # Ok?
36
36
  def ok?(level)
37
- !Mongocore.access || self.class.get.nil? || AL.index(level.to_sym) <= AL.index(self.class.get || :app)
37
+ !Mongocore.access || (g = self.class.get).nil? || AL.index(level.to_sym) <= AL.index(g || :app)
38
38
  end
39
39
 
40
40
 
@@ -38,10 +38,10 @@ module Mongocore
38
38
  # Instance variables
39
39
  # @errors is used for validates
40
40
  # @changes keeps track of object changes
41
- # @saved indicates whether this is saved or not
41
+ # @persisted indicates whether this is saved or not
42
42
  #
43
43
 
44
- attr_accessor :errors, :changes, :saved, :state
44
+ attr_accessor :errors, :changes, :persisted, :original
45
45
 
46
46
  # # # # # # # # # # #
47
47
  # The class initializer, called when you write Model.new
@@ -50,11 +50,11 @@ module Mongocore
50
50
  #
51
51
  def initialize(a = {})
52
52
 
53
- # Store attributes. Storing state state for dirty tracking.
54
- self.attributes = self.class.schema.defaults.merge(a).tap{|r| self.state = r.dup}
53
+ # Store attributes. Storing original state for dirty tracking.
54
+ self.attributes = self.class.schema.defaults.merge(a).tap{|r| @original = r.dup}
55
55
 
56
56
  # The _id is a BSON object, create new unless it exists
57
- @_id ? @saved = true : @_id = BSON::ObjectId.new
57
+ @_id ? @persisted = true : @_id = BSON::ObjectId.new
58
58
 
59
59
  # The errors hash
60
60
  @errors = Hash.new{|h, k| h[k] = []}
@@ -86,8 +86,8 @@ module Mongocore
86
86
  end
87
87
 
88
88
  # Run filters before and after accessing the db
89
- def filter(cmd, saved = true, &block)
90
- run(:before, cmd); yield.tap{@saved = saved; run(:after, cmd)}
89
+ def filter(cmd, persisted = true, &block)
90
+ run(:before, cmd); yield.tap{@persisted = persisted; run(:after, cmd)}
91
91
  end
92
92
 
93
93
  # Reload the document from db and update attributes
@@ -131,7 +131,7 @@ module Mongocore
131
131
 
132
132
  # Valid?
133
133
  def valid?
134
- self.class.filters.valid?(self)
134
+ @errors.clear; self.class.filters.valid?(self)
135
135
  end
136
136
 
137
137
  # Available filters are :save, :update, :delete
@@ -145,11 +145,11 @@ module Mongocore
145
145
  #
146
146
 
147
147
  # Saved? Persisted?
148
- def saved?; !!@saved; end
148
+ def saved?; !!@persisted; end
149
149
  alias_method :persisted?, :saved?
150
150
 
151
151
  # Unsaved? New record?
152
- def unsaved?; !@saved; end
152
+ def unsaved?; !@persisted; end
153
153
  alias_method :new_record?, :unsaved?
154
154
 
155
155
  # Short cut for setting up a Mongocore::Query object
@@ -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] = [@state[key], v] if v != r} if @changes
188
+ read!(key).tap{|r| @changes[key] = [@original[key], v] if v != r} if @changes
189
189
 
190
190
  # Write attribute
191
191
  write!(key, v)
@@ -210,7 +210,7 @@ module Mongocore
210
210
  return @changes.has_key?($1.to_sym) if key =~ /(.+)_changed\?/
211
211
 
212
212
  # Attributes was
213
- return @state[$1.to_sym] if key =~ /(.+)_was/
213
+ return @original[$1.to_sym] if key =~ /(.+)_was/
214
214
 
215
215
  # Pass if nothing found
216
216
  super
@@ -234,7 +234,7 @@ module Mongocore
234
234
  return false unless valid? if o[:validate]
235
235
 
236
236
  # Create a new query
237
- filter(type){one.send((@saved ? :update : :insert), attributes).ok?}
237
+ filter(type){one.send((@persisted ? :update : :insert), attributes).ok?}
238
238
  end
239
239
 
240
240
  # Replace _id with id, takes a hash
data/lib/mongocore/ext.rb CHANGED
@@ -22,6 +22,7 @@ end
22
22
  module BSON
23
23
  class ObjectId
24
24
 
25
+ # Override the to_json method
25
26
  def as_json(o = {})
26
27
  self.to_s
27
28
  end
@@ -24,8 +24,8 @@ module Mongocore
24
24
  # Storing the Mongo::Collection object
25
25
  @collection = Mongocore.db[@colname]
26
26
 
27
- # Storing query and options. Sort and limit is stored in options
28
- s[:sort] ||= {}; s[:limit] ||= 0; s[:chain] ||= []; s[:source] ||= nil; s[:fields] ||= {}; s[:skip] ||= 0
27
+ # Storing query and options
28
+ s[:limit] ||= 0; s[:chain] ||= []; s[:source] ||= nil; s[:fields] ||= {}; s[:skip] ||= 0
29
29
  @query, @options, @store = @model.schema.ids(hashify(q)), o, s
30
30
 
31
31
  # Set up cache
@@ -45,7 +45,7 @@ module Mongocore
45
45
 
46
46
  # Cursor
47
47
  def cursor
48
- @collection.find(@query, @options).projection(@store[:fields]).skip(@store[:skip]).sort(@store[:sort]).limit(@store[:limit])
48
+ @collection.find(@query, @options).projection(@store[:fields]).skip(@store[:skip]).sort(@store[:sort] || Mongocore.sort).limit(@store[:limit])
49
49
  end
50
50
 
51
51
  # Insert
@@ -86,7 +86,7 @@ module Mongocore
86
86
 
87
87
  # Return last document
88
88
  def last(*args)
89
- sort(:_id => -1).limit(1).first(*args)
89
+ sort((a = Mongocore.sort.dup).each{|k, v| a[k] = v * -1}).limit(1).first(*args)
90
90
  end
91
91
 
92
92
  # Return all documents
@@ -135,7 +135,7 @@ module Mongocore
135
135
 
136
136
  # Sort
137
137
  def sort(o = {})
138
- find(@query, @options, @store.tap{store[:sort].merge!(o)})
138
+ find(@query, @options, @store.tap{(store[:sort] ||= {}).merge!(o)})
139
139
  end
140
140
 
141
141
  # Limit
@@ -104,7 +104,7 @@ module Mongocore
104
104
  end
105
105
 
106
106
  def #{$1}=(m)
107
- @#{key} = m._id
107
+ @#{key} = m ? m._id : nil
108
108
  @#{$1} = m
109
109
  end
110
110
  }
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.3.0'
10
+ VERSION = '0.3.1'
11
11
 
12
12
  # # # # # #
13
13
  # Mongocore Ruby Database Driver.
@@ -16,7 +16,7 @@ module Mongocore
16
16
  # @license: MIT, contributions are welcome.
17
17
  # # # # # #
18
18
 
19
- class << self; attr_accessor :db, :schema, :cache, :access, :timestamps, :per_page, :debug; end
19
+ class << self; attr_accessor :db, :schema, :cache, :access, :timestamps, :sort, :per_page, :debug; end
20
20
 
21
21
  # Schema path is $app_root/config/db/schema/:model_name.yml
22
22
  @schema = File.join(Dir.pwd, 'config', 'db', 'schema')
@@ -30,6 +30,9 @@ module Mongocore
30
30
  # Enable timestamps, auto-save created_at and updated_at fields
31
31
  @timestamps = true
32
32
 
33
+ # Default sorting, last will be opposite. Should be indexed.
34
+ @sort = {:_id => 1}
35
+
33
36
  # Pagination results per page
34
37
  @per_page = 20
35
38
 
data/mongocore.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mongocore'
3
- s.version = '0.3.0'
4
- s.date = '2017-10-30'
3
+ s.version = '0.3.1'
4
+ s.date = '2017-11-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."
7
- s.authors = ["Fugroup Limited"]
7
+ s.authors = ['Fugroup Limited']
8
8
  s.email = 'mail@fugroup.net'
9
9
 
10
10
  s.add_runtime_dependency 'mongo', '~> 2.2'
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.3.0
4
+ version: 0.3.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-30 00:00:00.000000000 Z
11
+ date: 2017-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo