summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam P. Marques Jr <abinoam@gmail.com>2017-09-24 08:22:02 -0300
committerAbinoam P. Marques Jr <abinoam@gmail.com>2017-09-25 06:49:34 -0300
commitb4191cec86ab13e5531afd5cdc52bc44e0374fd1 (patch)
treed207d036017051b42f755329c6903bd30e065055
parent7063e71f864d9afbfb296495ec01b19233ff9c8c (diff)
downloadhighline-b4191cec86ab13e5531afd5cdc52bc44e0374fd1.tar.gz
Improve StringExtension method_missing
-rw-r--r--.rubocop.yml5
-rw-r--r--lib/highline/string_extensions.rb12
-rw-r--r--test/test_string_extension.rb6
3 files changed, 20 insertions, 3 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index afc480d..3ab76a8 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -54,3 +54,8 @@ Style/Encoding:
Style/OptionalArguments:
Exclude:
- 'lib/highline/list_renderer.rb'
+
+# TemplateRenderer should never fail on method missing.
+Style/MethodMissing:
+ Exclude:
+ - 'lib/highline/template_renderer.rb'
diff --git a/lib/highline/string_extensions.rb b/lib/highline/string_extensions.rb
index aab4c27..bdeaf5a 100644
--- a/lib/highline/string_extensions.rb
+++ b/lib/highline/string_extensions.rb
@@ -11,6 +11,8 @@ class HighLine
# HighLine extensions for String class.
# Included by HighLine::String.
module StringExtensions
+ STYLE_METHOD_NAME_PATTERN = /^(on_)?rgb_([0-9a-fA-F]{6})$/
+
# Included hook. Actions to take when being included.
# @param base [Class, Module] base class
def self.included(base)
@@ -58,14 +60,18 @@ class HighLine
# @todo Chain existing method_missing?
undef :method_missing if method_defined? :method_missing
def method_missing(method, *_args)
- if method.to_s =~ /^(on_)?rgb_([0-9a-fA-F]{6})$/
+ if method.to_s =~ STYLE_METHOD_NAME_PATTERN
color(method)
else
- raise NoMethodError, "undefined method `#{method}' for" /
- "#<#{self.class}:#{format('%#x', object_id)}>"
+ super
end
end
+ undef :respond_to_missing if method_defined? :respond_to_missing
+ def respond_to_missing?(method_name, include_private = false)
+ method_name.to_s =~ STYLE_METHOD_NAME_PATTERN || super
+ end
+
private
def setup_color_code(*colors)
diff --git a/test/test_string_extension.rb b/test/test_string_extension.rb
index 44b514e..125f48e 100644
--- a/test/test_string_extension.rb
+++ b/test/test_string_extension.rb
@@ -71,4 +71,10 @@ class TestStringExtension < Minitest::Test
end
assert_equal 1, @include_received
end
+
+ def test_respond_to_dynamic_style_methods
+ string = HighLine::String.new("pirarucu")
+ assert_respond_to(string, :on_rgb_123456)
+ assert_respond_to(string, :rgb_abcdef)
+ end
end