mongous 0.1.0 → 0.1.5

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: 95182d94474fd128deef15317dc1131c44d3eee27e920f1cfca3030cc387a3dc
4
- data.tar.gz: 2f11edcd8442be189916eadd722e131c2b63a70030285f42bc7347937351ab66
3
+ metadata.gz: 6616ba889ba858cb82f877ba24bb755ec8e205aa9b8c70738864ce855d5ebb52
4
+ data.tar.gz: d733c6a2bcaeec6725f51f2e7638f8fd9cd99c7decfe568fcf0b80ee98cd3ebb
5
5
  SHA512:
6
- metadata.gz: 337bd873fe2ac4323cd1fd3606a32058f669ffe9aaf8ac6f0778b37959d771d47a79b0a54739f2864d525c6283868f96676a1980631e56bd8e962b4f967d7298
7
- data.tar.gz: 4881ebeeb6e17ccbcfab55ba050485974225a76790f9ccc19a06192c3c179669d5b6aadb3f955f9e2cd673bd2fee8b8562c1713b58418ac898f3c5d1cdfaf972
6
+ metadata.gz: 7ae43077eadf5806fe60e766050a58dfb926de35ebe50f7af0e2f7a37b3b9675112cdd3a80ca95837c93513b0286154caab863f125b3f33d0e4958ed14b6237a
7
+ data.tar.gz: 2e21b03862a403e6839ca2c6e569879c06e3c6f3f5e6fb63409ee6799a0257443a8e2c6612bf4a35956aa4cd931f108c2c3640c8a71c8749fa90b8913b7ac98d
@@ -30,7 +30,7 @@ Or install it yourself as:
30
30
 
31
31
  == Usage
32
32
 
33
- === Simple description
33
+ === Simple declaration
34
34
 
35
35
  [source,ruby]
36
36
  ----
@@ -91,18 +91,16 @@ book = Book.new( title: "title 2", price: 2000, style: "A5" )
91
91
  book.save
92
92
 
93
93
  doc = { title: "title 3", price: 3000, style: "A6" }
94
- Book.create( doc )
94
+ Book.create( **doc )
95
95
  ----
96
96
 
97
97
  === Search document
98
98
 
99
99
  [source,ruby]
100
100
  ----
101
- books = Book.all
102
- pp books
101
+ pp books = Book.all
103
102
 
104
- book = Book.filter( title: "title 1" ).first
105
- p book
103
+ p book = Book.filter( title: "title 1" ).first
106
104
 
107
105
  books = Book.filter( title: /title/ ).all
108
106
  books.each do |book|
@@ -116,6 +114,23 @@ end
116
114
  Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
115
  p book
118
116
  end
117
+
118
+ filter1 = Book.filter( title: /title/ )
119
+ filter2 = Book.filter( price: (1..2000) )
120
+ filter3 = Book.filter( style: ["A4","A5"] )
121
+ Book.not( filter1 ).each do |book|
122
+ p book
123
+ end
124
+ Book.and( filter1, filter2 ).each do |book|
125
+ p book
126
+ end
127
+ Book.or( filter1, filter3 ).each do |book|
128
+ p book
129
+ end
130
+
131
+ Book.find( {}, { projection: {_id: 0} } ).each do |book|
132
+ p book
133
+ end
119
134
  ----
120
135
 
121
136
  === Update document
@@ -155,7 +170,6 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
155
170
  *** database: Database name. (default: "test")
156
171
  *** * Other optional arguments for Mongo::Client.new.
157
172
 
158
- ** label: Field label symbol.
159
173
  === Connect database.
160
174
 
161
175
  [source,ruby]
@@ -164,7 +178,7 @@ Mongous.connect( hosts_or_uri = nil, **opts )
164
178
  ----
165
179
 
166
180
  * Result:
167
- ** Mongous::Client.
181
+ ** Mongo::Client.
168
182
 
169
183
  === Include document functions.
170
184
 
@@ -91,18 +91,16 @@ book = Book.new( title: "title 2", price: 2000, style: "A5" )
91
91
  book.save
92
92
 
93
93
  doc = { title: "title 3", price: 3000, style: "A6" }
94
- Book.create( doc )
94
+ Book.create( **doc )
95
95
  ----
96
96
 
97
97
  === ドキュメント検索
98
98
 
99
99
  [source,ruby]
100
100
  ----
101
- books = Book.all
102
- pp books
101
+ pp books = Book.all
103
102
 
104
- book = Book.filter( title: "title 1" ).first
105
- p book
103
+ p book = Book.filter( title: "title 1" ).first
106
104
 
107
105
  books = Book.filter( title: /title/ ).all
108
106
  books.each do |book|
@@ -116,6 +114,23 @@ end
116
114
  Book.filter( price: (1..2000), style: ["A4","A5"] ).each do |book|
117
115
  p book
118
116
  end
117
+
118
+ filter1 = Book.filter( title: /title/ )
119
+ filter2 = Book.filter( price: (1..2000) )
120
+ filter3 = Book.filter( style: ["A4","A5"] )
121
+ Book.not( filter1 ).each do |book|
122
+ p book
123
+ end
124
+ Book.and( filter1, filter2 ).each do |book|
125
+ p book
126
+ end
127
+ Book.or( filter1, filter3 ).each do |book|
128
+ p book
129
+ end
130
+
131
+ Book.find( {}, { projection: {_id: 0} } ).each do |book|
132
+ p book
133
+ end
119
134
  ----
120
135
 
121
136
  === ドキュメント更新
@@ -163,7 +178,7 @@ Mongous.connect( hosts_or_uri = nil, **opts )
163
178
  ----
164
179
 
165
180
  * Result:
166
- ** Mongous::Client.
181
+ ** Mongo::Client.
167
182
 
168
183
  === ドキュメントの機能を取り入れる.
169
184
 
@@ -95,24 +95,8 @@ module Mongous
95
95
  self.new( **doc ).save
96
96
  end
97
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
98
+ def find( conditios = {}, options = {} )
99
+ self.collection.find( conditios, options )
116
100
  end
117
101
 
118
102
  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
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -3,12 +3,13 @@ require_relative 'lib/mongous/version'
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "mongous"
5
5
  spec.version = Mongous::VERSION
6
- spec.authors = ["ARIMA Yasuhiro"]
6
+ spec.authors = ["arimay"]
7
7
  spec.email = ["arima.yasuhiro@gmail.com"]
8
8
 
9
- spec.summary = %q{ Yet another mongo wrapper library. }
10
- spec.description = %q{ It depends only on mongodb and bson, except for the built-in and standard attachment libraries. }
11
- spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
9
+ spec.summary = %q{ Mongo wrapper library. }
10
+ spec.description = %q{ Yet another lightweight mongo wrapper library. }
11
+ spec.homepage = "https://github.com/arimay/mongous"
12
+ spec.license = "MIT"
12
13
 
13
14
  # Specify which files should be added to the gem when it is released.
14
15
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -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
+
@@ -0,0 +1,32 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ p book = Book.find.first
12
+ puts
13
+
14
+ Book.find.each do |book|
15
+ p book
16
+ end
17
+ puts
18
+
19
+ Book.find( title: /title/ ).each do |book|
20
+ p book
21
+ end
22
+ puts
23
+
24
+ Book.find( {}, { projection: {_id: 0} } ).each do |book|
25
+ p book
26
+ end
27
+ puts
28
+
29
+ Book.find( {title: /title/}, { projection: {_id: 0} } ).each do |book|
30
+ p book
31
+ end
32
+
@@ -8,6 +8,11 @@ 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
12
+ book.title = "title basic 1"
13
+ book.author = "Alice"
14
+ book.style = "A4"
15
+ book.price = 1000
16
+ book.page = 100
12
17
  book.save
13
18
 
@@ -8,11 +8,6 @@ class Book
8
8
  end
9
9
 
10
10
 
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
11
+ book = Book.new( title: "title basic 2", author: "Bob", style: "A5", price: 2000, page: 200 )
17
12
  book.save
18
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.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
- - ARIMA Yasuhiro
7
+ - arimay
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
@@ -52,8 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: " It depends only on mongodb and bson, except for the built-in and standard
56
- attachment libraries. "
55
+ description: " Yet another lightweight mongo wrapper library. "
57
56
  email:
58
57
  - arima.yasuhiro@gmail.com
59
58
  executables: []
@@ -98,13 +97,14 @@ files:
98
97
  - sample/query_basic_3.rb
99
98
  - sample/query_basic_4.rb
100
99
  - sample/query_basic_5.rb
100
+ - sample/query_basic_6.rb
101
101
  - sample/query_detail_1.rb
102
102
  - sample/query_detail_2.rb
103
103
  - sample/query_detail_3.rb
104
104
  - sample/query_limit_1.rb
105
105
  - sample/query_order_1.rb
106
106
  - sample/query_projection_1.rb
107
- - sample/query_raw_1.rb
107
+ - sample/query_super_1.rb
108
108
  - sample/save_basic_1.rb
109
109
  - sample/save_basic_2.rb
110
110
  - sample/save_basic_3.rb
@@ -122,8 +122,9 @@ files:
122
122
  - sample/zap_basic_1.rb
123
123
  - sample/zap_basic_2.rb
124
124
  - sample/zap_basic_3.rb
125
- homepage:
126
- licenses: []
125
+ homepage: https://github.com/arimay/mongous
126
+ licenses:
127
+ - MIT
127
128
  metadata: {}
128
129
  post_install_message:
129
130
  rdoc_options: []
@@ -133,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
134
  requirements:
134
135
  - - ">="
135
136
  - !ruby/object:Gem::Version
136
- version: 2.7.0
137
+ version: '0'
137
138
  required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  requirements:
139
140
  - - ">="
@@ -143,5 +144,5 @@ requirements: []
143
144
  rubygems_version: 3.1.4
144
145
  signing_key:
145
146
  specification_version: 4
146
- summary: Yet another mongo wrapper library.
147
+ summary: Mongo wrapper library.
147
148
  test_files: []
@@ -1,33 +0,0 @@
1
-
2
- require "mongous"
3
-
4
- Mongous.connect!
5
-
6
- class Book
7
- include Mongous::Document
8
- end
9
-
10
-
11
- book = Book.collection.find.first
12
- p book
13
- puts
14
-
15
- Book.collection.find.each do |book|
16
- p book
17
- end
18
- puts
19
-
20
- Book.collection.find( title: /title/ ).each do |book|
21
- p book
22
- end
23
- puts
24
-
25
- Book.collection.find( {}, { projection: {_id: 0, title: 1, author: 1} } ).each do |book|
26
- p book
27
- end
28
- puts
29
-
30
- Book.collection.find( {title: /title/}, { projection: {_id: 0, title: 1, author: 1} } ).each do |book|
31
- p book
32
- end
33
-