Skip to content

Commit

Permalink
Merge pull request #6 from giginet/support-2.5
Browse files Browse the repository at this point in the history
Remove constant Deprecate warning on Ruby 2.5
  • Loading branch information
KrauseFx authored Feb 15, 2018
2 parents 46ddf15 + 3e1f93e commit 73982b2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 19 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
language: ruby
cache: bundler
before_install:
- gem update --system
- gem update bundler
rvm:
- 1.9.3
- 2.0.0
- 2.1.10
- 2.2.6
- 2.3.3
- 2.4.0
- 2.2.9
- 2.3.6
- 2.4.3
- 2.5.0
- jruby-19mode
2 changes: 1 addition & 1 deletion commander.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Gem::Specification.new do |s|
s.add_development_dependency('rubocop', '~> 0.41.1')
s.add_development_dependency('json', '< 2.0')
else
s.add_development_dependency('rubocop', '~> 0.46')
s.add_development_dependency('rubocop', '~> 0.49.1')
end
end
47 changes: 35 additions & 12 deletions lib/commander/user_interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,45 @@ def progress(arr, options = {})
# Implements ask_for_CLASS methods.

module AskForClass
DEPRECATED_CONSTANTS = [:Config, :TimeoutError, :MissingSourceFile, :NIL, :TRUE, :FALSE, :Fixnum, :Bignum].freeze
# All special cases in HighLine::Question#convert, except those that implement #parse
(
[Float, Integer, String, Symbol, Regexp, Array, File, Pathname] +
# All Classes that respond to #parse
# Ignore constants that trigger deprecation warnings
(Object.constants - DEPRECATED_CONSTANTS).map do |const|
Object.const_get(const)
end.select do |const|
const.class == Class && const.respond_to?(:parse)
end
).each do |klass|
DEPRECATED_CONSTANTS = [:Config, :TimeoutError, :MissingSourceFile, :NIL, :TRUE, :FALSE, :Fixnum, :Bignum, :Data].freeze

# define methods for common classes
[Float, Integer, String, Symbol, Regexp, Array, File, Pathname].each do |klass|
define_method "ask_for_#{klass.to_s.downcase}" do |prompt|
$terminal.ask(prompt, klass)
end
end

def method_missing(method_name, *arguments, &block)
if method_name.to_s =~ /^ask_for_(.*)/
if arguments.count != 1
fail ArgumentError, "wrong number of arguments (given #{arguments.count}, expected 1)"
end
prompt = arguments.first
requested_class = Regexp.last_match[1]

# All Classes that respond to #parse
# Ignore constants that trigger deprecation warnings
available_classes = (Object.constants - DEPRECATED_CONSTANTS).map do |const|
Object.const_get(const)
end.select do |const|
const.class == Class && const.respond_to?(:parse)
end

klass = available_classes.find { |k| k.to_s.downcase == requested_class }
if klass
$terminal.ask(prompt, klass)
else
super
end
else
super
end
end

def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('ask_for_') || super
end
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/commander/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Commander
VERSION = '4.4.5'.freeze
VERSION = '4.4.6'.freeze
end
45 changes: 43 additions & 2 deletions spec/methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,49 @@
expect(subject.ancestors).to include(Commander::UI)
end

it 'includes Commander::UI::AskForClass' do
expect(subject.ancestors).to include(Commander::UI::AskForClass)
describe 'AskForClass' do
it 'includes Commander::UI::AskForClass' do
expect(subject.ancestors).to include(Commander::UI::AskForClass)
end

describe 'defining methods' do
let(:terminal) { double }

before do
allow(terminal).to receive(:ask)
$_old_terminal = $terminal
$terminal = terminal
end

after do
$terminal = $_old_terminal
end

subject do
Class.new do
include Commander::UI::AskForClass
end.new
end

it 'defines common "ask_for_*" methods' do
expect(subject.respond_to?(:ask_for_float)).to be_truthy
end

it 'responds to "ask_for_*" methods for classes that implement #parse' do
expect(subject.respond_to?(:ask_for_datetime)).to be_truthy
end

it 'fails "ask_for_*" method invocations without a prompt' do
expect do
subject.ask_for_datetime
end.to raise_error(ArgumentError)
end

it 'implements "ask_for_*"' do
expect(terminal).to receive(:ask)
subject.ask_for_datetime('hi')
end
end
end

it 'includes Commander::Delegates' do
Expand Down

0 comments on commit 73982b2

Please sign in to comment.