minidoc 2.1.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 003a6c6c6a986d6e5193a35c320500665de4ca191e7e95025cbfaabfb3cfe38b
4
- data.tar.gz: f523648aa8f17fd5c338e7bf5306aeba83176c9ff93eba27b5c5f71e4b2a5d02
3
+ metadata.gz: db222ae3336710cc1304f5c0528c8e92bd57579dd2cf6ae3f1aee9d8b686b131
4
+ data.tar.gz: 363c7e70ee2e925907cb0fb5ea4d875735cc7b11ef215b2c1f9d41ba829e3d48
5
5
  SHA512:
6
- metadata.gz: f7dbc5a167eb7154b9c6ac518c3b14bc06a932f9f392ad6f9f7ab9352f370ef9ebc5b0725ee033552dfe3235d11f15d8cc775ec9ac34f1056d0d8964df850518
7
- data.tar.gz: 7a5d5e8021410d2c97849e7a8d68df67cd1fad709d8f1b9d6efa4068edb33c8c0953cde2d0d54a691693fcc9c4e357f5525149843411a9edbb12705a48c4d03d
6
+ metadata.gz: 5a76ee71f56f3325a28e89b5a09f6e8e745a29973b7fa535f4f64dbf0d3659e3d73b8b118b9b88a811269f6fdc78a76c1bd65b554c667ac6205368af1a589ac3
7
+ data.tar.gz: 12e94541e1bdefd34c7cc3009a4bb2435a3b5e860fa80c546a9e28de1393bb5e34efbbcca327382b18bb057cffdf622319a5a1a2a613399dde938941fa12b5ab
@@ -12,12 +12,13 @@ module Minidoc::Connection
12
12
 
13
13
  module ClassMethods
14
14
  def collection
15
- database[collection_name]
15
+ validate_config
16
+ connection.use(database_name)
17
+ connection[collection_name]
16
18
  end
17
19
 
18
20
  def database
19
- validate_config
20
- connection[database_name]
21
+ connection.use(database_name).database
21
22
  end
22
23
 
23
24
  def collection_name=(name)
@@ -27,10 +27,10 @@ class Minidoc
27
27
  end
28
28
 
29
29
  def increment(step_size = 1)
30
- result = record.class.collection.find_and_modify(
31
- query: { _id: record.id },
32
- update: { "$inc" => { field => step_size } },
33
- new: true,
30
+ result = record.class.collection.find_one_and_update(
31
+ { _id: record.id },
32
+ { "$inc" => { field => step_size } },
33
+ return_document: :after,
34
34
  )
35
35
 
36
36
  result[field.to_s]
@@ -1,12 +1,11 @@
1
- class Minidoc::DuplicateKey < Mongo::OperationFailure
2
- DUPLICATE_KEY_ERROR_CODE = 11000
1
+ class Minidoc::DuplicateKey < StandardError
2
+ DUPLICATE_KEY_ERROR_CODE = "E11000".freeze
3
3
 
4
- def self.duplicate_key_exception(exception)
5
- if exception.respond_to?(:error_code) && exception.error_code == DUPLICATE_KEY_ERROR_CODE
6
- new(exception.message, exception.error_code, exception.result)
4
+ def self.duplicate_key_exception(ex)
5
+ if ex.is_a?(Mongo::Error::OperationFailure) && ex.message.starts_with?(DUPLICATE_KEY_ERROR_CODE)
6
+ new(ex.message)
7
7
  else
8
8
  nil
9
9
  end
10
10
  end
11
-
12
11
  end
@@ -1,4 +1,5 @@
1
1
  require "active_support/concern"
2
+ require "forwardable"
2
3
 
3
4
  module Minidoc::Finders
4
5
  extend ActiveSupport::Concern
@@ -15,7 +16,7 @@ module Minidoc::Finders
15
16
  end
16
17
 
17
18
  def count(selector = {})
18
- collection.count(query: selector)
19
+ collection.count_documents(selector)
19
20
  end
20
21
 
21
22
  def exists?(selector = {})
@@ -24,17 +25,21 @@ module Minidoc::Finders
24
25
 
25
26
  def find(id_or_selector, options = {})
26
27
  if id_or_selector.is_a?(Hash)
27
- options.merge!(transformer: method(:wrap))
28
- collection.find(id_or_selector, options)
28
+ ResultSet.new(collection.find(id_or_selector, options), wrapper)
29
29
  else
30
30
  raise ArgumentError unless options.empty?
31
31
  id = BSON::ObjectId(id_or_selector.to_s)
32
- wrap(collection.find_one(_id: id))
32
+ wrapper.call(collection.find(_id: id).first)
33
33
  end
34
34
  end
35
35
 
36
36
  def find_one(selector = {}, options = {})
37
- wrap(collection.find_one(selector, options))
37
+ # running collection.find({}).first will leave the cursor open unless we iterate across all of the documents
38
+ # so in order to do not let a cusror open we want to kill the cursor after having grabbed the first document
39
+ view = collection.find(selector, options)
40
+ wrapped_doc = wrapper.call(view.first)
41
+ view.close_query
42
+ wrapped_doc
38
43
  end
39
44
 
40
45
  def find_one!(selector = {}, options = {})
@@ -54,14 +59,39 @@ module Minidoc::Finders
54
59
  doc
55
60
  end
56
61
 
57
- def wrap(doc)
58
- return nil unless doc
62
+ def wrapper
63
+ @wrapper ||= proc do |doc|
64
+ if doc
65
+ if doc.is_a?(Array) || doc.is_a?(Mongo::Cursor)
66
+ doc.map { |d| from_db(d) }
67
+ else
68
+ from_db(doc)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ class ResultSet
76
+ include Enumerable
59
77
 
60
- if doc.is_a?(Array) || doc.is_a?(Mongo::Cursor)
61
- doc.map { |d| from_db(d) }
78
+ def initialize(view, doc_wrapper)
79
+ @view = view
80
+ @doc_wrapper = doc_wrapper
81
+ end
82
+
83
+ def each(&_block)
84
+ if block_given?
85
+ @view.each do |doc|
86
+ yield @doc_wrapper.call(doc)
87
+ end
62
88
  else
63
- from_db(doc)
89
+ to_enum
64
90
  end
65
91
  end
92
+
93
+ def count
94
+ @view.count_documents
95
+ end
66
96
  end
67
97
  end
data/lib/minidoc/grid.rb CHANGED
@@ -1,7 +1,18 @@
1
- class Minidoc::Grid < Mongo::Grid
1
+ class Minidoc::Grid
2
+ def initialize(database)
3
+ @bucket = Mongo::Grid::FSBucket.new(database)
4
+ end
5
+
6
+ def put(str, filename = SecureRandom.uuid)
7
+ bucket.upload_from_stream(filename, StringIO.new(str))
8
+ end
9
+
2
10
  def get(id)
3
11
  id = BSON::ObjectId(id.to_s)
4
- super(id)
12
+ io = StringIO.new
13
+ bucket.download_to_stream(id, io)
14
+ io.rewind
15
+ io
5
16
  end
6
17
 
7
18
  def get_json(id)
@@ -11,6 +22,10 @@ class Minidoc::Grid < Mongo::Grid
11
22
 
12
23
  def delete(id)
13
24
  id = BSON::ObjectId(id.to_s)
14
- super(id)
25
+ bucket.delete(id)
15
26
  end
27
+
28
+ private
29
+
30
+ attr_reader :bucket
16
31
  end
@@ -12,7 +12,7 @@ class Minidoc
12
12
  end
13
13
 
14
14
  def clear_collections(connection = Minidoc.connection)
15
- each_collection(connection) { |c| c.remove({}) }
15
+ each_collection(connection, &:drop)
16
16
  end
17
17
 
18
18
  def clear_indexes(connection = Minidoc.connection)
@@ -20,7 +20,7 @@ class Minidoc
20
20
  end
21
21
 
22
22
  def each_collection(connection, &block)
23
- connection.db.collections.
23
+ connection.collections.
24
24
  reject { |c| c.name.include?("system") }.
25
25
  each(&block)
26
26
  end
@@ -1,3 +1,3 @@
1
1
  class Minidoc
2
- VERSION = "2.1.0".freeze
2
+ VERSION = "3.0.0".freeze
3
3
  end
data/lib/minidoc.rb CHANGED
@@ -41,7 +41,7 @@ class Minidoc
41
41
  end
42
42
 
43
43
  def self.delete(id)
44
- collection.remove(_id: BSON::ObjectId(id.to_s))
44
+ collection.delete_one(_id: BSON::ObjectId(id.to_s))
45
45
  end
46
46
 
47
47
  def self.set(id, attributes)
@@ -61,12 +61,12 @@ class Minidoc
61
61
  end
62
62
 
63
63
  def self.update_one(id, updates)
64
- collection.update({ "_id" => id }, updates)
64
+ collection.update_one({ "_id" => id }, updates)
65
65
  end
66
66
 
67
67
  def self.atomic_set(query, attributes)
68
- result = collection.update(query, "$set" => attributes)
69
- result["ok"] == 1 && result["n"] == 1
68
+ result = collection.update_one(query, "$set" => attributes)
69
+ result.ok? && result.n == 1
70
70
  end
71
71
 
72
72
  def self.value_class
@@ -85,7 +85,7 @@ class Minidoc
85
85
  yield
86
86
  rescue Minidoc::DuplicateKey
87
87
  false
88
- rescue Mongo::OperationFailure => ex
88
+ rescue Mongo::Error::OperationFailure => ex
89
89
  if Minidoc::DuplicateKey.duplicate_key_exception(ex)
90
90
  false
91
91
  else
@@ -193,7 +193,7 @@ private
193
193
  new_record? ? create : update
194
194
  @new_record = false
195
195
  true
196
- rescue Mongo::OperationFailure => exception
196
+ rescue Mongo::Error::OperationFailure => exception
197
197
  if (duplicate_key_exception = Minidoc::DuplicateKey.duplicate_key_exception(exception))
198
198
  raise duplicate_key_exception
199
199
  else
@@ -202,11 +202,11 @@ private
202
202
  end
203
203
 
204
204
  def create
205
- self.class.collection << attributes
205
+ self.class.collection.insert_one(attributes)
206
206
  end
207
207
 
208
208
  def update
209
- self.class.collection.update({ _id: id }, { "$set" => attributes.except(:_id) })
209
+ self.class.collection.update_one({ _id: id }, { "$set" => attributes.except(:_id) })
210
210
  end
211
211
 
212
212
  if ActiveSupport.respond_to?(:run_load_hooks)
@@ -29,7 +29,7 @@ describe "connection" do
29
29
 
30
30
  describe ".database" do
31
31
  it "exposes the underlying Mongo object" do
32
- expect(User.database).to be_a Mongo::DB
32
+ expect(User.database).to be_a Mongo::Database
33
33
  end
34
34
 
35
35
  it "passes through the database name to the underlying Mongo::DB" do
@@ -3,8 +3,8 @@ require "spec_helper"
3
3
  describe Minidoc::Finders do
4
4
  describe ".all" do
5
5
  it "returns all of the documents in the collection" do
6
- (User.collection << { name: "Joe" })
7
- (User.collection << { name: "Bryan" })
6
+ User.collection.insert_one(name: "Joe")
7
+ User.collection.insert_one(name: "Bryan")
8
8
  users = User.all
9
9
  expect(["Bryan", "Joe"]).to match_array(users.map(&:name))
10
10
  end
@@ -12,21 +12,21 @@ describe Minidoc::Finders do
12
12
 
13
13
  describe ".count" do
14
14
  it "counts the documents in the collection" do
15
- (User.collection << { name: "Joe" })
16
- (User.collection << { name: "Bryan" })
15
+ User.collection.insert_one(name: "Joe")
16
+ User.collection.insert_one(name: "Bryan")
17
17
  expect(User.count).to eq 2
18
18
  end
19
19
 
20
20
  it "can be scoped by a query" do
21
- (User.collection << { name: "Joe" })
22
- (User.collection << { name: "Bryan" })
21
+ User.collection.insert_one(name: "Joe")
22
+ User.collection.insert_one(name: "Bryan")
23
23
  expect(User.count(name: "Bryan")).to eq 1
24
24
  end
25
25
  end
26
26
 
27
27
  describe ".exists?" do
28
28
  it "tells you if any documents exist that match a query" do
29
- (User.collection << { name: "Joe" })
29
+ User.collection.insert_one(name: "Joe")
30
30
  expect(User.exists?(name: "Joe")).to be(true)
31
31
  expect(User.exists?(name: "Bryan")).to be(false)
32
32
  end
@@ -10,7 +10,6 @@ describe Minidoc::Grid do
10
10
 
11
11
  document = grid.get doc_id.to_s
12
12
 
13
- expect(document).to be_a Mongo::GridIO
14
13
  expect(document.read).to eq "estamos en espana"
15
14
  end
16
15
  end
@@ -30,9 +29,9 @@ describe Minidoc::Grid do
30
29
  it "removes the 'file' from the database" do
31
30
  doc_id = grid.put "estamos en espana"
32
31
 
32
+ expect(grid.get(doc_id).read).to eq("estamos en espana")
33
33
  grid.delete doc_id.to_s
34
-
35
- expect { grid.get doc_id }.to raise_error(Mongo::GridFileNotFound)
34
+ expect { grid.get(doc_id) }.to raise_error(Mongo::Error::FileNotFound)
36
35
  end
37
36
  end
38
37
  end
@@ -3,12 +3,15 @@ require "spec_helper"
3
3
  describe Minidoc::ReadOnly do
4
4
  class ReadOnlyUser < Minidoc::ReadOnly
5
5
  self.collection_name = "users"
6
+ self.database_name = "minidoc_test"
7
+ self.connection = $mongo
8
+
6
9
  attribute :name, String
7
10
  end
8
11
 
9
12
  class AltModel < Minidoc::ReadOnly
10
13
  self.connection = $alt_mongo
11
- self.database_name = $alt_mongo.db.name
14
+ self.database_name = $alt_mongo.database.name
12
15
 
13
16
  attribute :name, String
14
17
  end
@@ -27,7 +30,7 @@ describe Minidoc::ReadOnly do
27
30
 
28
31
  it "can have a separate mongo connection" do
29
32
  expect(AltModel.count).to eq(0)
30
- $alt_mongo.db[:alt_models].insert(name: "42")
33
+ $alt_mongo.database[:alt_models].insert_one(name: "42")
31
34
  expect(AltModel.count).to eq(1)
32
35
 
33
36
  instance = AltModel.find_one(name: "42")
data/spec/minidoc_spec.rb CHANGED
@@ -95,7 +95,7 @@ describe Minidoc do
95
95
  it "raises Minidoc::DuplicateKey where appropriate" do
96
96
  collection = double
97
97
  expect(User).to receive(:collection).and_return(collection)
98
- expect(collection).to receive(:<<).and_raise(Mongo::OperationFailure.new("Duplicate key exception", Minidoc::DuplicateKey::DUPLICATE_KEY_ERROR_CODE))
98
+ expect(collection).to receive(:insert_one).and_raise(Mongo::Error::OperationFailure.new("E11000 Boooo"))
99
99
  user = User.new
100
100
 
101
101
  expect { user.save }.to raise_error(Minidoc::DuplicateKey)
@@ -104,7 +104,7 @@ describe Minidoc do
104
104
  it "doesn't suppress errors it shouldn't" do
105
105
  collection = double
106
106
  expect(User).to receive(:collection).and_return(collection)
107
- expect(collection).to receive(:<<).and_raise(ArgumentError)
107
+ expect(collection).to receive(:insert_one).and_raise(ArgumentError)
108
108
  user = User.new
109
109
 
110
110
  expect { user.save }.to raise_error(ArgumentError)
@@ -260,7 +260,7 @@ describe Minidoc do
260
260
  user = User.create!(name: "Bryan")
261
261
  expect(user.reload.name).to eq "Bryan"
262
262
 
263
- User.collection.update({ :_id => user.id }, name: "Noah")
263
+ User.collection.update_one({ :_id => user.id }, name: "Noah")
264
264
  expect(user.name).to eq "Bryan"
265
265
 
266
266
  user.reload
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,10 @@
1
1
  require "simplecov"
2
+
3
+ if ENV["CI"]
4
+ require "simplecov_json_formatter"
5
+ SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
6
+ end
7
+
2
8
  SimpleCov.start do
3
9
  add_filter "/test/"
4
10
  end
@@ -22,12 +28,16 @@ class SecondUser < Minidoc
22
28
  attribute :age, Integer
23
29
  end
24
30
 
25
- $mongo = Mongo::MongoClient.from_uri(ENV["MONGODB_URI"] || "mongodb://localhost/minidoc_test")
31
+ uri = Mongo::URI.new(ENV["MONGODB_URI"] || "mongodb://localhost:27017")
32
+
33
+ $mongo = Mongo::Client.new(uri.servers, uri.client_options)
34
+ $mongo.logger.level = Logger::FATAL
35
+
26
36
  Minidoc.connection = $mongo
27
- Minidoc.database_name = $mongo.db.name
37
+ Minidoc.database_name = $mongo.database.name
28
38
 
29
- alt_url = "mongodb://#{$mongo.host}/#{$mongo.db.name}_2"
30
- $alt_mongo = Mongo::MongoClient.from_uri(alt_url)
39
+ options = uri.options.merge(database: "#{uri.database}_2")
40
+ $alt_mongo = Mongo::Client.new(uri.servers, options)
31
41
 
32
42
  RSpec.configure do |config|
33
43
  if config.files_to_run.one?
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minidoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Helmkamp
8
8
  - Code Climate
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-05-10 00:00:00.000000000 Z
12
+ date: 2023-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -52,40 +52,46 @@ dependencies:
52
52
  - !ruby/object:Gem::Version
53
53
  version: '6'
54
54
  - !ruby/object:Gem::Dependency
55
- name: virtus
55
+ name: mongo
56
56
  requirement: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: 1.0.0
61
58
  - - "~>"
62
59
  - !ruby/object:Gem::Version
63
- version: '1.0'
60
+ version: '2.14'
61
+ - - "<="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.14.1
64
64
  type: :runtime
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: 1.0.0
71
68
  - - "~>"
72
69
  - !ruby/object:Gem::Version
73
- version: '1.0'
70
+ version: '2.14'
71
+ - - "<="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.14.1
74
74
  - !ruby/object:Gem::Dependency
75
- name: mongo
75
+ name: virtus
76
76
  requirement: !ruby/object:Gem::Requirement
77
77
  requirements:
78
78
  - - "~>"
79
79
  - !ruby/object:Gem::Version
80
- version: '1'
80
+ version: '1.0'
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.0.0
81
84
  type: :runtime
82
85
  prerelease: false
83
86
  version_requirements: !ruby/object:Gem::Requirement
84
87
  requirements:
85
88
  - - "~>"
86
89
  - !ruby/object:Gem::Version
87
- version: '1'
88
- description:
90
+ version: '1.0'
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 1.0.0
94
+ description:
89
95
  email:
90
96
  - bryan@brynary.com
91
97
  - hello@codeclimate.com
@@ -122,7 +128,7 @@ homepage: https://github.com/codeclimate/minidoc
122
128
  licenses:
123
129
  - MIT
124
130
  metadata: {}
125
- post_install_message:
131
+ post_install_message:
126
132
  rdoc_options: []
127
133
  require_paths:
128
134
  - lib
@@ -137,8 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
143
  - !ruby/object:Gem::Version
138
144
  version: '0'
139
145
  requirements: []
140
- rubygems_version: 3.0.3
141
- signing_key:
146
+ rubygems_version: 3.2.3
147
+ signing_key:
142
148
  specification_version: 4
143
149
  summary: Lightweight wrapper for MongoDB documents
144
150
  test_files: