mongoid-crud 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7086bf12049b8739ecbc8aeedeefb60c897087d
4
+ data.tar.gz: 24afb72a0b0710c5e8fde42360515d4f7d506300
5
+ SHA512:
6
+ metadata.gz: e1c752a14299e935071f1709c9c37db4da4f7dc865b9680c91ba841acab4fc54a42e85e2a032f0fdb454737e0a08d15b216f721e646118cfbae1927d7e222e76
7
+ data.tar.gz: b7ba1bb51b0d702657268ee22ad69a0ff10b9ae0538484fd5377bcbd5c19f116ac2df837926c5cfc6221c9b01b733a9a72f6fce2842204939e3eb1181f898391
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Adam Luzsi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ mongoid-crud
2
+ ============
3
+
4
+ CRUD methods for mongoid
5
+ Four super simple to use method on any mongoid model.
6
+
7
+ ```ruby
8
+
9
+ __create__
10
+ __read__
11
+ __update__
12
+ __delete__
13
+
14
+ ```
15
+
16
+ ### Example
17
+
18
+ ```ruby
19
+
20
+ test_a= TestA.__create__ hello: "world"
21
+ #> TestA obj return
22
+
23
+ test_b= TestB.__create__( hello: "world", world: "no", parent_id: test_a['_id'] )
24
+ #> TestB obj return
25
+
26
+ TestB.__read__( hello: "world", world: "no", parent_id: test_a['_id'] ).each{|e|puts(e.inspect)}
27
+ #> search by parent_id + query , mongoid criteria return
28
+
29
+ puts TestB.__read__( _id: test_b['_id'] ).inspect
30
+ #> search for target _id in embeds, object return
31
+
32
+ TestB.__read__( hello: "world", world: "no" ).inspect
33
+ #> search all embeds by query , return in simple array
34
+
35
+ TestB.__update__ _id: test_b['_id'], hello: "sup!"
36
+ #> true
37
+
38
+ TestB.__read__( _id: test_b['_id'] )
39
+ #> TestB Obj
40
+
41
+ TestB.__delete__( _id: test_b['_id'] )
42
+ #> true
43
+
44
+ TestB.__read__( _id: test_b['_id'] )
45
+ #> nil
46
+
47
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require File.join"bundler","gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
data/examples/crud.rb ADDED
@@ -0,0 +1,28 @@
1
+ require_relative "../lib/mongoid-crud"
2
+ require_relative "helper/connection"
3
+ require_relative "helper/models"
4
+
5
+ test_a= TestA.__create__( hello: "world" )
6
+ 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
+
9
+ #> puts into json
10
+ puts TestA._read( _id: test_a['_id'] ).to_json
11
+ # [{"_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"}]
12
+
13
+ TestB.__read__( hello: "world", world: "no", parent_id: test_a['_id'] ).each{|e|puts(e.inspect)}
14
+ #> search by parent_id + query
15
+
16
+ puts TestB.__read__( _id: test_b['_id'] ).inspect
17
+ #> search for target _id in embeds
18
+
19
+ puts TestB.__read__( hello: "world", world: "no" ).inspect
20
+ #> search all embeds by query
21
+
22
+ puts TestB.__update__ _id: test_b['_id'], hello: "sup!"
23
+
24
+ puts TestB.__read__( _id: test_b['_id'] ).inspect
25
+ puts TestB.__delete__( _id: test_b['_id'] ).inspect
26
+ puts TestB.__read__( _id: test_b['_id'] ).inspect
27
+
28
+ Mongoid.purge!
@@ -0,0 +1,2 @@
1
+ Mongoid.load!(File.join(File.dirname(__FILE__),"mongoid.yml"), :development)
2
+ I18n.enforce_available_locales = false
@@ -0,0 +1,34 @@
1
+
2
+ class TestA
3
+
4
+ include Mongoid::Document
5
+ include Mongoid::Timestamps
6
+ include Mongoid::CRUD #> but you should use extend for right use!
7
+
8
+ store_in :collection => self.mongoid_name
9
+
10
+ embeds_many :TestB.mongoid_name
11
+ embeds_one :TestC.mongoid_name
12
+
13
+ end
14
+
15
+ class TestB
16
+
17
+ include Mongoid::Document
18
+ include Mongoid::Timestamps
19
+ include Mongoid::CRUD
20
+
21
+ embedded_in :TestA.mongoid_name
22
+ embeds_many :TestC.mongoid_name
23
+
24
+ end
25
+
26
+ class TestC
27
+
28
+ include Mongoid::Document
29
+ include Mongoid::Timestamps
30
+ include Mongoid::CRUD
31
+
32
+ embedded_in :TestA.mongoid_name
33
+
34
+ end
@@ -0,0 +1,6 @@
1
+ development:
2
+ sessions:
3
+ default:
4
+ database: mongoid
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,103 @@
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
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+
5
+ spec.name = "mongoid-crud"
6
+ spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
7
+ spec.authors = ["Adam Luzsi"]
8
+ spec.email = ["adamluzsi@gmail.com"]
9
+ spec.description = "four method to rule them all! Use crud methods on mongoid classes to do anything. Super easy to use! Work even on deeply embedded models. Check on GitHub"
10
+ spec.summary = "CRUD on any mongoid model"
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_development_dependency "bundler"
18
+ spec.add_development_dependency "rake"
19
+
20
+ spec.add_dependency "mongoid-dsl", ">= 1.0.2"
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-crud
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Luzsi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongoid-dsl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.2
55
+ description: four method to rule them all! Use crud methods on mongoid classes to
56
+ do anything. Super easy to use! Work even on deeply embedded models. Check on GitHub
57
+ email:
58
+ - adamluzsi@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - VERSION
68
+ - examples/crud.rb
69
+ - examples/helper/connection.rb
70
+ - examples/helper/models.rb
71
+ - examples/helper/mongoid.yml
72
+ - lib/mongoid-crud.rb
73
+ - mongoid-crud.gemspec
74
+ homepage:
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: CRUD on any mongoid model
97
+ test_files: []