mongoid-crud 1.1.0 → 1.2.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: b7086bf12049b8739ecbc8aeedeefb60c897087d
4
- data.tar.gz: 24afb72a0b0710c5e8fde42360515d4f7d506300
3
+ metadata.gz: fbce96459af1c57f5b98d2bf14e933707ad3ae8d
4
+ data.tar.gz: 1e31e3cad5d3ba14800bfab0af3091a9f1669ec7
5
5
  SHA512:
6
- metadata.gz: e1c752a14299e935071f1709c9c37db4da4f7dc865b9680c91ba841acab4fc54a42e85e2a032f0fdb454737e0a08d15b216f721e646118cfbae1927d7e222e76
7
- data.tar.gz: b7ba1bb51b0d702657268ee22ad69a0ff10b9ae0538484fd5377bcbd5c19f116ac2df837926c5cfc6221c9b01b733a9a72f6fce2842204939e3eb1181f898391
6
+ metadata.gz: 0c4ebe35f65019622ab0489d7cd3a07ccd6ffe7b3c42de76291fa6d29b25500bbf2b333ee19f5a53e4ff886039cc55df8a32805b597bda07a685739111498b3c
7
+ data.tar.gz: 18a995f01c8bb2c14dee9e192d6d39dd5ab90ab633bf9a03efae312c1f2e740c87eb9a26e56d8cb7849e18b31dd64fe28bcaf8662d99afa07b13dc2c620048fe
data/README.md CHANGED
@@ -1,23 +1,73 @@
1
1
  mongoid-crud
2
2
  ============
3
3
 
4
- CRUD methods for mongoid
4
+ CRUD methods for mongoid models
5
+
6
+ ## Four method to rule them all!
7
+
5
8
  Four super simple to use method on any mongoid model.
6
9
 
10
+
7
11
  ```ruby
12
+ __create__ || _create
13
+ __read__ || _read
14
+ __update__ || _update
15
+ __delete__ || _delete
16
+ ```
17
+ ### Behavior
8
18
 
9
- __create__
10
- __read__
11
- __update__
12
- __delete__
19
+ create:
20
+ * if the model is an embeds it require a parent_id passed by "parent_id" or :parent_id key
21
+ * same goes for the referenced
22
+ * if the model is a main one it's the same as normaly, pass the hash obj
23
+
24
+ read:
25
+ * if the model embeds or referenced you MAY tell the parent_id but you dont have to
26
+ * in that case every doc that match the query hash will be returned in an array
27
+ * if you give _id than it will the object (even if it's embedded deeply in mordor)
28
+ * if you pass parent_id but no _id it will return a mongoid criteria with the collection of finds under the specified parent
29
+
30
+ update:
31
+ * you must give the _id for the target obj and any other hash tag will be the values to be set
32
+
33
+ delete:
34
+ * you must give the _id for delete the target obj
35
+ * if you want drop a collection from a parent i suggest to use read + _id to find that object and give direct order to drop the embeds/reference collection
13
36
 
14
- ```
15
37
 
16
38
  ### Example
17
39
 
40
+ short example:
41
+
42
+ ```ruby
43
+ require "mongoid-crud"
44
+
45
+ require_relative "helper/connection"
46
+ require_relative "helper/models"
47
+
48
+ test_a= TestA.__create__( hello: "world" )
49
+ test_b= TestB.__create__( hello: "world", world: "no", parent_id: test_a['_id'] )
50
+ test_C= TestC.__create__( hello: "embeds one", parent_id: test_a['_id'] )
51
+
52
+ #> puts into json
53
+ puts TestA._read( _id: test_a['_id'] ).to_json
54
+ ```
55
+
56
+ produce:
57
+
58
+ ```json
59
+ [{"_id":"536b298b241548e55a000001","created_at":"2014-05-08T08:51:55+02:00","hello":"world","test_b":[{"_id":"536b298b241548e55a000002","created_at":"2014-05-08T08:51:55+02:00","hello":"world","updated_at":"2014-05-08T08:51:55+02:00","world":"no"}],"test_c":{"_id":"536b298b241548e55a000003","created_at":"2014-05-08T08:51:55+02:00","hello":"embeds one","updated_at":"2014-05-08T08:51:55+02:00"},"updated_at":"2014-05-08T08:51:55+02:00"}]
60
+ ```
61
+
62
+ #### Complex example
63
+
18
64
  ```ruby
65
+ require "mongoid-crud"
19
66
 
20
- test_a= TestA.__create__ hello: "world"
67
+ require_relative "helper/connection"
68
+ require_relative "helper/models"
69
+
70
+ test_a= TestA.__create__( hello: "world" )
21
71
  #> TestA obj return
22
72
 
23
73
  test_b= TestB.__create__( hello: "world", world: "no", parent_id: test_a['_id'] )
@@ -43,5 +93,4 @@ Four super simple to use method on any mongoid model.
43
93
 
44
94
  TestB.__read__( _id: test_b['_id'] )
45
95
  #> nil
46
-
47
96
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -0,0 +1,15 @@
1
+ require "mongoid-crud"
2
+
3
+ require_relative "helper/connection"
4
+ require_relative "helper/models"
5
+
6
+ test_a= TestA.__create__( hello: "world" )
7
+ test_b= TestB.__create__( hello: "world", world: "no", parent_id: test_a['_id'] )
8
+ test_c= TestC.__create__( hello: "embeds one", parent_id: test_a['_id'] )
9
+ test_d= TestD.__create__( hello: "embeds many in embeded one", parent_id: test_c['_id'] )
10
+
11
+ #> puts into json
12
+ puts TestA._read( _id: test_a['_id'] )
13
+
14
+ # [{"_id":"536b298b241548e55a000001","created_at":"2014-05-08T08:51:55+02:00","hello":"world","test_b":[{"_id":"536b298b241548e55a000002","created_at":"2014-05-08T08:51:55+02:00","hello":"world","updated_at":"2014-05-08T08:51:55+02:00","world":"no"}],"test_c":{"_id":"536b298b241548e55a000003","created_at":"2014-05-08T08:51:55+02:00","hello":"embeds one","updated_at":"2014-05-08T08:51:55+02:00"},"updated_at":"2014-05-08T08:51:55+02:00"}]
15
+
data/examples/crud.rb CHANGED
@@ -1,10 +1,11 @@
1
- require_relative "../lib/mongoid-crud"
1
+ require "mongoid-crud"
2
+
2
3
  require_relative "helper/connection"
3
4
  require_relative "helper/models"
4
5
 
5
6
  test_a= TestA.__create__( hello: "world" )
6
7
  test_b= TestB.__create__( hello: "world", world: "no", parent_id: test_a['_id'] )
7
- test_C= TestC.__create__( hello: "embeds one", parent_id: test_a['_id'] )
8
+ test_c= TestC.__create__( hello: "embeds one", parent_id: test_a['_id'] )
8
9
 
9
10
  #> puts into json
10
11
  puts TestA._read( _id: test_a['_id'] ).to_json
@@ -24,5 +25,3 @@ puts TestB.__update__ _id: test_b['_id'], hello: "sup!"
24
25
  puts TestB.__read__( _id: test_b['_id'] ).inspect
25
26
  puts TestB.__delete__( _id: test_b['_id'] ).inspect
26
27
  puts TestB.__read__( _id: test_b['_id'] ).inspect
27
-
28
- Mongoid.purge!
@@ -30,5 +30,18 @@ class TestC
30
30
  include Mongoid::CRUD
31
31
 
32
32
  embedded_in :TestA.mongoid_name
33
+ embeds_many :TestD.mongoid_name
33
34
 
34
- end
35
+ end
36
+
37
+ class TestD
38
+
39
+ include Mongoid::Document
40
+ include Mongoid::Timestamps
41
+ #> if auto extended the CRUD methods, it require not to include / extend here
42
+
43
+ embedded_in :TestC.mongoid_name
44
+
45
+ end
46
+
47
+ ::Kernel.at_exit {Mongoid.purge!}
@@ -0,0 +1,10 @@
1
+ begin
2
+
3
+ require 'mongoid-crud/ext'
4
+ Mongoid::Config.inject_singleton_method :register_model, add: :before do |klass|
5
+ klass.__send__ :extend, Mongoid::CRUD::Extend
6
+ end
7
+
8
+ rescue Exception
9
+ STDOUT.puts("mongoid-dsl")
10
+ end
@@ -0,0 +1,109 @@
1
+ require 'mongoid'
2
+ require 'mongoid-dsl'
3
+
4
+ module Mongoid
5
+ module CRUD
6
+
7
+ module Extend
8
+
9
+ @@parent_sym= :parent_id
10
+
11
+ def __create__ *args
12
+
13
+ query = Hash[*args.select{|e| e.class <= ::Hash }]
14
+ classes = args.select{|e| e.class <= ::Class }
15
+
16
+ if self.embedded?
17
+
18
+ parent_id = query.delete(@@parent_sym) || query.delete(@@parent_sym.to_s)
19
+ raise(ArgumentError,"for embeded document, you need :#{@@parent_sym}") if parent_id.nil?
20
+ parent_model= self._get_class_path(*classes).pinch.last
21
+
22
+ case parent_model.relation_connection_type(self).to_s.downcase.split('::').last.to_sym
23
+
24
+ when :many
25
+ return self._get_class_path(*classes).pinch.last._find(parent_id).__send__(self.mongoid_name).create!(query)
26
+
27
+ when :one
28
+ return self._get_class_path(*classes).pinch.last._find(parent_id).__send__("create_#{self.mongoid_name}",query)
29
+
30
+ end
31
+
32
+ else
33
+ return self.create!(query)
34
+ end
35
+
36
+ end
37
+ alias _create __create__
38
+
39
+ def __read__ *args
40
+
41
+ query = Hash[*args.select{|e| e.class <= ::Hash }]
42
+ classes = args.select{|e| e.class <= ::Class }
43
+ _id = query.delete(:_id) || query.delete('_id')
44
+
45
+ if self.embedded?
46
+
47
+ parent_id = query.delete(@@parent_sym) || query.delete(@@parent_sym.to_s)
48
+
49
+ if !_id.nil?
50
+ return self._find(_id)#.__send__(self.mongoid_name).create(query)
51
+ elsif !parent_id.nil?
52
+ return self._get_class_path(*classes).pinch.last._find(parent_id).__send__(self.mongoid_name).where(query)
53
+ else
54
+ return self._where(query)
55
+ end
56
+
57
+ else
58
+
59
+ if !_id.nil?
60
+ return self.find(_id)
61
+ else
62
+ return self.where(query)
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+ alias _read __read__
69
+
70
+ def __update__ *args
71
+
72
+ query = Hash[*args.select{|e| e.class <= ::Hash }]
73
+ classes = args.select{|e| e.class <= ::Class }
74
+
75
+ raise(ArgumentError,"to #{__method__} document, you need :_id") if query[:_id].nil?
76
+ var= self._find(query.delete(:_id))
77
+ query.each{|k,v| var[k]= v }
78
+ return var.save!
79
+
80
+ end
81
+ alias _update __update__
82
+
83
+ def __delete__ *args
84
+
85
+ query = Hash[*args.select{|e| e.class <= ::Hash }]
86
+ classes = args.select{|e| e.class <= ::Class }
87
+
88
+ raise(ArgumentError,"to #{__method__} document, you need :_id") if query[:_id].nil?
89
+ return self._find(query[:_id]).delete
90
+
91
+ end
92
+ alias _delete __delete__
93
+
94
+ end
95
+
96
+ class << self
97
+
98
+ def included klass
99
+ klass.__send__ :extend, self::Extend
100
+ end
101
+
102
+ def extended klass
103
+ klass.__send__ :extend, self::Extend
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+ end
data/lib/mongoid-crud.rb CHANGED
@@ -1,103 +1 @@
1
- require 'mongoid'
2
- require 'mongoid-dsl'
3
-
4
- module Mongoid
5
-
6
- module CRUD
7
-
8
- module Extend
9
-
10
- @@parent_sym= :parent_id
11
-
12
- def __create__ *args
13
-
14
- query = Hash[*args.select{|e| e.class <= ::Hash }]
15
- classes = args.select{|e| e.class <= ::Class }
16
-
17
- if self.embedded?
18
-
19
- raise(ArgumentError,"for embeded document, you need :#{@@parent_sym}") if query[@@parent_sym].nil?
20
- parent_model= self._get_class_path(*classes).pinch.last
21
-
22
- case parent_model.relation_connection_type(self).to_s.downcase.split('::').last.to_sym
23
-
24
- when :many
25
- return self._get_class_path(*classes).pinch.last._find(query.delete(@@parent_sym)).__send__(self.mongoid_name).create!(query)
26
-
27
- when :one
28
- return self._get_class_path(*classes).pinch.last._find(query.delete(@@parent_sym)).__send__("create_#{self.mongoid_name}",query)
29
-
30
- end
31
-
32
- else
33
- return self.create!(query)
34
- end
35
-
36
- end
37
- alias _create __create__
38
-
39
- def __read__ *args
40
-
41
- query = Hash[*args.select{|e| e.class <= ::Hash }]
42
- classes = args.select{|e| e.class <= ::Class }
43
-
44
- if self.embedded?
45
-
46
- parent_id = query.delete(@@parent_sym)
47
- _id = query.delete('_id')
48
-
49
- if !_id.nil?
50
- return self._find(_id)#.__send__(self.mongoid_name).create(query)
51
- elsif !query[@@parent_sym].nil?
52
- return self._get_class_path(*classes).pinch.last._find(parent_id).__send__(self.mongoid_name).where(query)
53
- else
54
- return self._where(query)
55
- end
56
-
57
- else
58
- return self.where(query)
59
- end
60
-
61
- end
62
- alias _read __read__
63
-
64
- def __update__ *args
65
-
66
- query = Hash[*args.select{|e| e.class <= ::Hash }]
67
- classes = args.select{|e| e.class <= ::Class }
68
-
69
- raise(ArgumentError,"to #{__method__} document, you need :_id") if query[:_id].nil?
70
- var= self._find(query.delete(:_id))
71
- query.each{|k,v| var[k]= v }
72
- return var.save!
73
-
74
- end
75
- alias _update __update__
76
-
77
- def __delete__ *args
78
-
79
- query = Hash[*args.select{|e| e.class <= ::Hash }]
80
- classes = args.select{|e| e.class <= ::Class }
81
-
82
- raise(ArgumentError,"to #{__method__} document, you need :_id") if query[:_id].nil?
83
- return self._find(query[:_id]).delete
84
-
85
- end
86
- alias _delete __delete__
87
-
88
- end
89
-
90
- class << self
91
-
92
- def included klass
93
- klass.__send__ :extend, self::Extend
94
- end
95
-
96
- def extended klass
97
- klass.__send__ :extend, self::Extend
98
- end
99
-
100
- end
101
-
102
- end
103
- end
1
+ require 'mongoid-crud/auto'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-crud
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -65,11 +65,14 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - VERSION
68
+ - examples/create.rb
68
69
  - examples/crud.rb
69
70
  - examples/helper/connection.rb
70
71
  - examples/helper/models.rb
71
72
  - examples/helper/mongoid.yml
72
73
  - lib/mongoid-crud.rb
74
+ - lib/mongoid-crud/auto.rb
75
+ - lib/mongoid-crud/ext.rb
73
76
  - mongoid-crud.gemspec
74
77
  homepage:
75
78
  licenses: []