mongous 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +5 -0
  6. data/README.adoc +239 -0
  7. data/README.ja.adoc +238 -0
  8. data/Rakefile +96 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/mongous.rb +11 -0
  12. data/lib/mongous/base.rb +68 -0
  13. data/lib/mongous/document.rb +159 -0
  14. data/lib/mongous/extention.rb +167 -0
  15. data/lib/mongous/filter.rb +124 -0
  16. data/lib/mongous/version.rb +3 -0
  17. data/mongous.gemspec +26 -0
  18. data/sample/connect_auto_1.rb +14 -0
  19. data/sample/connect_auto_2.rb +15 -0
  20. data/sample/connect_default_1.rb +14 -0
  21. data/sample/connect_default_2.rb +15 -0
  22. data/sample/connect_environ_1.rb +14 -0
  23. data/sample/connect_environ_2.rb +15 -0
  24. data/sample/connect_loadfile_1.rb +15 -0
  25. data/sample/connect_loadfile_2.rb +16 -0
  26. data/sample/connect_mongo.yml +15 -0
  27. data/sample/declare_compact_1.rb +27 -0
  28. data/sample/declare_label_1.rb +39 -0
  29. data/sample/declare_ndex_1.rb +41 -0
  30. data/sample/declare_ndex_2.rb +41 -0
  31. data/sample/declare_strict_1.rb +47 -0
  32. data/sample/declare_strict_2.rb +35 -0
  33. data/sample/multi_docs_1.rb +27 -0
  34. data/sample/multi_docs_2.rb +58 -0
  35. data/sample/query_basic_1.rb +18 -0
  36. data/sample/query_basic_2.rb +18 -0
  37. data/sample/query_basic_3.rb +19 -0
  38. data/sample/query_basic_4.rb +25 -0
  39. data/sample/query_basic_5.rb +39 -0
  40. data/sample/query_detail_1.rb +24 -0
  41. data/sample/query_detail_2.rb +23 -0
  42. data/sample/query_detail_3.rb +29 -0
  43. data/sample/query_limit_1.rb +44 -0
  44. data/sample/query_order_1.rb +49 -0
  45. data/sample/query_projection_1.rb +44 -0
  46. data/sample/query_raw_1.rb +33 -0
  47. data/sample/save_basic_1.rb +13 -0
  48. data/sample/save_basic_2.rb +18 -0
  49. data/sample/save_basic_3.rb +13 -0
  50. data/sample/save_detail_1.rb +34 -0
  51. data/sample/save_detail_2.rb +34 -0
  52. data/sample/save_detail_3.rb +40 -0
  53. data/sample/save_verify_1.rb +46 -0
  54. data/sample/save_verify_2.rb +28 -0
  55. data/sample/save_verify_3.rb +26 -0
  56. data/sample/save_verify_4.rb +29 -0
  57. data/sample/save_verify_5.rb +23 -0
  58. data/sample/save_verify_6.rb +23 -0
  59. data/sample/template_article.rb +5 -0
  60. data/sample/template_person.rb +12 -0
  61. data/sample/zap_basic_1.rb +11 -0
  62. data/sample/zap_basic_2.rb +11 -0
  63. data/sample/zap_basic_3.rb +11 -0
  64. 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,18 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ book = Book.first
12
+ p book
13
+ puts
14
+
15
+ Book.each do |book|
16
+ p book
17
+ end
18
+
@@ -0,0 +1,18 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ book = Book.filter( title: "title 1" ).first
12
+ p book
13
+ puts
14
+
15
+ Book.filter( title: /title/ ).each do |book|
16
+ p book
17
+ end
18
+
@@ -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
+