mongous 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/README.adoc +239 -0
- data/README.ja.adoc +238 -0
- data/Rakefile +96 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mongous.rb +11 -0
- data/lib/mongous/base.rb +68 -0
- data/lib/mongous/document.rb +159 -0
- data/lib/mongous/extention.rb +167 -0
- data/lib/mongous/filter.rb +124 -0
- data/lib/mongous/version.rb +3 -0
- data/mongous.gemspec +26 -0
- data/sample/connect_auto_1.rb +14 -0
- data/sample/connect_auto_2.rb +15 -0
- data/sample/connect_default_1.rb +14 -0
- data/sample/connect_default_2.rb +15 -0
- data/sample/connect_environ_1.rb +14 -0
- data/sample/connect_environ_2.rb +15 -0
- data/sample/connect_loadfile_1.rb +15 -0
- data/sample/connect_loadfile_2.rb +16 -0
- data/sample/connect_mongo.yml +15 -0
- data/sample/declare_compact_1.rb +27 -0
- data/sample/declare_label_1.rb +39 -0
- data/sample/declare_ndex_1.rb +41 -0
- data/sample/declare_ndex_2.rb +41 -0
- data/sample/declare_strict_1.rb +47 -0
- data/sample/declare_strict_2.rb +35 -0
- data/sample/multi_docs_1.rb +27 -0
- data/sample/multi_docs_2.rb +58 -0
- data/sample/query_basic_1.rb +18 -0
- data/sample/query_basic_2.rb +18 -0
- data/sample/query_basic_3.rb +19 -0
- data/sample/query_basic_4.rb +25 -0
- data/sample/query_basic_5.rb +39 -0
- data/sample/query_detail_1.rb +24 -0
- data/sample/query_detail_2.rb +23 -0
- data/sample/query_detail_3.rb +29 -0
- data/sample/query_limit_1.rb +44 -0
- data/sample/query_order_1.rb +49 -0
- data/sample/query_projection_1.rb +44 -0
- data/sample/query_raw_1.rb +33 -0
- data/sample/save_basic_1.rb +13 -0
- data/sample/save_basic_2.rb +18 -0
- data/sample/save_basic_3.rb +13 -0
- data/sample/save_detail_1.rb +34 -0
- data/sample/save_detail_2.rb +34 -0
- data/sample/save_detail_3.rb +40 -0
- data/sample/save_verify_1.rb +46 -0
- data/sample/save_verify_2.rb +28 -0
- data/sample/save_verify_3.rb +26 -0
- data/sample/save_verify_4.rb +29 -0
- data/sample/save_verify_5.rb +23 -0
- data/sample/save_verify_6.rb +23 -0
- data/sample/template_article.rb +5 -0
- data/sample/template_person.rb +12 -0
- data/sample/zap_basic_1.rb +11 -0
- data/sample/zap_basic_2.rb +11 -0
- data/sample/zap_basic_3.rb +11 -0
- metadata +147 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
|
9
|
+
field :title, String, :must
|
10
|
+
field :author, String
|
11
|
+
field :publisher, String
|
12
|
+
field :style, String, %w[A4 A5 A6]
|
13
|
+
field :price, Integer, (0..1_000_000)
|
14
|
+
field :page, Integer, proc{ page % 4 == 0 }
|
15
|
+
field :publish_at, Date, &proc{ Date.today }
|
16
|
+
field :isbn, String, proc{ isbn? }
|
17
|
+
field :lang, String, &proc{ "ja" }
|
18
|
+
|
19
|
+
verify :strict
|
20
|
+
verify { having?( title ) }
|
21
|
+
verify do
|
22
|
+
having?( author ) | having?( publisher )
|
23
|
+
end
|
24
|
+
|
25
|
+
def isbn?
|
26
|
+
isbn&.gsub(/\D*/, '')&.size == 13
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
book = Book.new
|
32
|
+
book.title = "title strict"
|
33
|
+
book.author = "foobar"
|
34
|
+
book.publisher = nil
|
35
|
+
book.style = "A6"
|
36
|
+
book.price = 300
|
37
|
+
book.page = 300
|
38
|
+
# book.publish_at = nil # (default)
|
39
|
+
book.isbn = "978-3-16-148410-0"
|
40
|
+
# book.lang = nil # (default)
|
41
|
+
book.save
|
42
|
+
|
43
|
+
|
44
|
+
Book.each do |book|
|
45
|
+
pp book
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
|
9
|
+
field :title, String, :must
|
10
|
+
field :author, String
|
11
|
+
field :publisher, String, :must
|
12
|
+
field :style, String, %w[A4 A5 A6]
|
13
|
+
field :price, Integer, (0..1_000_000)
|
14
|
+
field :page, Integer, proc{ page % 4 == 0 }
|
15
|
+
field :publish_at, Date, &proc{ Date.today }
|
16
|
+
field :isbn, String, proc{ isbn? }
|
17
|
+
field :lang, String, &proc{ "ja" }
|
18
|
+
|
19
|
+
verify :strict
|
20
|
+
|
21
|
+
def isbn?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
Book.each do |book|
|
28
|
+
begin
|
29
|
+
pp book
|
30
|
+
book.save # cause exception when detected violation.
|
31
|
+
rescue => e
|
32
|
+
p e.message
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book1
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
class Book2
|
11
|
+
include Mongous::Document
|
12
|
+
end
|
13
|
+
|
14
|
+
class Book3
|
15
|
+
include Mongous::Document
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
pp Book1.collection
|
20
|
+
puts
|
21
|
+
|
22
|
+
pp Book2.collection
|
23
|
+
puts
|
24
|
+
|
25
|
+
pp Book3.collection
|
26
|
+
puts
|
27
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book1
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
class Book2
|
11
|
+
include Mongous::Document
|
12
|
+
|
13
|
+
field :title
|
14
|
+
field :publisher
|
15
|
+
field :author
|
16
|
+
field :style
|
17
|
+
field :price
|
18
|
+
field :page
|
19
|
+
field :publish_at
|
20
|
+
field :isbn
|
21
|
+
field :lang
|
22
|
+
|
23
|
+
verify :strict
|
24
|
+
end
|
25
|
+
|
26
|
+
class Book3
|
27
|
+
include Mongous::Document
|
28
|
+
|
29
|
+
field :title, :must
|
30
|
+
field :author,
|
31
|
+
field :publisher,
|
32
|
+
field :price, Integer, (0..1_000_000)
|
33
|
+
field :page, Integer, proc{ page > 0 }
|
34
|
+
field :publish_at, Date, &proc{ Date.today }
|
35
|
+
field :isbn, proc{ isbn? }
|
36
|
+
field :lang, &proc{ "ja" }
|
37
|
+
|
38
|
+
verify :strict
|
39
|
+
verify { having?( title ) }
|
40
|
+
verify do
|
41
|
+
having?( author ) | having?( publisher )
|
42
|
+
end
|
43
|
+
|
44
|
+
def isbn?
|
45
|
+
isbn&.gsub(/[\D]*/, '').size == 13
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
pp Book1.fields
|
51
|
+
puts
|
52
|
+
|
53
|
+
pp Book2.fields
|
54
|
+
puts
|
55
|
+
|
56
|
+
pp Book3.fields
|
57
|
+
puts
|
58
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
Book.filter.option( projection: {_id: 0, title: 1, author: 1} ).each do |book|
|
12
|
+
p book
|
13
|
+
end
|
14
|
+
puts
|
15
|
+
|
16
|
+
Book.filter( title: /title/ ).option( projection: {_id: 0, title: 1} ).each do |book|
|
17
|
+
p book
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
Book.filter( style: %w[A4 A5 A6] ).each do |book|
|
12
|
+
p book
|
13
|
+
end
|
14
|
+
puts
|
15
|
+
|
16
|
+
Book.filter( style: %w[A4 A5]).each do |book|
|
17
|
+
p book
|
18
|
+
end
|
19
|
+
puts
|
20
|
+
|
21
|
+
Book.filter( style: %w[A5] ).each do |book|
|
22
|
+
p book
|
23
|
+
end
|
24
|
+
puts
|
25
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
Book.filter( page: (100..300) ).each do |book|
|
12
|
+
p book
|
13
|
+
end
|
14
|
+
puts
|
15
|
+
|
16
|
+
Book.filter( page: (100...300) ).each do |book|
|
17
|
+
p book
|
18
|
+
end
|
19
|
+
puts
|
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,24 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
|
9
|
+
field :title
|
10
|
+
field :author
|
11
|
+
field :publisher
|
12
|
+
field :style
|
13
|
+
field :price
|
14
|
+
field :page
|
15
|
+
field :publish_at
|
16
|
+
field :isbn
|
17
|
+
field :lang
|
18
|
+
|
19
|
+
verify :strict
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
pp Book.fields
|
24
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
|
9
|
+
field :title
|
10
|
+
field :author
|
11
|
+
field :publisher
|
12
|
+
field :style
|
13
|
+
field :price, Integer
|
14
|
+
field :page, Integer
|
15
|
+
field :publish_at, Date
|
16
|
+
field :isbn
|
17
|
+
field :lang
|
18
|
+
|
19
|
+
verify :strict
|
20
|
+
end
|
21
|
+
|
22
|
+
pp Book.fields
|
23
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Book
|
7
|
+
include Mongous::Document
|
8
|
+
|
9
|
+
field :title, :must
|
10
|
+
field :author
|
11
|
+
field :publisher
|
12
|
+
field :style, %w[ A4 A5 A6 ]
|
13
|
+
field :price, Integer, (0..1_000_000)
|
14
|
+
field :page, Integer, proc{ page > 0 }
|
15
|
+
field :publish_at, Date, &proc{ Date.today }
|
16
|
+
field :isbn, proc{ isbn? }
|
17
|
+
field :lang, &proc{ "ja" }
|
18
|
+
|
19
|
+
verify :strict
|
20
|
+
|
21
|
+
def isbn?
|
22
|
+
return false if isbn.nil?
|
23
|
+
str = isbn.gsub(/\D*/, '')
|
24
|
+
str.size == 13
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
pp Book.fields
|
29
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Item
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
(0...10).each do |n|
|
12
|
+
if item = Item.filter( n: n ).first
|
13
|
+
item
|
14
|
+
else
|
15
|
+
Item.create( n: n )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
puts
|
19
|
+
|
20
|
+
Item.each do |item|
|
21
|
+
p item
|
22
|
+
end
|
23
|
+
puts
|
24
|
+
|
25
|
+
Item.filter.skip(2).each do |item|
|
26
|
+
p item
|
27
|
+
end
|
28
|
+
puts
|
29
|
+
|
30
|
+
Item.filter.limit(3).each do |item|
|
31
|
+
p item
|
32
|
+
end
|
33
|
+
puts
|
34
|
+
|
35
|
+
Item.filter.skip(4).limit(5).each do |item|
|
36
|
+
p item
|
37
|
+
end
|
38
|
+
puts
|
39
|
+
|
40
|
+
Item.filter.offset(4).limit(5).each do |item|
|
41
|
+
p item
|
42
|
+
end
|
43
|
+
puts
|
44
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
require "mongous"
|
3
|
+
|
4
|
+
Mongous.connect!
|
5
|
+
|
6
|
+
class Item
|
7
|
+
include Mongous::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
(0...10).each do |n|
|
12
|
+
if item = Item.filter( n: n ).first
|
13
|
+
item
|
14
|
+
else
|
15
|
+
Item.create( n: n )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
puts
|
19
|
+
|
20
|
+
Item.each do |item|
|
21
|
+
p item
|
22
|
+
end
|
23
|
+
puts
|
24
|
+
|
25
|
+
Item.filter.sort(n: 1).each do |item|
|
26
|
+
p item
|
27
|
+
end
|
28
|
+
puts
|
29
|
+
|
30
|
+
Item.filter.sort(n: -1).each do |item|
|
31
|
+
p item
|
32
|
+
end
|
33
|
+
puts
|
34
|
+
|
35
|
+
Item.filter.order(n: -1).each do |item|
|
36
|
+
p item
|
37
|
+
end
|
38
|
+
puts
|
39
|
+
|
40
|
+
Item.filter( n: (3...8) ).order(n: -1).each do |item|
|
41
|
+
p item
|
42
|
+
end
|
43
|
+
puts
|
44
|
+
|
45
|
+
Item.filter( n: [0,2,4,6,8] ).order(n: -1).each do |item|
|
46
|
+
p item
|
47
|
+
end
|
48
|
+
puts
|
49
|
+
|