mongous 0.1.2 → 0.1.3

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
  SHA256:
3
- metadata.gz: 4f126c03248f80e7dd9c674ddff08879a994ecf36d8c443d0bb43694f6a6fd53
4
- data.tar.gz: d9c694cfd344075ec4d744a71562281ba61dac5c3df011154df0b65cd443041e
3
+ metadata.gz: de69281ec1896f88f5b8d5c7c30654a12559cf3a7c97ea7d58cd2fde6e8579fd
4
+ data.tar.gz: 8db5720698450a5f9fcbf912fab5611d800588e02bfea09141c9bf23a1c0a088
5
5
  SHA512:
6
- metadata.gz: 867d5753bac0b0a1bea9b82b4a650fcda7233465cc9e03529488e533464ec86a0d9e387afa220bdc019de07522af96e53e644fbbd92d9e5e465c69af47b9b15e
7
- data.tar.gz: e0cf057b86df920b3e2f47c26c6fedb8bcbf2ccdb5232cebb3ba3b0fd02ee7b71c90274e0ac3a168d01c5d4cd396131ba5872e286c039b91103924e1bb752689
6
+ metadata.gz: 90d5962f8140f085afc07e9b9c9380a6c38aca825dd977d5ee6a1e8d1042ad39514a6185eb9fed38f9a370e794dea2b8c99b5d99adb661dfc0e9306806084bdd
7
+ data.tar.gz: 9c22f0d55962fce5fead46e9d4e0c27d89fb4b87ca268cef9c123c8737d9b5ae8987748d158a3b39d3acf1c21175729005ebce917fdc0d9118c2fc1dd19623d1
@@ -7,9 +7,9 @@ module Mongous
7
7
  end
8
8
  end
9
9
 
10
- def initialize( doc = nil, **hash )
10
+ def initialize( **doc )
11
11
  @doc = {}
12
- (doc || hash).each do |label, value|
12
+ doc.each do |label, value|
13
13
  label = label.to_s
14
14
  @doc[label] = value
15
15
  end
@@ -91,28 +91,8 @@ module Mongous
91
91
  end
92
92
  end
93
93
 
94
- def create( doc = nil, **hash )
95
- self.new( doc || hash ).save
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 )
@@ -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( _filter )
84
+ def filter( conditions )
11
85
  hash = {}
12
- _filter.each do |key, item|
13
- case item
14
- when Array
15
- hash[key] = {"$in"=>item}
86
+ conditions.each do |key, item|
87
+ case key
88
+ when /\$(and|or|nor)/
89
+ hash[key] = item
16
90
 
17
- when Range
18
- _begin_oper = "$gte"
19
- _end_oper = item.exclude_end? ? "$lt" : "$lte"
91
+ else
92
+ case item
93
+ when Array
94
+ hash[key] = {"$in"=>item}
20
95
 
21
- if item.begin && item.end
22
- hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
96
+ when Range
97
+ _begin_oper = "$gte"
98
+ _end_oper = item.exclude_end? ? "$lt" : "$lte"
23
99
 
24
- elsif !item.begin && item.end
25
- hash[key] = { _end_oper => item.end }
100
+ if item.begin && item.end
101
+ hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
26
102
 
27
- elsif item.begin && !item.end
28
- hash[key] = { _begin_oper => item.begin }
103
+ elsif !item.begin && item.end
104
+ hash[key] = { _end_oper => item.end }
29
105
 
30
- else
31
- raise Mongous::Error, "invalid range. : #{ item }"
106
+ elsif item.begin && !item.end
107
+ hash[key] = { _begin_oper => item.begin }
32
108
 
33
- end
109
+ else
110
+ raise Mongous::Error, "invalid range. : #{ item }"
34
111
 
35
- else
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
 
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -8,11 +8,14 @@ class Book
8
8
  end
9
9
 
10
10
 
11
- book = Book.first
12
- p book
11
+ p book = Book.first
12
+ puts
13
+
14
+ pp books = Book.all
13
15
  puts
14
16
 
15
17
  Book.each do |book|
16
18
  p book
17
19
  end
20
+ puts
18
21
 
@@ -8,11 +8,14 @@ class Book
8
8
  end
9
9
 
10
10
 
11
- book = Book.filter( title: "title 1" ).first
12
- p book
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
 
@@ -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
+
@@ -9,10 +9,10 @@ end
9
9
 
10
10
 
11
11
  book = Book.new
12
- book.title = "title basic 2"
13
- book.author = "Bob"
14
- book.style = "A5"
15
- book.price = 2000
16
- book.page = 200
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
 
@@ -8,6 +8,6 @@ class Book
8
8
  end
9
9
 
10
10
 
11
- book = Book.new( title: "title basic 1", author: "Alice", style: "A4", price: 1000, page: 100 )
11
+ book = Book.new( title: "title basic 2", author: "Bob", style: "A5", price: 2000, page: 200 )
12
12
  book.save
13
13
 
@@ -9,5 +9,5 @@ end
9
9
 
10
10
 
11
11
  doc = { title: "title basic 3", author: "Candy", style: "A6", price: 3000, page: 300 }
12
- Book.create( doc )
12
+ Book.create( **doc )
13
13
 
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.2
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-09-30 00:00:00.000000000 Z
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