mongous 0.1.1 → 0.1.6

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: 3ffb544bfae82a4597345d166e314e50eb6e251f44f57c5da4dfc9fab6680970
4
- data.tar.gz: 909fcdcc00bb4ea65bf691b79420a9c6bed4fee4e07cfe0dee8c8da2bbd344a4
3
+ metadata.gz: '099aa151de9cf50a6c25b861ad877eafc6e16972f12f463181d735d8fb4ae71f'
4
+ data.tar.gz: b41e34b02325dcb2933ec9e195452d5e2a027ee375d56cb0fe44a2144b275928
5
5
  SHA512:
6
- metadata.gz: 21bacfb05dbf0d0c2193759e50df55ff4ae7432522fea90ee30688f6ccc6e9a42b9645dac9f41f3a0b0ce1114c63cbdc9a18ae633b8db84639a1b8cc8ea7a49a
7
- data.tar.gz: 2eef0a2fecffd43f12cbe65cea598795835727248af48c9fc45385f23a0f23ec7dda910daea6bc66b9bd2906e443f95ceefd3f644046e2cd4b36793347d2b067
6
+ metadata.gz: a058325e628afba439934ea18e82a2cbc80e4053c00aff7c7ca3326da6be2b588a37361152c359e4ed558f375ab2c7bc1e77f6fe8968a62bdc432733298fc660
7
+ data.tar.gz: 8bc4c0bdef2be7e1c4bad582a5c0dc28f167fb0f99a7a74b800793b84c19db68a5a20564d8f7a6d6d41d210c78c78670bdd63ef12aec20f2375c56bb83237120
@@ -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
@@ -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
  === Include document functions.
169
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.new( self ).filter( filter ).to_condition
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.1"
2
+ VERSION = "0.1.6"
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
9
  spec.summary = %q{ Mongo wrapper library. }
10
10
  spec.description = %q{ Yet another lightweight mongo wrapper library. }
11
- spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
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( price: (2000...3000) )
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.1
4
+ version: 0.1.6
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-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo
@@ -97,13 +97,14 @@ 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
103
104
  - sample/query_limit_1.rb
104
105
  - sample/query_order_1.rb
105
106
  - sample/query_projection_1.rb
106
- - sample/query_raw_1.rb
107
+ - sample/query_super_1.rb
107
108
  - sample/save_basic_1.rb
108
109
  - sample/save_basic_2.rb
109
110
  - sample/save_basic_3.rb
@@ -121,8 +122,9 @@ files:
121
122
  - sample/zap_basic_1.rb
122
123
  - sample/zap_basic_2.rb
123
124
  - sample/zap_basic_3.rb
124
- homepage:
125
- licenses: []
125
+ homepage: https://github.com/arimay/mongous
126
+ licenses:
127
+ - MIT
126
128
  metadata: {}
127
129
  post_install_message:
128
130
  rdoc_options: []
@@ -132,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
134
  requirements:
133
135
  - - ">="
134
136
  - !ruby/object:Gem::Version
135
- version: 2.7.0
137
+ version: '0'
136
138
  required_rubygems_version: !ruby/object:Gem::Requirement
137
139
  requirements:
138
140
  - - ">="
@@ -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
-