mongous 0.1.2 → 0.1.3
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 +4 -4
- data/lib/mongous/document.rb +2 -2
- data/lib/mongous/extention.rb +2 -22
- data/lib/mongous/filter.rb +105 -21
- data/lib/mongous/version.rb +1 -1
- data/sample/query_basic_1.rb +5 -2
- data/sample/query_basic_2.rb +5 -2
- data/sample/query_basic_5.rb +0 -19
- data/sample/query_basic_6.rb +43 -0
- data/sample/save_basic_1.rb +5 -5
- data/sample/save_basic_2.rb +1 -1
- data/sample/save_basic_3.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de69281ec1896f88f5b8d5c7c30654a12559cf3a7c97ea7d58cd2fde6e8579fd
|
4
|
+
data.tar.gz: 8db5720698450a5f9fcbf912fab5611d800588e02bfea09141c9bf23a1c0a088
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90d5962f8140f085afc07e9b9c9380a6c38aca825dd977d5ee6a1e8d1042ad39514a6185eb9fed38f9a370e794dea2b8c99b5d99adb661dfc0e9306806084bdd
|
7
|
+
data.tar.gz: 9c22f0d55962fce5fead46e9d4e0c27d89fb4b87ca268cef9c123c8737d9b5ae8987748d158a3b39d3acf1c21175729005ebce917fdc0d9118c2fc1dd19623d1
|
data/lib/mongous/document.rb
CHANGED
data/lib/mongous/extention.rb
CHANGED
@@ -91,28 +91,8 @@ module Mongous
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
|
-
def create( doc
|
95
|
-
self.new( doc
|
96
|
-
end
|
97
|
-
|
98
|
-
def filter( **_filter )
|
99
|
-
Filter.new( self ).filter( _filter )
|
100
|
-
end
|
101
|
-
|
102
|
-
def first
|
103
|
-
Filter.new( self ).first
|
104
|
-
end
|
105
|
-
|
106
|
-
def all
|
107
|
-
Filter.new( self ).all
|
108
|
-
end
|
109
|
-
|
110
|
-
def each( &block )
|
111
|
-
Filter.new( self ).each( &block )
|
112
|
-
end
|
113
|
-
|
114
|
-
def delete
|
115
|
-
Filter.new( self ).delete
|
94
|
+
def create( **doc )
|
95
|
+
self.new( **doc ).save
|
116
96
|
end
|
117
97
|
|
118
98
|
def field( label, *args, **opts, &block )
|
data/lib/mongous/filter.rb
CHANGED
@@ -1,4 +1,78 @@
|
|
1
1
|
|
2
|
+
module Mongous
|
3
|
+
module Extention
|
4
|
+
def first
|
5
|
+
doc = self.collection.find.first
|
6
|
+
self.new( **doc ) if doc
|
7
|
+
end
|
8
|
+
|
9
|
+
def all
|
10
|
+
self.collection.find.map do |doc|
|
11
|
+
self.new( **doc )
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def each( &block )
|
16
|
+
all.each( &block )
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete
|
20
|
+
self.collection.delete_many({})
|
21
|
+
end
|
22
|
+
|
23
|
+
def filter( **conditions )
|
24
|
+
Filter.new( self ).filter( conditions )
|
25
|
+
end
|
26
|
+
|
27
|
+
def not( filter = nil, **conditions )
|
28
|
+
raise Mongous::Error, "Unset args for #{self}.not." if filter.nil? && conditions.empty?
|
29
|
+
|
30
|
+
filter ||= conditions
|
31
|
+
condition = case filter
|
32
|
+
when Hash
|
33
|
+
filter
|
34
|
+
when Filter
|
35
|
+
filter.to_condition
|
36
|
+
else
|
37
|
+
raise Mongous::Error, "Invalid args for #{self}.not. : #{filter}"
|
38
|
+
end
|
39
|
+
Filter.new( self ).filter({"$nor" => [condition]})
|
40
|
+
end
|
41
|
+
|
42
|
+
def and( *filters )
|
43
|
+
raise Mongous::Error, "Unset args for #{self}.and." if filters.empty?
|
44
|
+
|
45
|
+
conditions = filters.map do |filter|
|
46
|
+
case filter
|
47
|
+
when Hash
|
48
|
+
filter
|
49
|
+
when Filter
|
50
|
+
filter.to_condition
|
51
|
+
else
|
52
|
+
raise Mongous::Error, "Invalid args for #{self}.and. : #{filter}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
Filter.new( self ).filter({"$and" => conditions})
|
56
|
+
end
|
57
|
+
|
58
|
+
def or( *filters )
|
59
|
+
raise Mongous::Error, "Unset args for #{self}.or." if filters.empty?
|
60
|
+
|
61
|
+
conditions = filters.map do |filter|
|
62
|
+
case filter
|
63
|
+
when Hash
|
64
|
+
filter
|
65
|
+
when Filter
|
66
|
+
filter.to_condition
|
67
|
+
else
|
68
|
+
raise Mongous::Error, "Invalid args for #{self}.or. : #{filter}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
Filter.new( self ).filter({"$or" => conditions})
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
2
76
|
module Mongous
|
3
77
|
class Filter
|
4
78
|
def initialize( klass )
|
@@ -7,40 +81,50 @@ module Mongous
|
|
7
81
|
@option = {}
|
8
82
|
end
|
9
83
|
|
10
|
-
def filter(
|
84
|
+
def filter( conditions )
|
11
85
|
hash = {}
|
12
|
-
|
13
|
-
case
|
14
|
-
when
|
15
|
-
hash[key] =
|
86
|
+
conditions.each do |key, item|
|
87
|
+
case key
|
88
|
+
when /\$(and|or|nor)/
|
89
|
+
hash[key] = item
|
16
90
|
|
17
|
-
|
18
|
-
|
19
|
-
|
91
|
+
else
|
92
|
+
case item
|
93
|
+
when Array
|
94
|
+
hash[key] = {"$in"=>item}
|
20
95
|
|
21
|
-
|
22
|
-
|
96
|
+
when Range
|
97
|
+
_begin_oper = "$gte"
|
98
|
+
_end_oper = item.exclude_end? ? "$lt" : "$lte"
|
23
99
|
|
24
|
-
|
25
|
-
|
100
|
+
if item.begin && item.end
|
101
|
+
hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
|
26
102
|
|
27
|
-
|
28
|
-
|
103
|
+
elsif !item.begin && item.end
|
104
|
+
hash[key] = { _end_oper => item.end }
|
29
105
|
|
30
|
-
|
31
|
-
|
106
|
+
elsif item.begin && !item.end
|
107
|
+
hash[key] = { _begin_oper => item.begin }
|
32
108
|
|
33
|
-
|
109
|
+
else
|
110
|
+
raise Mongous::Error, "invalid range. : #{ item }"
|
34
111
|
|
35
|
-
|
36
|
-
hash[key] = item
|
112
|
+
end
|
37
113
|
|
114
|
+
else
|
115
|
+
hash[key] = item
|
116
|
+
|
117
|
+
end
|
38
118
|
end
|
39
119
|
end
|
40
120
|
@filter.merge!( hash )
|
41
121
|
self.dup
|
42
122
|
end
|
43
123
|
|
124
|
+
def to_condition
|
125
|
+
@filter.dup
|
126
|
+
end
|
127
|
+
|
44
128
|
def option( _option )
|
45
129
|
self.option!( _option )
|
46
130
|
self.dup
|
@@ -102,12 +186,12 @@ module Mongous
|
|
102
186
|
|
103
187
|
def first
|
104
188
|
doc = do_find.first
|
105
|
-
@klass.new( doc ) if doc
|
189
|
+
@klass.new( **doc ) if doc
|
106
190
|
end
|
107
191
|
|
108
192
|
def all
|
109
193
|
do_find.map do |doc|
|
110
|
-
@klass.new( doc )
|
194
|
+
@klass.new( **doc )
|
111
195
|
end
|
112
196
|
end
|
113
197
|
|
data/lib/mongous/version.rb
CHANGED
data/sample/query_basic_1.rb
CHANGED
data/sample/query_basic_2.rb
CHANGED
@@ -8,11 +8,14 @@ class Book
|
|
8
8
|
end
|
9
9
|
|
10
10
|
|
11
|
-
book = Book.filter( title:
|
12
|
-
|
11
|
+
p book = Book.filter( title: /title/ ).first
|
12
|
+
puts
|
13
|
+
|
14
|
+
pp books = Book.filter( title: /title/ ).all
|
13
15
|
puts
|
14
16
|
|
15
17
|
Book.filter( title: /title/ ).each do |book|
|
16
18
|
p book
|
17
19
|
end
|
20
|
+
puts
|
18
21
|
|
data/sample/query_basic_5.rb
CHANGED
@@ -18,22 +18,3 @@ Book.filter( page: (100...300) ).each do |book|
|
|
18
18
|
end
|
19
19
|
puts
|
20
20
|
|
21
|
-
exit if RUBY_VERSION < "2.6"
|
22
|
-
|
23
|
-
Book.filter( page: (200..) ).each do |book|
|
24
|
-
p book
|
25
|
-
end
|
26
|
-
puts
|
27
|
-
|
28
|
-
exit if RUBY_VERSION < "2.7"
|
29
|
-
|
30
|
-
Book.filter( page: (..200) ).each do |book|
|
31
|
-
p book
|
32
|
-
end
|
33
|
-
puts
|
34
|
-
|
35
|
-
Book.filter( page: (...300) ).each do |book|
|
36
|
-
p book
|
37
|
-
end
|
38
|
-
puts
|
39
|
-
|
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
Book.new( title: "complex 1", author: "Alice", style: "A4", price: 1000, page: 100 ).save
|
11
|
+
Book.new( title: "complex 2", author: "Bob", style: "A5", price: 2000, page: 200 ).save
|
12
|
+
Book.new( title: "complex 3", author: "Candy", style: "A6", price: 3000, page: 300 ).save
|
13
|
+
|
14
|
+
filter1 = Book.filter( title: /comp/ )
|
15
|
+
filter2 = Book.not( author: /Candy/ )
|
16
|
+
filter3 = Book.and( filter1, filter2 ).select( _id: 0 )
|
17
|
+
filter4 = Book.or( filter1, filter2 ).select( _id: 0 )
|
18
|
+
p filter1.to_condition
|
19
|
+
p filter2.to_condition
|
20
|
+
p filter3.to_condition
|
21
|
+
p filter4.to_condition
|
22
|
+
puts
|
23
|
+
|
24
|
+
filter1.each do |book|
|
25
|
+
p book
|
26
|
+
end
|
27
|
+
puts
|
28
|
+
|
29
|
+
filter2.each do |book|
|
30
|
+
p book
|
31
|
+
end
|
32
|
+
puts
|
33
|
+
|
34
|
+
filter3.each do |book|
|
35
|
+
p book
|
36
|
+
end
|
37
|
+
puts
|
38
|
+
|
39
|
+
filter4.each do |book|
|
40
|
+
p book
|
41
|
+
end
|
42
|
+
puts
|
43
|
+
|
data/sample/save_basic_1.rb
CHANGED
@@ -9,10 +9,10 @@ end
|
|
9
9
|
|
10
10
|
|
11
11
|
book = Book.new
|
12
|
-
book.title = "title basic
|
13
|
-
book.author = "
|
14
|
-
book.style = "
|
15
|
-
book.price =
|
16
|
-
book.page =
|
12
|
+
book.title = "title basic 1"
|
13
|
+
book.author = "Alice"
|
14
|
+
book.style = "A4"
|
15
|
+
book.price = 1000
|
16
|
+
book.page = 100
|
17
17
|
book.save
|
18
18
|
|
data/sample/save_basic_2.rb
CHANGED
data/sample/save_basic_3.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongous
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ARIMA Yasuhiro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongo
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- sample/query_basic_3.rb
|
98
98
|
- sample/query_basic_4.rb
|
99
99
|
- sample/query_basic_5.rb
|
100
|
+
- sample/query_basic_6.rb
|
100
101
|
- sample/query_detail_1.rb
|
101
102
|
- sample/query_detail_2.rb
|
102
103
|
- sample/query_detail_3.rb
|