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,124 @@
1
+
2
+ module Mongous
3
+ class Filter
4
+ def initialize( klass )
5
+ @klass = klass
6
+ @filter = {}
7
+ @option = {}
8
+ end
9
+
10
+ def filter( _filter )
11
+ hash = {}
12
+ _filter.each do |key, item|
13
+ case item
14
+ when Array
15
+ hash[key] = {"$in"=>item}
16
+
17
+ when Range
18
+ _begin_oper = "$gte"
19
+ _end_oper = item.exclude_end? ? "$lt" : "$lte"
20
+
21
+ if item.begin && item.end
22
+ hash[key] = { _begin_oper => item.begin, _end_oper => item.end }
23
+
24
+ elsif !item.begin && item.end
25
+ hash[key] = { _end_oper => item.end }
26
+
27
+ elsif item.begin && !item.end
28
+ hash[key] = { _begin_oper => item.begin }
29
+
30
+ else
31
+ raise Mongous::Error, "invalid range. : #{ item }"
32
+
33
+ end
34
+
35
+ else
36
+ hash[key] = item
37
+
38
+ end
39
+ end
40
+ @filter.merge!( hash )
41
+ self.dup
42
+ end
43
+
44
+ def option( _option )
45
+ self.option!( _option )
46
+ self.dup
47
+ end
48
+
49
+ def option!( _option )
50
+ @option.merge!( _option )
51
+ end
52
+
53
+ def projection( _projection )
54
+ self.projection!( _projection )
55
+ self.dup
56
+ end
57
+ alias :select :projection
58
+
59
+ def projection!( _projection )
60
+ @projection = _projection
61
+ end
62
+
63
+ def sort( _sort )
64
+ self.sort!( _sort )
65
+ self.dup
66
+ end
67
+ alias :order :sort
68
+
69
+ def sort!( _sort )
70
+ @sort = _sort
71
+ end
72
+
73
+ def skip( _skip )
74
+ self.skip!( _skip )
75
+ self.dup
76
+ end
77
+ alias :offset :skip
78
+
79
+ def skip!( _skip )
80
+ @skip = _skip
81
+ end
82
+
83
+ def limit( _limit )
84
+ self.limit!( _limit )
85
+ self.dup
86
+ end
87
+
88
+ def limit!( _limit )
89
+ @limit = _limit
90
+ end
91
+
92
+ def do_find
93
+ _filter = @filter
94
+ _option = @option
95
+ _option[:projection] = @projection if @projection
96
+ found = @klass.collection.find( _filter, _option )
97
+ found = found.sort( @sort ) if @sort
98
+ found = found.skip( @skip ) if @skip
99
+ found = found.limit( @limit ) if @limit
100
+ found
101
+ end
102
+
103
+ def first
104
+ doc = do_find.first
105
+ @klass.new( **doc ) if doc
106
+ end
107
+
108
+ def all
109
+ do_find.map do |doc|
110
+ @klass.new( **doc )
111
+ end
112
+ end
113
+
114
+ def each( &block )
115
+ all.each( &block )
116
+ end
117
+
118
+ def delete
119
+ _filter = @filter
120
+ @klass.collection.delete_many( _filter )
121
+ end
122
+ end
123
+ end
124
+
@@ -0,0 +1,3 @@
1
+ module Mongous
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/mongous/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "mongous"
5
+ spec.version = Mongous::VERSION
6
+ spec.authors = ["ARIMA Yasuhiro"]
7
+ spec.email = ["arima.yasuhiro@gmail.com"]
8
+
9
+ spec.summary = %q{ Yet another mongo wrapper library. }
10
+ spec.description = %q{ It depends only on mongodb and bson, except for the built-in and standard attachment libraries. }
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
12
+
13
+ # Specify which files should be added to the gem when it is released.
14
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "mongo", "~> 2.13"
23
+
24
+ spec.add_development_dependency "rake", "~> 12.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,14 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ Book.each do |book|
12
+ p book
13
+ end
14
+
@@ -0,0 +1,15 @@
1
+
2
+ require "mongous"
3
+
4
+ $client = Mongous.connect
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ set_client $client
9
+ end
10
+
11
+
12
+ Book.each do |book|
13
+ p book
14
+ end
15
+
@@ -0,0 +1,14 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!( ["localhost:27017"], database: "test" )
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ Book.each do |book|
12
+ p book
13
+ end
14
+
@@ -0,0 +1,15 @@
1
+
2
+ require "mongous"
3
+
4
+ $client = Mongous.connect( ["localhost:27017"], database: "test" )
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ set_client $client
9
+ end
10
+
11
+
12
+ Book.each do |book|
13
+ p book
14
+ end
15
+
@@ -0,0 +1,14 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!( ENV["MONGOLAB_URI"] || "mongodb://localhost:27017/test" )
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ Book.each do |book|
12
+ p book
13
+ end
14
+
@@ -0,0 +1,15 @@
1
+
2
+ require "mongous"
3
+
4
+ $client = Mongous.connect( ENV["MONGOLAB_URI"] || "mongodb://localhost:27017/test" )
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ set_client $client
9
+ end
10
+
11
+
12
+ Book.each do |book|
13
+ p book
14
+ end
15
+
@@ -0,0 +1,15 @@
1
+
2
+ require "mongous"
3
+
4
+ filepath = File.join( File.dirname(__FILE__), "connect_mongo.yml" )
5
+ Mongous.connect!( file: filepath, mode: ENV["RACK_ENV"] || "development" )
6
+
7
+ class Book
8
+ include Mongous::Document
9
+ end
10
+
11
+
12
+ Book.each do |book|
13
+ p book
14
+ end
15
+
@@ -0,0 +1,16 @@
1
+
2
+ require "mongous"
3
+
4
+ filepath = File.join( File.dirname(__FILE__), "connect_mongo.yml" )
5
+ $client = Mongous.connect( file: filepath, mode: ENV["RACK_ENV"] || "development" )
6
+
7
+ class Book
8
+ include Mongous::Document
9
+ set_client $client
10
+ end
11
+
12
+
13
+ Book.each do |book|
14
+ p book
15
+ end
16
+
@@ -0,0 +1,15 @@
1
+
2
+ development:
3
+ clients:
4
+ default:
5
+ database: test
6
+ hosts:
7
+ - localhost:27017
8
+ options:
9
+ max_pool_size: 5
10
+
11
+ production:
12
+ clients:
13
+ default:
14
+ uri: <%= ENV['MONGOLAB_URI'] %>
15
+
@@ -0,0 +1,27 @@
1
+
2
+ require "mongous"
3
+
4
+ Mongous.connect!
5
+
6
+ class Book
7
+ include Mongous::Document
8
+ end
9
+
10
+
11
+ book = Book.new
12
+ book.title = "title compact"
13
+ book.author = "foobar"
14
+ book.publisher = nil
15
+ book.style = "A4"
16
+ book.price = 100
17
+ book.page = 100
18
+ # book.publish_at = nil # (default)
19
+ # book.isbn = "978-3-16-148410-0"
20
+ # book.lang = nil # (default)
21
+ book.save
22
+
23
+
24
+ Book.each do |book|
25
+ pp book
26
+ end
27
+
@@ -0,0 +1,39 @@
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
+ book = Book.new
24
+ book.title = "title label"
25
+ book.author = "foobar"
26
+ book.publisher = nil
27
+ book.style = "A5"
28
+ book.price = 200
29
+ book.page = 200
30
+ # book.publish_at = nil # (default)
31
+ # book.isbn = "978-3-16-148410-0"
32
+ # book.lang = nil # (default)
33
+ book.save
34
+
35
+
36
+ Book.each do |book|
37
+ pp book
38
+ end
39
+
@@ -0,0 +1,41 @@
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
11
+ field :publisher
12
+ field :style
13
+ field :price
14
+ field :page
15
+ field :publish_at
16
+ field :isbn
17
+ field :lang
18
+
19
+ index :title
20
+
21
+ verify :strict
22
+ end
23
+
24
+
25
+ book = Book.new
26
+ book.title = "title strict"
27
+ book.author = "foobar"
28
+ book.publisher = nil
29
+ book.style = "A6"
30
+ book.price = 300
31
+ book.page = 300
32
+ # book.publish_at = nil # (default)
33
+ book.isbn = "978-3-16-148410-0"
34
+ # book.lang = nil # (default)
35
+ book.save
36
+
37
+
38
+ Book.each do |book|
39
+ pp book
40
+ end
41
+
@@ -0,0 +1,41 @@
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
11
+ field :publisher
12
+ field :style
13
+ field :price
14
+ field :page
15
+ field :publish_at
16
+ field :isbn
17
+ field :lang
18
+
19
+ index :isbn, unique: true
20
+
21
+ verify :strict
22
+ end
23
+
24
+
25
+ book = Book.new
26
+ book.title = "title strict"
27
+ book.author = "foobar"
28
+ book.publisher = nil
29
+ book.style = "A6"
30
+ book.price = 300
31
+ book.page = 300
32
+ # book.publish_at = nil # (default)
33
+ book.isbn = "978-3-16-148410-0"
34
+ # book.lang = nil # (default)
35
+ book.save
36
+
37
+
38
+ Book.each do |book|
39
+ pp book
40
+ end
41
+