Skip to content

Commit

Permalink
Merge 37bfd79 into b941ff7
Browse files Browse the repository at this point in the history
  • Loading branch information
2called-chaos committed Mar 24, 2020
2 parents b941ff7 + 37bfd79 commit d658012
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
36 changes: 21 additions & 15 deletions lib/tty/prompt/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,29 @@ def to_s
#
# @api public
def initialize(prompt, **options)
@prompt = prompt
@prefix = options.fetch(:prefix) { @prompt.prefix }
@default = options.fetch(:default) { UndefinedSetting }
@required = options.fetch(:required) { false }
@echo = options.fetch(:echo) { true }
@in = options.fetch(:in) { UndefinedSetting }
@modifier = options.fetch(:modifier) { [] }
@validation = options.fetch(:validation) { UndefinedSetting }
@convert = options.fetch(:convert) { UndefinedSetting }
# Option deprecation
if options[:validation]
warn "[DEPRECATION] The `:validation` option is deprecated. Use `:validate` instead."
options[:validate] = options[:validation]
end

@prompt = prompt
@prefix = options.fetch(:prefix) { @prompt.prefix }
@default = options.fetch(:default) { UndefinedSetting }
@required = options.fetch(:required) { false }
@echo = options.fetch(:echo) { true }
@in = options.fetch(:in) { UndefinedSetting }
@modifier = options.fetch(:modifier) { [] }
@validation = options.fetch(:validate) { UndefinedSetting }
@convert = options.fetch(:convert) { UndefinedSetting }
@active_color = options.fetch(:active_color) { @prompt.active_color }
@help_color = options.fetch(:help_color) { @prompt.help_color }
@error_color = options.fetch(:error_color) { :red }
@value = options.fetch(:value) { UndefinedSetting }
@messages = Utils.deep_copy(options.fetch(:messages) { { } })
@done = false
@help_color = options.fetch(:help_color) { @prompt.help_color }
@error_color = options.fetch(:error_color) { :red }
@value = options.fetch(:value) { UndefinedSetting }
@messages = Utils.deep_copy(options.fetch(:messages) { { } })
@done = false
@first_render = true
@input = nil
@input = nil

@evaluator = Evaluator.new(self)

Expand Down
12 changes: 10 additions & 2 deletions spec/unit/question/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
expect(answer).to eq('piotr@example.com')
end

it 'deprecates :validation option' do
prompt.input << 'piotr@example.com'
prompt.input.rewind

expect do
prompt.ask('What is your email?', validation: :email)
end.to output("[DEPRECATION] The `:validation` option is deprecated. Use `:validate` instead.\n").to_stderr
end

it "provides default error message for wrong input" do
prompt.input << "wrong\np@m.com\n"
prompt.input.rewind
Expand Down Expand Up @@ -83,8 +92,7 @@
prompt.input << "wrong\np@m.com"
prompt.input.rewind

answer = prompt.ask('What is your email?') do |q|
q.validate :email
answer = prompt.ask('What is your email?', validate: :email) do |q|
q.messages[:valid?] = 'Not an email!'
end

Expand Down

0 comments on commit d658012

Please sign in to comment.