mongocore 0.2.3 → 0.2.5

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: bc69ea47a26f1a85fd3519180999a21710800a44
4
- data.tar.gz: 523eaba0048b6d6f887bf4b1647f2f6f1ba8ecb1
3
+ metadata.gz: e348dc0ae701c8da46874976c5b32563bb5e3016
4
+ data.tar.gz: ce8cb95db0f96025242aedc2edc898fb0763372e
5
5
  SHA512:
6
- metadata.gz: 1129eb75394fa108c0c701bf2c8722c8cca9b886c4e3ff2d8eb1c9d496e98b46bb69af066489ae0f4158eb67394f2e1ea193a222544bed0f7f16831224db803d
7
- data.tar.gz: 05f1db2a8a7077981fb1690906e8c0e328fc531ff740a2ad226b6fb3d819347e9fb4b1426edaa91da8d945656d39f1bab347d77fc447159df857962162b8259f
6
+ metadata.gz: 3858bf69ee794b819aaa0acfc8e303002e618ca9eb5dcdb2f0bae7a8097612b639e83d4dbb7604d67b5de86d8416fffc372e23e80afd5697cdb2e05d6f5266d4
7
+ data.tar.gz: 4d5e536cbc8ca2a279148568ae658acf39a5cba8a9e87f20dade9279104c7f9db3747d09d77c58127166c14e97a4f7bdaf4b62928fb9dc0566c38b06889bb67e
data/README.md CHANGED
@@ -15,13 +15,13 @@ With Mongocore you can do:
15
15
 
16
16
  The schema is specified with a YAML file which supports default values, data types, and security levels for each key.
17
17
 
18
- Please read [the source code](https://github.com/fugroup/mongocore/tree/master/lib/mongocore) to see how it works, it's fully commented and very small, only 8 files, and 360 lines of fully test driven code.
18
+ Please read [the source code](https://github.com/fugroup/mongocore/tree/master/lib/mongocore) to see how it works, it's fully commented and very small, only 8 files, and 447 lines of fully test driven code.
19
19
 
20
20
  | Library | Files | Comment | Lines of code |
21
21
  | -------------------------------------- | ----- | ------- | ------------- |
22
22
  | [Mongoid](http://mongoid.com) | 256 | 14371 | 10590 |
23
23
  | [MongoMapper](http://mongomapper.com) | 91 | 200 | 4070 |
24
- | [Mongocore](http://mongocore.com) | 8 | 254 | 448 |
24
+ | [Mongocore](http://mongocore.com) | 8 | 253 | 447 |
25
25
 
26
26
  <br>
27
27
 
@@ -143,12 +143,11 @@ c = p.models.featured.count
143
143
  # Skip
144
144
  m = Model.find.skip(2).first
145
145
 
146
- # Tagged keys for attributes and to_json
146
+ # Attributes
147
147
  m = Model.first
148
148
  m.attributes # => All attributes
149
149
  m.attributes(:badge) # => Attributes with the badge tag only
150
150
  m.to_json # => All attributes as json
151
- m.to_json(:badge, :test) # => Pass multiple tags if needed
152
151
 
153
152
  # Track changes
154
153
  m.duration = 33
@@ -182,6 +181,12 @@ q = p.models.featured.all
182
181
  q = p.models.featured.nested.all
183
182
  m = Model.featured.first
184
183
 
184
+ # Access
185
+ model = Mongocore::Access.role(:user) do
186
+ # Reads and writes in the block will be with the above access level
187
+ Model.first
188
+ end
189
+
185
190
  # In your model
186
191
  class Model
187
192
  include Mongocore::Document
@@ -11,8 +11,8 @@ module Mongocore
11
11
  # where f.ex. you want to show the email to logged in users, but not to all.
12
12
  #
13
13
 
14
- # Access levels (6)
15
- AL = [:all, :user, :dev, :admin, :super, :app]
14
+ # Access levels (7)
15
+ AL = [:all, :user, :owner, :dev, :admin, :super, :app]
16
16
 
17
17
  # Holds the keys from the model schema
18
18
  attr_accessor :keys
@@ -22,41 +22,50 @@ module Mongocore
22
22
  @keys = schema.keys
23
23
  end
24
24
 
25
- # Set the current access level
26
- def set(level = nil)
27
- set?(level) ? RequestStore.store[:access] = level : get
25
+ # Key readable?
26
+ def read?(key)
27
+ ok?(keys[key][:read]) rescue false
28
28
  end
29
29
 
30
- # Get the current access level
31
- def get
32
- RequestStore.store[:access]
30
+ # Key writable?
31
+ def write?(key)
32
+ ok?(keys[key][:write]) rescue false
33
33
  end
34
34
 
35
+ # Ok?
36
+ def ok?(level)
37
+ !Mongocore.access || self.class.get.nil? || AL.index(level.to_sym) <= AL.index(self.class.get || :app)
38
+ end
39
+
40
+
41
+ ###########################
42
+ # Class methods
43
+ #
44
+
35
45
  # Reset the access level
36
- def reset
46
+ def self.reset
37
47
  RequestStore.store[:access] = nil
38
48
  end
39
49
 
40
- # Key readable?
41
- def read?(key)
42
- ok?(keys[key][:read]) rescue false
50
+ # Get the current access level
51
+ def self.get
52
+ RequestStore.store[:access]
43
53
  end
44
54
 
45
- # Key writable?
46
- def write?(key)
47
- ok?(keys[key][:write]) rescue false
55
+ # Set the current access level
56
+ def self.set(level = nil)
57
+ (level.nil? || set?(level)) ? RequestStore.store[:access] = level : get
48
58
  end
49
59
 
50
- private
51
-
52
60
  # Set?
53
- def set?(level)
54
- AL.index(level) > AL.index(get || :all)
61
+ def self.set?(level)
62
+ AL.index(level) >= AL.index(get || :all)
55
63
  end
56
64
 
57
- # Ok?
58
- def ok?(level)
59
- !Mongocore.access || AL.index(level.to_sym) <= AL.index(get || :app)
65
+ # Access block
66
+ # Run with Mongocore::Access(:user){ # Do something as :user}
67
+ def self.role(level, &block)
68
+ set(level); yield.tap{ set(nil)}
60
69
  end
61
70
 
62
71
  end
@@ -107,7 +107,7 @@ module Mongocore
107
107
 
108
108
  # Collect the attributes, pass tags like defined in your model yml
109
109
  def attributes(*tags)
110
- a = {}; self.class.schema.attributes(tags.map(&:to_s)).each{|k| a[k] = read!(k)}; a
110
+ a = {}; self.class.schema.attributes(tags.map(&:to_s)).each{|k| a[k] = read(k)}; a
111
111
  end
112
112
 
113
113
  # Set the attributes
@@ -307,6 +307,7 @@ module Mongocore
307
307
  find.each{|r| yield(r)}
308
308
  end
309
309
 
310
+
310
311
  # # # # # # # # #
311
312
  # After, before and validation filters
312
313
  # Pass a method name as symbol or a block
@@ -165,7 +165,7 @@ module Mongocore
165
165
 
166
166
  # Call and return the scope if it exists
167
167
  def method_missing(name, *arguments, &block)
168
- return @model.send(name, @query, @options, @store.tap{@store[:chain] << name}) if @model.schema.scopes.has_key?(name)
168
+ return @model.send(name, @query, @options, @store.tap{|r| r[:chain] << name}) if @model.schema.scopes.has_key?(name)
169
169
  super
170
170
  end
171
171
 
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.3'
10
+ VERSION = '0.2.5'
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.3'
4
- s.date = '2017-10-28'
3
+ s.version = '0.2.5'
4
+ s.date = '2017-10-30'
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.3
4
+ version: 0.2.5
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-28 00:00:00.000000000 Z
11
+ date: 2017-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo