mongous 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +142 -27
  3. data/README.ja.adoc +140 -25
  4. data/lib/mongous/document.rb +50 -27
  5. data/lib/mongous/extention.rb +56 -45
  6. data/lib/mongous/filter.rb +76 -57
  7. data/lib/mongous/version.rb +1 -1
  8. data/mongous.gemspec +3 -2
  9. data/sample/connect_auto_2.rb +2 -1
  10. data/sample/connect_default_2.rb +2 -1
  11. data/sample/connect_environ_2.rb +2 -1
  12. data/sample/connect_loadfile_2.rb +2 -1
  13. data/sample/declare_compact_1.rb +1 -1
  14. data/sample/declare_label_1.rb +1 -1
  15. data/sample/declare_ndex_1.rb +1 -1
  16. data/sample/declare_ndex_2.rb +1 -1
  17. data/sample/declare_strict_1.rb +7 -5
  18. data/sample/declare_strict_2.rb +3 -2
  19. data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
  20. data/sample/multi_collection_2.rb +61 -0
  21. data/sample/query_basic_1.rb +3 -1
  22. data/sample/query_basic_2.rb +5 -3
  23. data/sample/query_basic_3.rb +2 -3
  24. data/sample/query_basic_4.rb +3 -4
  25. data/sample/query_basic_5.rb +2 -3
  26. data/sample/query_detail_3.rb +3 -2
  27. data/sample/{query_basic_6.rb → query_filter_1.rb} +8 -5
  28. data/sample/query_filter_2.rb +50 -0
  29. data/sample/query_find_1.rb +31 -0
  30. data/sample/query_projection_1.rb +5 -8
  31. data/sample/query_skip_limit_1.rb +37 -0
  32. data/sample/query_skip_limit_2.rb +49 -0
  33. data/sample/query_sort_order_1.rb +46 -0
  34. data/sample/save_basic_1.rb +0 -1
  35. data/sample/save_basic_2.rb +0 -1
  36. data/sample/save_basic_3.rb +0 -1
  37. data/sample/save_detail_1.rb +4 -3
  38. data/sample/save_detail_2.rb +4 -3
  39. data/sample/save_detail_3.rb +6 -5
  40. data/sample/save_verify_1.rb +3 -2
  41. data/sample/save_verify_3.rb +1 -1
  42. data/sample/zap_basic_2.rb +1 -1
  43. data/sample/zap_basic_3.rb +1 -1
  44. metadata +15 -14
  45. data/sample/multi_docs_2.rb +0 -58
  46. data/sample/query_limit_1.rb +0 -44
  47. data/sample/query_order_1.rb +0 -49
  48. data/sample/query_raw_1.rb +0 -33
  49. data/sample/template_article.rb +0 -5
  50. data/sample/template_person.rb +0 -12
@@ -1,3 +1,3 @@
1
1
  module Mongous
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
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.6.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.
@@ -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,7 +9,7 @@ end
9
9
 
10
10
 
11
11
  book = Book.new
12
- book.title = "title compact"
12
+ book.title = "declare compact"
13
13
  book.author = "foobar"
14
14
  book.publisher = nil
15
15
  book.style = "A4"
@@ -21,7 +21,7 @@ end
21
21
 
22
22
 
23
23
  book = Book.new
24
- book.title = "title label"
24
+ book.title = "declare label"
25
25
  book.author = "foobar"
26
26
  book.publisher = nil
27
27
  book.style = "A5"
@@ -23,7 +23,7 @@ end
23
23
 
24
24
 
25
25
  book = Book.new
26
- book.title = "title strict"
26
+ book.title = "declare index 1"
27
27
  book.author = "foobar"
28
28
  book.publisher = nil
29
29
  book.style = "A6"
@@ -23,7 +23,7 @@ end
23
23
 
24
24
 
25
25
  book = Book.new
26
- book.title = "title strict"
26
+ book.title = "declare index 2"
27
27
  book.author = "foobar"
28
28
  book.publisher = nil
29
29
  book.style = "A6"
@@ -12,9 +12,10 @@ class Book
12
12
  field :style, String, %w[A4 A5 A6]
13
13
  field :price, Integer, (0..1_000_000)
14
14
  field :page, Integer, proc{ page % 4 == 0 }
15
- field :publish_at, Date, &proc{ Date.today }
16
15
  field :isbn, String, proc{ isbn? }
17
- field :lang, String, &proc{ "ja" }
16
+ field :lang, String, default: "en"
17
+ field :created_at, Time, create: ->(){ Time.now }
18
+ field :updated_at, Time, update: ->(){ Time.now }
18
19
 
19
20
  verify :strict
20
21
  verify { having?( title ) }
@@ -23,21 +24,22 @@ class Book
23
24
  end
24
25
 
25
26
  def isbn?
26
- isbn&.gsub(/\D*/, '')&.size == 13
27
+ isbn.gsub(/\D*/, '').size == 13
27
28
  end
28
29
  end
29
30
 
30
31
 
31
32
  book = Book.new
32
- book.title = "title strict"
33
+ book.title = "declare strict"
33
34
  book.author = "foobar"
34
35
  book.publisher = nil
35
36
  book.style = "A6"
36
37
  book.price = 300
37
38
  book.page = 300
38
- # book.publish_at = nil # (default)
39
39
  book.isbn = "978-3-16-148410-0"
40
40
  # book.lang = nil # (default)
41
+ # book.created_at = nil # (create)
42
+ # book.updated_at = nil # (update)
41
43
  book.save
42
44
 
43
45
 
@@ -12,9 +12,10 @@ class Book
12
12
  field :style, String, %w[A4 A5 A6]
13
13
  field :price, Integer, (0..1_000_000)
14
14
  field :page, Integer, proc{ page % 4 == 0 }
15
- field :publish_at, Date, &proc{ Date.today }
16
15
  field :isbn, String, proc{ isbn? }
17
- field :lang, String, &proc{ "ja" }
16
+ field :lang, String, default: "en"
17
+ field :created_at, Time, create: ->(){ Time.now }
18
+ field :updated_at, Time, update: ->(){ Time.now }
18
19
 
19
20
  verify :strict
20
21
 
@@ -0,0 +1,61 @@
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 :price
18
+ field :page
19
+ field :isbn
20
+ field :lang
21
+ field :created_at
22
+ field :updated_at
23
+
24
+ verify :strict
25
+ end
26
+
27
+ class Book3
28
+ include Mongous::Document
29
+
30
+ field :title, :must
31
+ field :author
32
+ field :publisher, String, :must
33
+ field :style, String, %w[A4 A5 A6]
34
+ field :price, Integer, (0..1_000_000)
35
+ field :page, Integer, proc{ page > 0 }
36
+ field :isbn, proc{ isbn? }
37
+ field :lang, String, default: "en"
38
+ field :created_at, Time, create: ->(){ Time.now }
39
+ field :updated_at, Time, update: ->(){ Time.now }
40
+
41
+ verify :strict
42
+ verify { having?( title ) }
43
+ verify do
44
+ having?( author ) | having?( publisher )
45
+ end
46
+
47
+ def isbn?
48
+ isbn.gsub(/[\D]*/, '').size == 13
49
+ end
50
+ end
51
+
52
+
53
+ pp Book1.fields
54
+ puts
55
+
56
+ pp Book2.fields
57
+ puts
58
+
59
+ pp Book3.fields
60
+ puts
61
+
@@ -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( style: %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( style: %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( style: %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
@@ -12,9 +12,10 @@ class Book
12
12
  field :style, %w[ A4 A5 A6 ]
13
13
  field :price, Integer, (0..1_000_000)
14
14
  field :page, Integer, proc{ page > 0 }
15
- field :publish_at, Date, &proc{ Date.today }
16
15
  field :isbn, proc{ isbn? }
17
- field :lang, &proc{ "ja" }
16
+ field :lang, default: "en"
17
+ field :created_at, Time, create: ->(){ Time.now }
18
+ field :updated_at, Time, update: ->(){ Time.now }
18
19
 
19
20
  verify :strict
20
21
 
@@ -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, style: "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( style: %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, style: "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
+
@@ -0,0 +1,31 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+ p book = Book.find.first
11
+ puts
12
+
13
+ Book.find.each do |book|
14
+ p book
15
+ end
16
+ puts
17
+
18
+ Book.find( title: /title/ ).each do |book|
19
+ p book
20
+ end
21
+ puts
22
+
23
+ Book.find( {}, { projection: {_id: 0} } ).each do |book|
24
+ p book
25
+ end
26
+ puts
27
+
28
+ Book.find( {title: /title/}, { projection: {_id: 0} } ).each do |book|
29
+ p book
30
+ end
31
+
@@ -7,11 +7,8 @@ class Label
7
7
  include Mongous::Document
8
8
  end
9
9
 
10
-
11
10
  (0...10).each do |n|
12
- if label = Label.filter( n: n ).first
13
- label
14
- else
11
+ unless Label.where( n: n ).first
15
12
  Label.create( n: n, tag: [0x40 + n].pack("C") )
16
13
  end
17
14
  end
@@ -22,22 +19,22 @@ Label.each do |label|
22
19
  end
23
20
  puts
24
21
 
25
- Label.filter.projection( tag: 1 ).each do |label|
22
+ Label.where.projection( tag: 1 ).each do |label|
26
23
  p label
27
24
  end
28
25
  puts
29
26
 
30
- Label.filter.projection( _id: 0, n: 1 ).each do |label|
27
+ Label.where.projection( _id: 0, n: 1 ).each do |label|
31
28
  p label
32
29
  end
33
30
  puts
34
31
 
35
- Label.filter.select( _id: 0, n: 1, tag: 1 ).each do |label|
32
+ Label.where.select( _id: 0, n: 1, tag: 1 ).each do |label|
36
33
  p label
37
34
  end
38
35
  puts
39
36
 
40
- Label.filter( n: [0,2,4,6,8] ).select( _id: 0, n: 1, tag: 1 ).each do |label|
37
+ Label.where( n: [0,2,4,6,8] ).select( _id: 0, n: 1, tag: 1 ).each do |label|
41
38
  p label
42
39
  end
43
40
  puts