summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Keele <dev@chriskeele.com>2018-01-19 19:05:53 -0800
committerChris Keele <dev@chriskeele.com>2018-01-19 20:29:10 -0800
commit69e528349fba9ee7ad12182a3bc705a4876e5319 (patch)
tree90523282c18b116d33eb84b55363cc2baa7684f4 /lib
parent596ae6b29f59d00f0bfb0d4519c32c11e4d15aaf (diff)
downloadbundler-69e528349fba9ee7ad12182a3bc705a4876e5319.tar.gz
Add base error class to new gems.
Closes #6260.
Diffstat (limited to 'lib')
-rw-r--r--lib/bundler/cli.rb2
-rw-r--r--lib/bundler/cli/gem.rb14
-rw-r--r--lib/bundler/templates/newgem/lib/newgem.rb.tt5
3 files changed, 21 insertions, 0 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index 40ba1287fb..7b0b78e6ca 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -515,6 +515,8 @@ module Bundler
method_option :mit, :type => :boolean, :desc => "Generate an MIT license file. Set a default with `bundle config gem.mit true`."
method_option :test, :type => :string, :lazy_default => "rspec", :aliases => "-t", :banner => "rspec",
:desc => "Generate a test directory for your library, either rspec or minitest. Set a default with `bundle config gem.test rspec`."
+ method_option :error_class, :type => :string, :default => "StandardError", :aliases => "-e",
+ :desc => "Generate a base error class for your library."
def gem(name)
end
diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb
index ebfaf75b8c..53cea7d754 100644
--- a/lib/bundler/cli/gem.rb
+++ b/lib/bundler/cli/gem.rb
@@ -28,6 +28,7 @@ module Bundler
@target = SharedHelpers.pwd.join(gem_name)
validate_ext_name if options[:ext]
+ validate_error_class if options[:error_class]
end
def run
@@ -56,6 +57,7 @@ module Bundler
:test => options[:test],
:ext => options[:ext],
:exe => options[:exe],
+ :error_class => options[:error_class],
:bundler_version => bundler_dependency_version,
:github_username => github_username.empty? ? "[USERNAME]" : github_username
}
@@ -242,6 +244,18 @@ module Bundler
exit 1
end
+ def validate_error_class
+ error_class = options[:error_class]
+
+ if (error = Object.const_get(error_class)) && !(error <= Exception) # rubocop:disable Style/InverseMethods
+ Bundler.ui.error "Invalid error class #{error_class}. Please use a class inheriting from Exception."
+ exit 1
+ end
+ rescue NameError
+ Bundler.ui.error "Invalid class name #{error_class} for error class. Please provide an Exception subclass."
+ exit 1
+ end
+
def open_editor(editor, file)
thor.run(%(#{editor} "#{file}"))
end
diff --git a/lib/bundler/templates/newgem/lib/newgem.rb.tt b/lib/bundler/templates/newgem/lib/newgem.rb.tt
index 7d8ad90ab0..457172b403 100644
--- a/lib/bundler/templates/newgem/lib/newgem.rb.tt
+++ b/lib/bundler/templates/newgem/lib/newgem.rb.tt
@@ -6,6 +6,11 @@ require "<%= config[:namespaced_path] %>/<%= config[:underscored_name] %>"
<%- config[:constant_array].each_with_index do |c, i| -%>
<%= " " * i %>module <%= c %>
<%- end -%>
+<%- if config[:error_class] -%>
+<%= " " * config[:constant_array].size %>class Error < <%= config[:error_class] %>
+<%= " " * config[:constant_array].size %> # Base class for your errors
+<%= " " * config[:constant_array].size %>end
+<%- end -%>
<%= " " * config[:constant_array].size %># Your code goes here...
<%- (config[:constant_array].size-1).downto(0) do |i| -%>
<%= " " * i %>end