Skip to content

Commit 7228fb1

Browse files
committed
Added support for Mongoid 4.
1 parent 5f143cd commit 7228fb1

File tree

14 files changed

+76
-28
lines changed

14 files changed

+76
-28
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ rvm:
66
- 2.0.0
77
- 2.1.0
88

9+
env:
10+
- MONGOID_VERSION=3
11+
- MONGOID_VERSION=4
12+
913
language: ruby
1014

1115
cache: bundler

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Next Release
22
============
33

4+
* [#3](https://github.com/dblock/mongoid-shell/pull/3): Added Mongoid 4.x support - [@pawelniewie](https://github.com/pawelniewie), [@dblock](https://github.com/dblock).
45
* [#2](https://github.com/dblock/mongoid-shell/pull/2): Added support for `--noIndexRestore` to `Mongoid::Shell::Commands::Mongorestore` - [@macreery](https://github.com/macreery).
56
* [#1](https://github.com/dblock/mongoid-shell/pull/1): Enforced compatibility with Mongoid 3.x only - [@macreery](https://github.com/macreery).
67
* Fix: the `mongorestore` command requires a primary node in a replica set - [@dblock](https://github.com/dblock).

Gemfile

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
source "http://rubygems.org"
2-
3-
gem 'mongoid', '~> 3.1'
1+
source 'http://rubygems.org'
42

53
gemspec
64

5+
case version = ENV['MONGOID_VERSION'] || '~> 3.1'
6+
when /4/
7+
gem 'mongoid', github: 'mongoid/mongoid'
8+
when /3/
9+
gem 'mongoid', '~> 3.1'
10+
else
11+
gem 'mongoid', version
12+
end
13+
714
group :development, :test do
8-
gem "rake"
9-
gem "bundler"
10-
gem "rspec"
11-
gem "rubocop", "~> 0.16.0"
15+
gem 'rake'
16+
gem 'bundler'
17+
gem 'rspec'
18+
gem 'rubocop', '~> 0.16.0'
1219
end

lib/mongoid-shell.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml")
44

55
require 'mongoid/shell/version'
6+
require 'mongoid/shell/mongoid'
67
require 'mongoid/shell/errors'
78
require 'mongoid/shell/properties'
89
require 'mongoid/shell/commands'

lib/mongoid/shell/commands/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def vargs(args = {})
2727
args.map do |key, property|
2828
value = send(property)
2929
next unless value
30-
if value.is_a? Boolean
30+
if value.is_a?(Boolean) || value.is_a?(TrueClass)
3131
key
3232
else
3333
value = value.to_s

lib/mongoid/shell/mongoid.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Mongoid
2+
module Shell
3+
def self.mongoid3?
4+
::Mongoid.const_defined? :Observer # deprecated in Mongoid 4.x
5+
end
6+
end
7+
end

lib/mongoid/shell/properties/host.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ def host
99
@host || begin
1010
node = session.cluster.nodes.first
1111
raise Mongoid::Shell::Errors::SessionNotConnectedError unless node
12-
node.address == "localhost:27017" ? nil : node.address
12+
if Mongoid::Shell.mongoid3?
13+
node.address == "localhost:27017" ? nil : node.address
14+
else
15+
node.address.original == "localhost:27017" ? nil : node.address.original
16+
end
1317
end
1418
end
1519
end

lib/mongoid/shell/properties/password.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ module Password
77
# current password
88
def password
99
@password || begin
10-
return nil unless session.context.cluster.auth && session.context.cluster.auth.first
11-
session.context.cluster.auth.first[1][1]
10+
if Mongoid::Shell.mongoid3?
11+
return nil unless session.context.cluster.auth && session.context.cluster.auth.first
12+
session.context.cluster.auth.first[1][1]
13+
else
14+
node = session.cluster.nodes.first
15+
raise Mongoid::Shell::Errors::SessionNotConnectedError unless node
16+
return nil unless node.credentials.has_key? db || node.credentials[db].empty?
17+
node.credentials[db][1]
18+
end
1219
end
1320
end
21+
22+
private
23+
24+
def db
25+
@db || session.send(:current_database).name
26+
end
1427
end
1528
end
1629
end

lib/mongoid/shell/properties/primary.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def primary
1010
raise Mongoid::Shell::Errors::SessionNotConnectedError unless session.cluster.nodes.any?
1111
node = session.cluster.nodes.find(&:primary?)
1212
raise Mongoid::Shell::Errors::MissingPrimaryNodeError unless node
13-
node.address == "localhost:27017" ? nil : node.address
13+
if Mongoid::Shell.mongoid3?
14+
node.address == "localhost:27017" ? nil : node.address
15+
else
16+
node.address.original == "localhost:27017" ? nil : node.address.original
17+
end
1418
end
1519
end
1620
end

lib/mongoid/shell/properties/username.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ module Username
77
# current username
88
def username
99
@username || begin
10-
return nil unless session.context.cluster.auth && session.context.cluster.auth.first
11-
session.context.cluster.auth.first[1][0]
10+
if Mongoid::Shell.mongoid3?
11+
return nil unless session.context.cluster.auth && session.context.cluster.auth.first
12+
session.context.cluster.auth.first[1][0]
13+
else
14+
node = session.cluster.nodes.first
15+
raise Mongoid::Shell::Errors::SessionNotConnectedError unless node
16+
return nil unless node.credentials.has_key? db || node.credentials[db].empty?
17+
node.credentials[db][0]
18+
end
1219
end
1320
end
21+
22+
private
23+
24+
def db
25+
@db || session.send(:current_database).name
26+
end
1427
end
1528
end
1629
end

mongoid-shell.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
88
s.email = "[email protected]"
99
s.platform = Gem::Platform::RUBY
1010
s.required_rubygems_version = '>= 1.3.6'
11-
s.files = `git ls-files`.split("\n")
11+
s.files = Dir['LICENSE.md', 'README.md', 'CHANGELOG.md', 'lib/**/*']
1212
s.require_paths = [ "lib" ]
1313
s.homepage = "http://github.com/dblock/mongoid-shell"
1414
s.licenses = [ "MIT" ]

spec/mongoid/commands/mongorestore_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
Mongoid::Shell::Commands::Mongorestore.new(
7070
session: @session,
7171
restore: "a folder"
72-
).to_s.should == "mongorestore --host flame.mongohq.com:27017 --db mongoid --username user --password password \"a folder\""
72+
).to_s.should == "mongorestore --host something.mongohq.com:27017 --db mongoid --username user --password password \"a folder\""
7373
end
7474
end
7575
context "url" do

spec/support/config/mongoid.yml

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# Tell Mongoid which environment this configuration is for.
21
production:
3-
# This starts the session configuration settings. You may have as
4-
# many sessions as you like, but you must have at least 1 named
5-
# 'default'.
62
sessions:
7-
# Define the default session.
83
default:
94
database: mongoid_shell_tests
105
hosts:
116
- localhost:27017
127
single_host:
138
# A single remote server.
149
hosts:
15-
- flame.mongohq.com:27017
10+
- something.mongohq.com:27017
1611
database: mongoid
1712
username: user
1813
password: password
@@ -23,11 +18,6 @@ production:
2318
- dedicated2.myapp.com:27017
2419
- dedicated3.myapp.com:27017
2520
database: mongoid
26-
# We can set session specific options, like reads executing
27-
# on secondary nodes, and defaulting the session to safe mode.
28-
options:
29-
consistency: :eventual
30-
safe: true
3121
username: user
3222
password: password
3323
# This configuration shows an authenticated replica set via a uri.

spec/support/helpers/moped_session_helper.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
module MopedSessionHelper
22
# returns a Moped session with stubbed address resolution
33
def moped_session(name)
4-
Moped::Node.any_instance.stub(:resolve_address).and_return(false)
4+
if Mongoid::Shell.mongoid3?
5+
Moped::Node.any_instance.stub(:resolve_address).and_return(false)
6+
else
7+
Moped::Address.any_instance.stub(:resolve).and_return(false)
8+
end
59
config = File.join(File.dirname(__FILE__), "../../support/config/mongoid.yml")
610
Mongoid.load! config, :production
711
session = Mongoid::Sessions.with_name(name)

0 commit comments

Comments
 (0)