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,96 @@
1
+ require "bundler/gem_helper"
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
9
+ class Bundler::GemHelper
10
+
11
+ def git_archive( dir = "../zip" )
12
+ FileUtils.mkdir_p dir
13
+ dest_path = File.join(dir, "#{name}-#{version}.zip")
14
+ cmnd = "git archive --format zip --prefix=#{name}/ HEAD > #{dest_path}"
15
+
16
+ out, code = sh_with_status( cmnd )
17
+ raise "Couldn't archive gem," unless code == 0
18
+
19
+ Bundler.ui.confirm "#{name} #{version} archived to #{dest_path}."
20
+ end
21
+
22
+ def git_push
23
+ ver = version.to_s
24
+
25
+ cmnd = "git push origin #{ver} "
26
+ out, code = sh_with_status( cmnd )
27
+ raise "Couldn't git push origin." unless code == 0
28
+
29
+ cmnd = "git push "
30
+ out, code = sh_with_status( cmnd )
31
+ raise "Couldn't git push." unless code == 0
32
+
33
+ Bundler.ui.confirm "Git Push #{ver}."
34
+ end
35
+
36
+ def update_version( new_version )
37
+ version_filename = %x[ find . -type f -name "version.rb" | grep -v vendor | head -1 ].chomp
38
+ version_pathname = File.expand_path( version_filename )
39
+ lines = File.open( version_pathname ).read
40
+ lines = lines.gsub( /VERSION\s*=\s*\"\d+\.\d+\.\d+\"/, "VERSION = \"#{new_version}\"" )
41
+ File.open( version_pathname, "w" ) do |file|
42
+ file.write( lines )
43
+ end
44
+
45
+ cmnd = "git add #{version_pathname} "
46
+ out, code = sh_with_status( cmnd )
47
+ raise "Couldn't git add," unless code == 0
48
+
49
+ cmnd = "git commit -m '#{new_version}' "
50
+ out, code = sh_with_status( cmnd )
51
+ raise "Couldn't git commit." unless code == 0
52
+
53
+ cmnd = "git tag #{new_version} "
54
+ out, code = sh_with_status( cmnd )
55
+ raise "Couldn't git tag." unless code == 0
56
+
57
+ Bundler.ui.confirm "Update Tags to #{new_version}."
58
+ end
59
+
60
+ end
61
+
62
+ Bundler::GemHelper.new(Dir.pwd).instance_eval do
63
+
64
+ desc "Archive #{name}-#{version}.zip from repository"
65
+ task 'zip' do
66
+ git_archive
67
+ end
68
+
69
+ desc "Git Push"
70
+ task 'push' do
71
+ git_push
72
+ end
73
+
74
+ desc "Update Version Tiny"
75
+ task 'tiny' do
76
+ major, minor, tiny = version.to_s.split('.')
77
+ new_version = [major, minor, tiny.to_i + 1].join('.')
78
+ update_version( new_version )
79
+ end
80
+
81
+ desc "Update Version Minor"
82
+ task 'minor' do
83
+ major, minor, tiny = version.to_s.split('.')
84
+ new_version = [major, minor.to_i + 1, 0].join('.')
85
+ update_version( new_version )
86
+ end
87
+
88
+ desc "Update Version Major"
89
+ task 'major' do
90
+ major, minor, tiny = version.to_s.split('.')
91
+ new_version = [major.to_i + 1, 0, 0].join('.')
92
+ update_version( new_version )
93
+ end
94
+
95
+ end
96
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mongous"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require "mongous/version"
2
+ require "mongous/base"
3
+ require "mongous/extention"
4
+ require "mongous/filter"
5
+ require "mongous/document"
6
+
7
+ module Mongous
8
+ class << self
9
+ include Mongous::Base
10
+ end
11
+ end
@@ -0,0 +1,68 @@
1
+ require "time"
2
+ require "erb"
3
+ require "yaml"
4
+ require "json"
5
+ require "mongo"
6
+
7
+ module Mongous
8
+ class Error < StandardError; end
9
+
10
+ module Base
11
+ def connect( hosts_or_uri = nil, file: nil, mode: nil, loglevel: nil, **options )
12
+ case hosts_or_uri
13
+ when Array, String
14
+ ;
15
+
16
+ when NilClass
17
+ if file
18
+ mode ||= "development"
19
+ text = ::File.open( file ).read rescue raise( Mongous::Error, "could not load #{ file }" )
20
+ yaml = ::ERB.new( text ).result rescue raise( Mongous::Error, "could not parse by ERB: #{ file }" )
21
+ hash = ::YAML.load( yaml )
22
+ conf = hash[mode]["clients"]["default"]
23
+
24
+ hosts_or_uri = conf["uri"]
25
+ if hosts_or_uri
26
+ conf.delete( "uri" )
27
+ options.merge!( conf )
28
+ else
29
+ hosts_or_uri = conf["hosts"]
30
+ conf.delete( "hosts" )
31
+ database = conf["database"]
32
+ conf.delete( "database" )
33
+ options.merge!( conf )
34
+ options["database"] = database
35
+ end
36
+
37
+ else
38
+ hosts_or_uri = [ "localhost:27017" ]
39
+ options[:database] = "test" if options[:database].nil? || options[:database].empty?
40
+
41
+ end
42
+ end
43
+
44
+ ::Mongo::Logger.logger.level = loglevel || Logger::ERROR
45
+ ::Mongo::Client.new( hosts_or_uri, options )
46
+ end
47
+
48
+ def connect!( hosts_or_uri = nil, **options )
49
+ _client = connect( hosts_or_uri, **options )
50
+ self.class_variable_set( :@@client, _client )
51
+ end
52
+
53
+ def client
54
+ self.class_variable_get( :@@client ) rescue nil
55
+ end
56
+
57
+ def client=( _client )
58
+ if !_client.is_a?( ::Mongo::Client )
59
+ raise Mongous::Error, "type invalid. : #{ _client }"
60
+ end
61
+ self.class_variable_set( :@@client, _client )
62
+ end
63
+
64
+ def loger
65
+ ::Mongo::Logger.logger
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,159 @@
1
+
2
+ module Mongous
3
+ module Document
4
+ class << self
5
+ def included( klass )
6
+ klass.extend Extention
7
+ end
8
+ end
9
+
10
+ def initialize( **doc )
11
+ @doc = {}
12
+ doc.each do |label, value|
13
+ label = label.to_s
14
+ @doc[label] = value
15
+ end
16
+ end
17
+
18
+ def method_missing( sym, *args, **opts, &block )
19
+ m = /\A(\w+)([=])?\Z/.match( sym.to_s )
20
+ if args.size == 0 && m[2].nil?
21
+ self[ m[1] ]
22
+ elsif args.size == 1 && !m[2].nil?
23
+ self[ m[1] ] = args.shift
24
+ else
25
+ raise Mongous::Error, "invalid parameter. #{sym} #{args.join(', ')}"
26
+ end
27
+ end
28
+
29
+ def having?( value )
30
+ case value
31
+ when NilClass
32
+ false
33
+ when String
34
+ !value.strip.empty?
35
+ else
36
+ true
37
+ end
38
+ end
39
+
40
+ def save
41
+ self.class.fields.each do |label, field|
42
+ _must = field[:_args].include?(:must)
43
+ if @doc.has_key?(label)
44
+ if _must && !having?( @doc[label] )
45
+ raise Mongous::Error, "must and not having field. : #{ label }"
46
+ end
47
+ else
48
+ if block = field[:_block]
49
+ self[label] = block.call
50
+ elsif _must
51
+ raise Mongous::Error, "must and unassigned field. : #{ label }"
52
+ elsif self.class.symbols[:strict]
53
+ self[label] = nil
54
+ end
55
+ end
56
+ end
57
+
58
+ self.class.blocks.each do |call_from, block|
59
+ if !self.instance_eval( &block )
60
+ raise Mongous::Error, "violation detected on save. : #{ call_from }"
61
+ end
62
+ end
63
+
64
+ if @doc["_id"]
65
+ _filter = { "_id"=> @doc["_id"] }
66
+ if self.class.collection.find( _filter ).first
67
+ self.class.collection.update_one( _filter, { '$set' => @doc } )
68
+ return self
69
+ end
70
+ end
71
+
72
+ self.class.collection.insert_one( @doc )
73
+ self
74
+ end
75
+
76
+ def []( label )
77
+ label = label.to_s
78
+
79
+ if self.class.symbols[:strict]
80
+ if !self.class.fields.keys.include?(label)
81
+ raise Mongous::Error, "undefined field. : #{ label }"
82
+ end
83
+ end
84
+
85
+ return @doc[label] if @doc.has_key?(label)
86
+
87
+ field = self.class.fields[label]
88
+ return nil if field.nil?
89
+
90
+ case default = field[:default]
91
+ when Proc
92
+ self.instance_eval( &default )
93
+ else
94
+ default
95
+ end
96
+ end
97
+
98
+ def []=( label, value )
99
+ label = label.to_s
100
+ if self.class.symbols[:strict]
101
+ labels = ["_id"] + self.class.fields.keys
102
+ if !labels.include?( label )
103
+ raise Mongous::Error, "undefined field. : #{ label }"
104
+ end
105
+ end
106
+
107
+ field = self.class.fields[label]
108
+ return @doc[label] = value if field.nil?
109
+
110
+ types = []
111
+ if args = field[:_args] || []
112
+ args.each do |arg|
113
+ if arg.class == Class
114
+ types << arg
115
+ args -= [arg]
116
+ break
117
+ end
118
+ end
119
+ end
120
+
121
+ if !types.empty?
122
+ if !(args & [:must, :not_null]).empty?
123
+ if !types.include?( value.class )
124
+ raise Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
125
+ end
126
+ else
127
+ if !(types + [NilClass] ).include?( value.class )
128
+ raise Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
129
+ end
130
+ end
131
+ else
132
+ if !(args & [:must, :not_null]).empty?
133
+ if [NilClass].include?( value.class )
134
+ raise Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
135
+ end
136
+ end
137
+ end
138
+
139
+ @doc[label] = value
140
+
141
+ args.each do |arg|
142
+ case arg
143
+ when Proc
144
+ if !self.instance_eval( &arg )
145
+ raise Mongous::Error, "violation detected. : #{ label } : #{ value }"
146
+ end
147
+ when Array
148
+ if !arg.include?( value )
149
+ raise Mongous::Error, "not include. : #{ label } :#{ value }"
150
+ end
151
+ when Range
152
+ if !arg.cover?( value )
153
+ raise Mongous::Error, "out of range. : #{ label } :#{ value }"
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,167 @@
1
+
2
+ module Mongous
3
+ module Extention
4
+ def client
5
+ if self.class_variable_defined?( :@@client )
6
+ self.class_variable_get( :@@client )
7
+ else
8
+ _client = Mongous.client
9
+ self.class_variable_set( :@@client, _client )
10
+ end
11
+ end
12
+
13
+ def set_client( _client )
14
+ m = /(.*?):(\d+)/.match( caller()[0] )
15
+ call_from = [ m[1], m[2] ].join(":")
16
+ if !_client.is_a?( Mongo::Client )
17
+ raise Mongous::Error, "type invalid. : #{ _client }"
18
+ end
19
+ self.class_variable_set( :@@client, _client )
20
+ end
21
+
22
+ def collection_name
23
+ if self.class_variable_defined?( :@@collection_name )
24
+ self.class_variable_get( :@@collection_name )
25
+ else
26
+ _client_name = self.name
27
+ self.class_variable_set( :@@collection_name, _client_name )
28
+ end
29
+ end
30
+
31
+ def set_collection_name( _collection_name )
32
+ self.class_variable_set( :@@collection_name, _collection_name )
33
+ if self.class_variable_defined?( :@@collection )
34
+ self.remove_class_variable( :@@collection )
35
+ end
36
+ end
37
+
38
+ def collection
39
+ if self.class_variable_defined?( :@@collection )
40
+ if _collection = self.class_variable_get( :@@collection )
41
+ return _collection
42
+ end
43
+ end
44
+
45
+ _collection_name = collection_name
46
+ _client = client
47
+
48
+ if _client.database.collection_names.include?( _collection_name )
49
+ _collection = _client[ _collection_name ]
50
+ else
51
+ _collection = _client[ _collection_name ]
52
+ _collection.create
53
+ end
54
+
55
+ indexes.each do |keys, opts|
56
+ _collection.indexes.create_one( keys, opts ) rescue nil
57
+ end
58
+
59
+ self.class_variable_set( :@@collection, _collection )
60
+ end
61
+
62
+ def fields
63
+ if self.class_variable_defined?( :@@fields )
64
+ self.class_variable_get( :@@fields )
65
+ else
66
+ self.class_variable_set( :@@fields, {} )
67
+ end
68
+ end
69
+
70
+ def symbols
71
+ if self.class_variable_defined?( :@@symbols )
72
+ self.class_variable_get( :@@symbols )
73
+ else
74
+ self.class_variable_set( :@@symbols, {} )
75
+ end
76
+ end
77
+
78
+ def blocks
79
+ if self.class_variable_defined?( :@@blocks )
80
+ self.class_variable_get( :@@blocks )
81
+ else
82
+ self.class_variable_set( :@@blocks, {} )
83
+ end
84
+ end
85
+
86
+ def indexes
87
+ if self.class_variable_defined?( :@@indexes )
88
+ self.class_variable_get( :@@indexes )
89
+ else
90
+ self.class_variable_set( :@@indexes, [] )
91
+ end
92
+ end
93
+
94
+ def create( **doc )
95
+ self.new( **doc ).save
96
+ end
97
+
98
+ def filter( **_filter )
99
+ Filter.new( self ).filter( _filter )
100
+ end
101
+
102
+ def first
103
+ Filter.new( self ).first
104
+ end
105
+
106
+ def all
107
+ Filter.new( self ).all
108
+ end
109
+
110
+ def each( &block )
111
+ Filter.new( self ).each( &block )
112
+ end
113
+
114
+ def delete
115
+ Filter.new( self ).delete
116
+ end
117
+
118
+ def field( label, *args, **opts, &block )
119
+ m = /(.*?):(\d+)/.match( caller()[0] )
120
+ call_from = [ m[1], m[2] ].join(":")
121
+
122
+ args.each do |arg|
123
+ if klass = arg.class
124
+ if ![Class, Range, Array, Proc, Symbol].include?(klass)
125
+ raise Mongous::Error, "field error. : #{arg} on #{ label } at #{ call_from }"
126
+ end
127
+ end
128
+ end
129
+
130
+ opts.each do |key, value|
131
+ case key
132
+ when :default
133
+ case value
134
+ when Proc, String, Numeric
135
+ else
136
+ raise Mongous::Error, "field error. : #{key} on #{ label } at #{ call_from }"
137
+ end
138
+ end
139
+ end
140
+
141
+ opts[:_args] = args
142
+ opts[:_block] = block
143
+ fields[label.to_s] = opts
144
+ end
145
+
146
+ def verify( *syms, &block )
147
+ if !syms.empty?
148
+ syms.each do |sym|
149
+ symbols[sym] = true
150
+ end
151
+ elsif block
152
+ m = /(.*?):(\d+)/.match( caller()[0] )
153
+ call_from = [ m[1], m[2] ].join(":")
154
+ blocks[call_from] = block
155
+ end
156
+ end
157
+
158
+ def index( *syms, **opts )
159
+ opts[:background] = true unless opts.has_key?(:background)
160
+ keys = {}
161
+ syms.each do |sym|
162
+ keys[sym] = 1
163
+ end
164
+ indexes.push << [keys, opts]
165
+ end
166
+ end
167
+ end