mongous 0.1.4 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +156 -37
  3. data/README.ja.adoc +155 -36
  4. data/lib/mongous/document.rb +66 -31
  5. data/lib/mongous/extention.rb +59 -46
  6. data/lib/mongous/filter.rb +82 -60
  7. data/lib/mongous/version.rb +1 -1
  8. data/sample/connect_auto_2.rb +2 -1
  9. data/sample/connect_default_2.rb +2 -1
  10. data/sample/connect_environ_2.rb +2 -1
  11. data/sample/connect_loadfile_2.rb +2 -1
  12. data/sample/declare_compact_1.rb +4 -3
  13. data/sample/declare_label_1.rb +5 -3
  14. data/sample/declare_ndex_1.rb +5 -3
  15. data/sample/declare_ndex_2.rb +5 -3
  16. data/sample/declare_strict_1.rb +14 -10
  17. data/sample/declare_strict_2.rb +5 -3
  18. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
  19. data/sample/multi_collection_2.rb +63 -0
  20. data/sample/query_basic_1.rb +3 -1
  21. data/sample/query_basic_2.rb +5 -3
  22. data/sample/query_basic_3.rb +2 -3
  23. data/sample/query_basic_4.rb +3 -4
  24. data/sample/query_basic_5.rb +2 -3
  25. data/sample/query_detail_1.rb +1 -0
  26. data/sample/query_detail_2.rb +1 -0
  27. data/sample/query_detail_3.rb +5 -3
  28. data/sample/{query_basic_6.rb → query_filter_1.rb} +8 -5
  29. data/sample/query_filter_2.rb +50 -0
  30. data/sample/query_find_1.rb +31 -0
  31. data/sample/query_projection_1.rb +5 -8
  32. data/sample/query_skip_limit_1.rb +47 -0
  33. data/sample/query_skip_limit_2.rb +49 -0
  34. data/sample/query_sort_order_1.rb +46 -0
  35. data/sample/save_basic_1.rb +1 -2
  36. data/sample/save_basic_2.rb +1 -2
  37. data/sample/save_basic_3.rb +1 -2
  38. data/sample/save_detail_1.rb +7 -4
  39. data/sample/save_detail_2.rb +7 -4
  40. data/sample/save_detail_3.rb +10 -7
  41. data/sample/save_verify_1.rb +7 -4
  42. data/sample/save_verify_3.rb +1 -1
  43. data/sample/save_verify_5.rb +3 -3
  44. data/sample/save_verify_6.rb +3 -3
  45. data/sample/save_verify_7.rb +23 -0
  46. data/sample/update_basic_1.rb +13 -0
  47. data/sample/zap_basic_2.rb +2 -2
  48. metadata +12 -11
  49. data/sample/multi_docs_2.rb +0 -58
  50. data/sample/query_limit_1.rb +0 -44
  51. data/sample/query_order_1.rb +0 -49
  52. data/sample/query_raw_1.rb +0 -33
  53. data/sample/template_article.rb +0 -5
  54. data/sample/template_person.rb +0 -12
  55. data/sample/zap_basic_3.rb +0 -11
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -5,7 +5,8 @@ $client = Mongous.connect
5
5
 
6
6
  class Book
7
7
  include Mongous::Document
8
- set_client $client
8
+ self.client = $client
9
+ self.collection_name = "Book"
9
10
  end
10
11
 
11
12
 
@@ -5,7 +5,8 @@ $client = Mongous.connect( ["localhost:27017"], database: "test" )
5
5
 
6
6
  class Book
7
7
  include Mongous::Document
8
- set_client $client
8
+ self.client = $client
9
+ self.collection_name = "Book"
9
10
  end
10
11
 
11
12
 
@@ -5,7 +5,8 @@ $client = Mongous.connect( ENV["MONGOLAB_URI"] || "mongodb://localhost:27017/t
5
5
 
6
6
  class Book
7
7
  include Mongous::Document
8
- set_client $client
8
+ self.client = $client
9
+ self.collection_name = "Book"
9
10
  end
10
11
 
11
12
 
@@ -6,7 +6,8 @@ $client = Mongous.connect( file: filepath, mode: ENV["RACK_ENV"] || "developme
6
6
 
7
7
  class Book
8
8
  include Mongous::Document
9
- set_client $client
9
+ self.client = $client
10
+ self.collection_name = "Book"
10
11
  end
11
12
 
12
13
 
@@ -9,10 +9,11 @@ end
9
9
 
10
10
 
11
11
  book = Book.new
12
- book.title = "title compact"
13
- book.author = "foobar"
12
+ book.title = "declare compact"
13
+ book.author = "Alice"
14
14
  book.publisher = nil
15
- book.style = "A4"
15
+ book.style = "hardcover"
16
+ book.size = "A4"
16
17
  book.price = 100
17
18
  book.page = 100
18
19
  # book.publish_at = nil # (default)
@@ -10,6 +10,7 @@ class Book
10
10
  field :author
11
11
  field :publisher
12
12
  field :style
13
+ field :size
13
14
  field :price
14
15
  field :page
15
16
  field :publish_at
@@ -21,10 +22,11 @@ end
21
22
 
22
23
 
23
24
  book = Book.new
24
- book.title = "title label"
25
- book.author = "foobar"
25
+ book.title = "declare label"
26
+ book.author = "Bob"
26
27
  book.publisher = nil
27
- book.style = "A5"
28
+ book.style = "softcover"
29
+ book.size = "A5"
28
30
  book.price = 200
29
31
  book.page = 200
30
32
  # book.publish_at = nil # (default)
@@ -10,6 +10,7 @@ class Book
10
10
  field :author
11
11
  field :publisher
12
12
  field :style
13
+ field :size
13
14
  field :price
14
15
  field :page
15
16
  field :publish_at
@@ -23,10 +24,11 @@ end
23
24
 
24
25
 
25
26
  book = Book.new
26
- book.title = "title strict"
27
- book.author = "foobar"
27
+ book.title = "declare index 1"
28
+ book.author = "Candy"
28
29
  book.publisher = nil
29
- book.style = "A6"
30
+ book.style = "paperback"
31
+ book.size = "A6"
30
32
  book.price = 300
31
33
  book.page = 300
32
34
  # book.publish_at = nil # (default)
@@ -10,6 +10,7 @@ class Book
10
10
  field :author
11
11
  field :publisher
12
12
  field :style
13
+ field :size
13
14
  field :price
14
15
  field :page
15
16
  field :publish_at
@@ -23,10 +24,11 @@ end
23
24
 
24
25
 
25
26
  book = Book.new
26
- book.title = "title strict"
27
- book.author = "foobar"
27
+ book.title = "declare index 2"
28
+ book.author = "Candy"
28
29
  book.publisher = nil
29
- book.style = "A6"
30
+ book.style = "paperback"
31
+ book.size = "A6"
30
32
  book.price = 300
31
33
  book.page = 300
32
34
  # book.publish_at = nil # (default)
@@ -9,12 +9,14 @@ class Book
9
9
  field :title, String, :must
10
10
  field :author, String
11
11
  field :publisher, String
12
- field :style, String, %w[A4 A5 A6]
12
+ field :style, String, %w[hardcover softcover paperback]
13
+ field :size, String, /[AB]\d/
13
14
  field :price, Integer, (0..1_000_000)
14
15
  field :page, Integer, proc{ page % 4 == 0 }
15
- field :publish_at, Date, &proc{ Date.today }
16
16
  field :isbn, String, proc{ isbn? }
17
- field :lang, String, &proc{ "ja" }
17
+ field :lang, String, default: "en"
18
+ field :created_at, Time, create: proc{ Time.now }
19
+ field :updated_at, Time, update: proc{ Time.now }
18
20
 
19
21
  verify :strict
20
22
  verify { having?( title ) }
@@ -23,21 +25,23 @@ class Book
23
25
  end
24
26
 
25
27
  def isbn?
26
- isbn&.gsub(/\D*/, '')&.size == 13
28
+ isbn.gsub(/\D*/, '').size == 13
27
29
  end
28
30
  end
29
31
 
30
32
 
31
33
  book = Book.new
32
- book.title = "title strict"
33
- book.author = "foobar"
34
+ book.title = "declare strict"
35
+ book.author = "David"
34
36
  book.publisher = nil
35
- book.style = "A6"
36
- book.price = 300
37
- book.page = 300
38
- # book.publish_at = nil # (default)
37
+ book.style = "paperback"
38
+ book.size = "A7"
39
+ book.price = 400
40
+ book.page = 400
39
41
  book.isbn = "978-3-16-148410-0"
40
42
  # book.lang = nil # (default)
43
+ # book.created_at = nil # (create)
44
+ # book.updated_at = nil # (update)
41
45
  book.save
42
46
 
43
47
 
@@ -9,12 +9,14 @@ class Book
9
9
  field :title, String, :must
10
10
  field :author, String
11
11
  field :publisher, String, :must
12
- field :style, String, %w[A4 A5 A6]
12
+ field :style, String, %w[hardcover softcover paperback]
13
+ field :size, String, /[AB]\d/
13
14
  field :price, Integer, (0..1_000_000)
14
15
  field :page, Integer, proc{ page % 4 == 0 }
15
- field :publish_at, Date, &proc{ Date.today }
16
16
  field :isbn, String, proc{ isbn? }
17
- field :lang, String, &proc{ "ja" }
17
+ field :lang, String, default: "en"
18
+ field :created_at, Time, create: proc{ Time.now }
19
+ field :updated_at, Time, update: proc{ Time.now }
18
20
 
19
21
  verify :strict
20
22
 
@@ -0,0 +1,63 @@
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 :author
15
+ field :publisher
16
+ field :style
17
+ field :size
18
+ field :price
19
+ field :page
20
+ field :isbn
21
+ field :lang
22
+ field :created_at
23
+ field :updated_at
24
+
25
+ verify :strict
26
+ end
27
+
28
+ class Book3
29
+ include Mongous::Document
30
+
31
+ field :title, :must
32
+ field :author
33
+ field :publisher, String, :must
34
+ field :style, String, %w[hardcover softcover paperback]
35
+ field :size, String, /[AB]\d/
36
+ field :price, Integer, (0..1_000_000)
37
+ field :page, Integer, proc{ page > 0 }
38
+ field :isbn, proc{ isbn? }
39
+ field :lang, String, default: "en"
40
+ field :created_at, Time, create: proc{ Time.now }
41
+ field :updated_at, Time, update: proc{ Time.now }
42
+
43
+ verify :strict
44
+ verify { having?( title ) }
45
+ verify do
46
+ having?( author ) | having?( publisher )
47
+ end
48
+
49
+ def isbn?
50
+ isbn.gsub(/[\D]*/, '').size == 13
51
+ end
52
+ end
53
+
54
+
55
+ pp Book1.fields
56
+ puts
57
+
58
+ pp Book2.fields
59
+ puts
60
+
61
+ pp Book3.fields
62
+ puts
63
+
@@ -7,10 +7,12 @@ class Book
7
7
  include Mongous::Document
8
8
  end
9
9
 
10
-
11
10
  p book = Book.first
12
11
  puts
13
12
 
13
+ p count = Book.count
14
+ puts
15
+
14
16
  pp books = Book.all
15
17
  puts
16
18
 
@@ -7,14 +7,16 @@ class Book
7
7
  include Mongous::Document
8
8
  end
9
9
 
10
+ p book = Book.where( title: /title/ ).first
11
+ puts
10
12
 
11
- p book = Book.filter( title: /title/ ).first
13
+ p count = Book.where( title: /title/ ).count
12
14
  puts
13
15
 
14
- pp books = Book.filter( title: /title/ ).all
16
+ pp books = Book.where( title: /title/ ).all
15
17
  puts
16
18
 
17
- Book.filter( title: /title/ ).each do |book|
19
+ Book.where( title: /title/ ).each do |book|
18
20
  p book
19
21
  end
20
22
  puts
@@ -7,13 +7,12 @@ class Book
7
7
  include Mongous::Document
8
8
  end
9
9
 
10
-
11
- Book.filter.option( projection: {_id: 0, title: 1, author: 1} ).each do |book|
10
+ Book.where.option( projection: {_id: 0, title: 1, author: 1} ).each do |book|
12
11
  p book
13
12
  end
14
13
  puts
15
14
 
16
- Book.filter( title: /title/ ).option( projection: {_id: 0, title: 1} ).each do |book|
15
+ Book.where( title: /title/ ).option( projection: {_id: 0, title: 1} ).each do |book|
17
16
  p book
18
17
  end
19
18
 
@@ -7,18 +7,17 @@ class Book
7
7
  include Mongous::Document
8
8
  end
9
9
 
10
-
11
- Book.filter( style: %w[A4 A5 A6] ).each do |book|
10
+ Book.where( size: %w[A4 A5 A6] ).each do |book|
12
11
  p book
13
12
  end
14
13
  puts
15
14
 
16
- Book.filter( style: %w[A4 A5]).each do |book|
15
+ Book.where( size: %w[A4 A5]).each do |book|
17
16
  p book
18
17
  end
19
18
  puts
20
19
 
21
- Book.filter( style: %w[A5] ).each do |book|
20
+ Book.where( size: %w[A5] ).each do |book|
22
21
  p book
23
22
  end
24
23
  puts
@@ -7,13 +7,12 @@ class Book
7
7
  include Mongous::Document
8
8
  end
9
9
 
10
-
11
- Book.filter( page: (100..300) ).each do |book|
10
+ Book.where( page: (100..300) ).each do |book|
12
11
  p book
13
12
  end
14
13
  puts
15
14
 
16
- Book.filter( page: (100...300) ).each do |book|
15
+ Book.where( page: (100...300) ).each do |book|
17
16
  p book
18
17
  end
19
18
  puts
@@ -10,6 +10,7 @@ class Book
10
10
  field :author
11
11
  field :publisher
12
12
  field :style
13
+ field :size
13
14
  field :price
14
15
  field :page
15
16
  field :publish_at
@@ -10,6 +10,7 @@ class Book
10
10
  field :author
11
11
  field :publisher
12
12
  field :style
13
+ field :size
13
14
  field :price, Integer
14
15
  field :page, Integer
15
16
  field :publish_at, Date
@@ -9,12 +9,14 @@ class Book
9
9
  field :title, :must
10
10
  field :author
11
11
  field :publisher
12
- field :style, %w[ A4 A5 A6 ]
12
+ field :style, %w[hardcover softcover paperback]
13
+ field :size, /[AB]\d/
13
14
  field :price, Integer, (0..1_000_000)
14
15
  field :page, Integer, proc{ page > 0 }
15
- field :publish_at, Date, &proc{ Date.today }
16
16
  field :isbn, proc{ isbn? }
17
- field :lang, &proc{ "ja" }
17
+ field :lang, default: "en"
18
+ field :created_at, Time, create: proc{ Time.now }
19
+ field :updated_at, Time, update: proc{ Time.now }
18
20
 
19
21
  verify :strict
20
22
 
@@ -7,12 +7,15 @@ class Book
7
7
  include Mongous::Document
8
8
  end
9
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
10
+ (1..3).each do |n|
11
+ unless Book.where( title: "complex #{n}" ).first
12
+ Book.create( title: "complex #{n}", author: (0x40 + n).chr, size: "A#{n + 3}", price: n * 1000, page: n * 100 )
13
+ end
14
+ end
15
+ puts
13
16
 
14
- filter1 = Book.filter( title: /comp/ )
15
- filter2 = Book.not( author: /Candy/ )
17
+ filter1 = Book.where( title: /comp/ )
18
+ filter2 = Book.not( price: (2000...3000) )
16
19
  filter3 = Book.and( filter1, filter2 ).select( _id: 0 )
17
20
  filter4 = Book.or( filter1, filter2 ).select( _id: 0 )
18
21
  p filter1.to_condition
@@ -0,0 +1,50 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+
9
+ filter :title1, { title: /filter/ }
10
+ filter :price1, { price: (2000..3000) }
11
+ filter :page1, where( size: %w[A3 A4] )
12
+ end
13
+
14
+ (1..5).each do |n|
15
+ unless Book.where( title: "filter #{n}" ).first
16
+ Book.create( title: "filter #{n}", author: (0x40 + n).chr, size: "A#{n + 3}", price: n * 1000, page: n * 100 )
17
+ end
18
+ end
19
+ puts
20
+
21
+ filter1 = Book.where( :price1 )
22
+ filter2 = Book.not( :page1 )
23
+ filter3 = Book.and( :title1, filter1, filter2 ).select( _id: 0 )
24
+ filter4 = Book.and( :title1, Book.or( filter1, filter2 ) ).select( _id: 0 )
25
+ p filter1.to_condition
26
+ p filter2.to_condition
27
+ p filter3.to_condition
28
+ p filter4.to_condition
29
+ puts
30
+
31
+ filter1.each do |book|
32
+ p book
33
+ end
34
+ puts
35
+
36
+ filter2.each do |book|
37
+ p book
38
+ end
39
+ puts
40
+
41
+ filter3.each do |book|
42
+ p book
43
+ end
44
+ puts
45
+
46
+ filter4.each do |book|
47
+ p book
48
+ end
49
+ puts
50
+