mongoid-shell 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=documentation
3
+
@@ -0,0 +1,9 @@
1
+ services:
2
+ - mongodb
3
+
4
+ rvm:
5
+ - 1.9.3
6
+
7
+ notifications:
8
+ email:
9
+ - dblock@dblock.org
@@ -0,0 +1,4 @@
1
+ 0.1.0 (1/27/2013)
2
+ ==================
3
+
4
+ * Initial public release with support for `mongodump` - [@dblock](https://github.com/dblock).
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem "rake"
7
+ gem "bundler"
8
+ gem "rspec"
9
+ end
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012 Daniel Doubrovkine, Artsy Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ Mongoid::Shell [![Build Status](https://travis-ci.org/dblock/mongoid-shell.png?branch=master)](https://travis-ci.org/dblock/mongoid-shell)
2
+ ==============
3
+
4
+ Create mongo command-lines from Mongoid configuration.
5
+
6
+ Commands can be created for the current default session or you can pass a session as an argument to a new command.
7
+
8
+ ``` ruby
9
+ Mongoid::Shell::Commands::Mongodump.new # will use Mongoid.default_session
10
+ Mongoid::Shell::Commands::Mongodump.new(session: Moped::Session.new([ "127.0.0.1:27017" ]))
11
+ ```
12
+
13
+ Supported Commands
14
+ ==================
15
+
16
+ ### Mongodump
17
+
18
+ ``` ruby
19
+ mongodump = Mongoid::Shell::Commands::Mongodump.new({ collection: 'test' })
20
+ mongodump.to_s # mongodump --host localhost:27017 --db test --collection test
21
+ ```
22
+
23
+ Supports `--db`, `--host`, `--username`, `--password` and `--collection`.
24
+
25
+ Contributing
26
+ ------------
27
+
28
+ Fork the project. Make your feature addition or bug fix with tests. Send a pull request. Bonus points for topic branches.
29
+
30
+ Copyright and License
31
+ ---------------------
32
+
33
+ MIT License, see [LICENSE](http://github.com/dblock/mongoid-shell/raw/master/LICENSE.md) for details.
34
+
35
+ (c) 2013 [Daniel Doubrovkine](http://github.com/dblock), [Artsy Inc.](http://artsy.net)
36
+
37
+
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler/gem_tasks'
3
+
4
+ require File.expand_path('../lib/mongoid/shell/version', __FILE__)
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+ require 'rake'
15
+
16
+ require 'rspec/core'
17
+ require 'rspec/core/rake_task'
18
+
19
+ RSpec::Core::RakeTask.new(:spec) do |spec|
20
+ spec.pattern = FileList['spec/**/*_spec.rb']
21
+ end
22
+
23
+ task :default => :spec
24
+
@@ -0,0 +1,10 @@
1
+ en:
2
+ mongoid:
3
+ shell:
4
+ errors:
5
+ messages:
6
+ missing_session:
7
+ message: "Missing session."
8
+ summary: "The command requires a session."
9
+ resolution: "Your code is missing a parameter to `new` or `command_for`.
10
+ Typically you can obtain a session from `Mongoid.default_session`."
@@ -0,0 +1,8 @@
1
+ require 'i18n'
2
+
3
+ I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml")
4
+
5
+ require 'mongoid/shell/version'
6
+ require 'mongoid/shell/errors'
7
+ require 'mongoid/shell/properties'
8
+ require 'mongoid/shell/commands'
@@ -0,0 +1,2 @@
1
+ require 'mongoid/shell/commands/base'
2
+ require 'mongoid/shell/commands/mongodump'
@@ -0,0 +1,46 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Commands
4
+ class Base
5
+
6
+ attr_accessor :session
7
+
8
+ class << self
9
+
10
+ def command_for(session)
11
+ self.new({ session: session })
12
+ end
13
+
14
+ end
15
+
16
+ def initialize(options = nil)
17
+ options ||= {}
18
+ options[:session] ||= Mongoid.default_session
19
+ options.each do |sym, val|
20
+ self.send "#{sym}=", val
21
+ end
22
+ raise Mongoid::Shell::Errors::MissingSessionError unless @session
23
+ end
24
+
25
+ def cmd
26
+ self.class.name.downcase.split(':').last
27
+ end
28
+
29
+ def vargs(args = {})
30
+ args.map do |key, property|
31
+ value = self.send(property)
32
+ next unless value
33
+ # TODO: quote other special characters?
34
+ value = '"' + value + '"' if value.include? ' '
35
+ "#{key} #{value}"
36
+ end
37
+ end
38
+
39
+ def to_s
40
+ [ self.cmd, vargs ].flatten.compact.join(" ")
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Commands
4
+ class Mongodump < Mongoid::Shell::Commands::Base
5
+ include Mongoid::Shell::Properties::Host
6
+ include Mongoid::Shell::Properties::Database
7
+ include Mongoid::Shell::Properties::Username
8
+ include Mongoid::Shell::Properties::Password
9
+
10
+ attr_accessor :collection, :query
11
+
12
+ def initialize(attrs = {})
13
+ super
14
+ end
15
+
16
+ def vargs
17
+ super({
18
+ '--host' => :host,
19
+ '--db' => :database_name,
20
+ '--username' => :username,
21
+ '--password' => :password,
22
+ '--collection' => :collection,
23
+ '--query' => :query
24
+ })
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongoid/shell/errors/base'
2
+ require 'mongoid/shell/errors/missing_session_error'
@@ -0,0 +1,81 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Errors
4
+ class Base < StandardError
5
+
6
+ # Problem occurred.
7
+ attr_reader :problem
8
+
9
+ # Summary of the problem.
10
+ attr_reader :summary
11
+
12
+ # Suggested problem resolution.
13
+ attr_reader :resolution
14
+
15
+ # Compose the message.
16
+ # === Parameters
17
+ # [key] Lookup key in the translation table.
18
+ # [attributes] The objects to pass to create the message.
19
+ def compose_message(key, attributes = {})
20
+ @problem = create_problem(key, attributes)
21
+ @summary = create_summary(key, attributes)
22
+ @resolution = create_resolution(key, attributes)
23
+
24
+ "\nProblem:\n #{@problem}"+
25
+ "\nSummary:\n #{@summary}"+
26
+ "\nResolution:\n #{@resolution}"
27
+ end
28
+
29
+ private
30
+
31
+ BASE_KEY = "mongoid.shell.errors.messages" #:nodoc:
32
+
33
+ # Given the key of the specific error and the options hash, translate the
34
+ # message.
35
+ #
36
+ # === Parameters
37
+ # [key] The key of the error in the locales.
38
+ # [options] The objects to pass to create the message.
39
+ #
40
+ # Returns a localized error message string.
41
+ def translate(key, options)
42
+ ::I18n.translate("#{BASE_KEY}.#{key}", { :locale => :en }.merge(options)).strip
43
+ end
44
+
45
+ # Create the problem.
46
+ #
47
+ # === Parameters
48
+ # [key] The error key.
49
+ # [attributes] The attributes to interpolate.
50
+ #
51
+ # Returns the problem.
52
+ def create_problem(key, attributes)
53
+ translate("#{key}.message", attributes)
54
+ end
55
+
56
+ # Create the summary.
57
+ #
58
+ # === Parameters
59
+ # [key] The error key.
60
+ # [attributes] The attributes to interpolate.
61
+ #
62
+ # Returns the summary.
63
+ def create_summary(key, attributes)
64
+ translate("#{key}.summary", attributes)
65
+ end
66
+
67
+ # Create the resolution.
68
+ #
69
+ # === Parameters
70
+ # [key] The error key.
71
+ # [attributes] The attributes to interpolate.
72
+ #
73
+ # Returns the resolution.
74
+ def create_resolution(key, attributes)
75
+ translate("#{key}.resolution", attributes)
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Errors
4
+ class MissingSessionError < Mongoid::Shell::Errors::Base
5
+ def initialize
6
+ super(compose_message("missing_session"))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ require 'mongoid/shell/properties/host'
2
+ require 'mongoid/shell/properties/database'
3
+ require 'mongoid/shell/properties/username'
4
+ require 'mongoid/shell/properties/password'
@@ -0,0 +1,14 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Properties
4
+ module Database
5
+
6
+ # current database name
7
+ def database_name
8
+ session.send(:current_database).name
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Properties
4
+ module Host
5
+
6
+ # database host
7
+ def host
8
+ session.cluster.nodes.first.address
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Properties
4
+ module Password
5
+
6
+ # current password
7
+ def password
8
+ return nil unless session.context.cluster.auth && session.context.cluster.auth.first
9
+ session.context.cluster.auth.first[1][1]
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Properties
4
+ module Username
5
+
6
+ # current username
7
+ def username
8
+ return nil unless session.context.cluster.auth && session.context.cluster.auth.first
9
+ session.context.cluster.auth.first[1][0]
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Shell
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongoid-shell'
2
+
@@ -0,0 +1,20 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "mongoid/shell/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "mongoid-shell"
6
+ s.version = Mongoid::Shell::VERSION
7
+ s.authors = [ "Daniel Doubrovkine" ]
8
+ s.email = "dblock@dblock.org"
9
+ s.platform = Gem::Platform::RUBY
10
+ s.required_rubygems_version = '>= 1.3.6'
11
+ s.files = `git ls-files`.split("\n")
12
+ s.require_paths = [ "lib" ]
13
+ s.homepage = "http://github.com/dblock/mongoid-shell"
14
+ s.licenses = [ "MIT" ]
15
+ s.summary = "Derive shell commands from Mongoid configuration options."
16
+ s.add_dependency "mongoid"
17
+ s.add_dependency "i18n"
18
+ end
19
+
20
+
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Commands::Base do
4
+ context "without a default session" do
5
+ before :each do
6
+ Mongoid.stub(:default_session).and_return(nil)
7
+ end
8
+ it "raises an exception when a session is missing" do
9
+ expect {
10
+ Mongoid::Shell::Commands::Base.new({})
11
+ }.to raise_error Mongoid::Shell::Errors::MissingSessionError, /Missing session./
12
+ end
13
+ it "raises an exception when options are missing" do
14
+ expect {
15
+ Mongoid::Shell::Commands::Base.new(nil)
16
+ }.to raise_error Mongoid::Shell::Errors::MissingSessionError, /Missing session./
17
+ end
18
+ end
19
+ it "creates a command using the default session" do
20
+ command = Mongoid::Shell::Commands::Base.new
21
+ command.session.should eq Mongoid.default_session
22
+ end
23
+ it "creates a command using the session provided" do
24
+ session = Moped::Session.new([ "127.0.0.1:27017" ])
25
+ command = Mongoid::Shell::Commands::Base.new(session: session)
26
+ command.session.should eq session
27
+ end
28
+ it "command_for" do
29
+ command = Mongoid::Shell::Commands::Base.command_for(Mongoid.default_session)
30
+ command.session.should eq Mongoid.default_session
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Commands::Mongodump do
4
+ context "default session" do
5
+ it "defaults to local" do
6
+ Mongoid::Shell::Commands::Mongodump.new.to_s.should == "mongodump --host localhost:27017 --db mongoid_shell_tests"
7
+ end
8
+ it "includes collection" do
9
+ Mongoid::Shell::Commands::Mongodump.new({
10
+ collection: 'test'
11
+ }).to_s.should == "mongodump --host localhost:27017 --db mongoid_shell_tests --collection test"
12
+ end
13
+ it "includes query" do
14
+ Mongoid::Shell::Commands::Mongodump.new({
15
+ query: 'find x'
16
+ }).to_s.should == 'mongodump --host localhost:27017 --db mongoid_shell_tests --query "find x"'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell do
4
+ it "has a version" do
5
+ Mongoid::Shell::VERSION.should_not be_nil
6
+ end
7
+ end
8
+
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+ require 'mongoid'
7
+ require 'mongoid-shell'
8
+
9
+ Mongoid.configure do |config|
10
+ config.connect_to('mongoid_shell_tests')
11
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-shell
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Doubrovkine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description:
47
+ email: dblock@dblock.org
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - .gitignore
53
+ - .rspec
54
+ - .travis.yml
55
+ - CHANGELOG.md
56
+ - Gemfile
57
+ - LICENSE.md
58
+ - README.md
59
+ - Rakefile
60
+ - lib/config/locales/en.yml
61
+ - lib/mongoid-shell.rb
62
+ - lib/mongoid/shell/commands.rb
63
+ - lib/mongoid/shell/commands/base.rb
64
+ - lib/mongoid/shell/commands/mongodump.rb
65
+ - lib/mongoid/shell/errors.rb
66
+ - lib/mongoid/shell/errors/base.rb
67
+ - lib/mongoid/shell/errors/missing_session_error.rb
68
+ - lib/mongoid/shell/properties.rb
69
+ - lib/mongoid/shell/properties/database.rb
70
+ - lib/mongoid/shell/properties/host.rb
71
+ - lib/mongoid/shell/properties/password.rb
72
+ - lib/mongoid/shell/properties/username.rb
73
+ - lib/mongoid/shell/version.rb
74
+ - lib/mongoid_shell.rb
75
+ - mongoid-shell.gemspec
76
+ - spec/mongoid/commands/base_spec.rb
77
+ - spec/mongoid/commands/mongodump_spec.rb
78
+ - spec/mongoid/mongoid_shell_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: http://github.com/dblock/mongoid-shell
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
94
+ - 0
95
+ hash: 408535153
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.6
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Derive shell commands from Mongoid configuration options.
108
+ test_files: []