summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam P. Marques Jr <abinoam@gmail.com>2017-11-05 11:55:11 -0300
committerGitHub <noreply@github.com>2017-11-05 11:55:11 -0300
commit413ad89036ab535e53642c66333e9622323fdf32 (patch)
tree787b80208ffe6f252dbd9221fe17517f4d46092a
parent46e6bba20ab921cb5600ae60e640bf2417f64efc (diff)
parent56a4fd370bf0d3d2b44afaadf4a5a1098066b22b (diff)
downloadhighline-413ad89036ab535e53642c66333e9622323fdf32.tar.gz
Merge pull request #220 from JEG2/issue_219v2.0.0.pre.develop.13
Resolves #219 - May set response to anything that answers to :call
-rw-r--r--Changelog.md5
-rwxr-xr-xlib/highline.rb9
-rwxr-xr-xlib/highline/question.rb9
-rw-r--r--lib/highline/question_asker.rb4
-rw-r--r--lib/highline/version.rb2
-rwxr-xr-xtest/test_highline.rb40
6 files changed, 56 insertions, 13 deletions
diff --git a/Changelog.md b/Changelog.md
index f19cb9f..53dd055 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,7 +2,10 @@
Below is a complete listing of changes for each revision of HighLine.
-### Unreleased
+### 2.0.0-develop.13 / 2017-11-05
+* PR #219 - Make possible to use a callable as response (@abinoam)
+
+### 2.0.0-develop.12 / 2017-10-19
* PR #218 - Ease transition from 1.7.x to 2.0.x (@abinoam)
* Copy use_color from HighLine.default_instance
* Expose IOConsoleCompatible
diff --git a/lib/highline.rb b/lib/highline.rb
index 33ab7eb..6ece3dc 100755
--- a/lib/highline.rb
+++ b/lib/highline.rb
@@ -479,15 +479,6 @@ class HighLine
private
- #
- # A helper method for sending the output stream and error and repeat
- # of the question.
- #
- def explain_error(error, question)
- say(question.final_responses[error]) if error
- say(question.ask_on_error_msg)
- end
-
# Adds a layer of scope (new_scope) to ask a question inside a
# question, without destroying instance data
def confirm(question)
diff --git a/lib/highline/question.rb b/lib/highline/question.rb
index f93d4d1..27b18b4 100755
--- a/lib/highline/question.rb
+++ b/lib/highline/question.rb
@@ -289,6 +289,15 @@ class HighLine
@internal_responses.merge(@user_responses)
end
+ def final_response(error)
+ response = final_responses[error]
+ if response.respond_to?(:call)
+ response.call(answer)
+ else
+ response
+ end
+ end
+
#
# Returns the provided _answer_string_ after changing character case by
# the rules of this Question. Valid settings for whitespace are:
diff --git a/lib/highline/question_asker.rb b/lib/highline/question_asker.rb
index cb8dfa8..03b3ff7 100644
--- a/lib/highline/question_asker.rb
+++ b/lib/highline/question_asker.rb
@@ -114,8 +114,8 @@ class HighLine
private
## Delegate to Highline
- def explain_error(error)
- @highline.say(question.final_responses[error]) if error
+ def explain_error(explanation_key) # eg: :not_valid, :not_in_range
+ @highline.say(question.final_response(explanation_key))
@highline.say(question.ask_on_error_msg)
end
diff --git a/lib/highline/version.rb b/lib/highline/version.rb
index 41ff060..1def22d 100644
--- a/lib/highline/version.rb
+++ b/lib/highline/version.rb
@@ -2,5 +2,5 @@
class HighLine
# The version of the installed library.
- VERSION = "2.0.0-develop.12".freeze
+ VERSION = "2.0.0-develop.13".freeze
end
diff --git a/test/test_highline.rb b/test/test_highline.rb
index 6f4d1e0..67d8fad 100755
--- a/test/test_highline.rb
+++ b/test/test_highline.rb
@@ -1577,6 +1577,46 @@ class TestHighLine < Minitest::Test
assert_equal("Gray, James Edward", answer)
end
+ def test_validation_with_overriding_static_message
+ @input << "Not valid answer\n" \
+ "42\n"
+
+ @input.rewind
+
+ answer = @terminal.ask("Enter only numbers: ") do |question|
+ question.validate = ->(ans) { ans =~ /\d+/ }
+ question.responses[:not_valid] = "We accept only numbers over here!"
+ end
+
+ assert_equal("42", answer)
+ assert_equal(
+ "Enter only numbers: We accept only numbers over here!\n" \
+ "? ",
+ @output.string
+ )
+ end
+
+ def test_validation_with_overriding_dynamic_message
+ @input << "Forty two\n" \
+ "42\n"
+
+ @input.rewind
+
+ answer = @terminal.ask("Enter only numbers: ") do |question|
+ question.validate = ->(ans) { ans =~ /\d+/ }
+ question.responses[:not_valid] =
+ ->(ans) { "#{ans} is not a valid answer" }
+ end
+
+ assert_equal("42", answer)
+ assert_equal(
+ "Enter only numbers: " \
+ "Forty two is not a valid answer\n" \
+ "? ",
+ @output.string
+ )
+ end
+
def test_whitespace
@input << " A lot\tof \t space\t \there! \n"
@input.rewind