summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatrinox <geofflee21@me.com>2016-02-01 22:01:47 -0800
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2016-02-19 04:15:08 -0300
commit9b21f34ddcfac7605f585680a134fe626e588445 (patch)
tree836540cb03cb857dcf252a82495da83dd7d9e8c5
parent3f1935e62b1b8e6f45d02895956c7d6085da9820 (diff)
downloadhighline-9b21f34ddcfac7605f585680a134fe626e588445.tar.gz
Add menu#build_item covenience, bringing us one step closer to a cleaner API
-rw-r--r--lib/highline/menu.rb13
-rw-r--r--test/test_menu.rb18
2 files changed, 29 insertions, 2 deletions
diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb
index 9318a6f..1560fde 100644
--- a/lib/highline/menu.rb
+++ b/lib/highline/menu.rb
@@ -169,11 +169,24 @@ class HighLine
end
#
+ # This method helps reduce the namespaces in the original call, which would look
+ # like this: HighLine::Menu::MenuItem.new(...)
+ # With #build_item, it looks like this: menu.build_item(...)
+ # @param *args splat args, the same args you would pass to an initialization of
+ # HighLine::Menu::MenuItem
+ # @return [HighLine::Menu::MenuItem] the menu item
+
+ def build_item(*args)
+ MenuItem.new(*args)
+ end
+
+ #
# Adds an item directly to the menu. If you want more configuraiton or options,
# use this method
#
# @param item [Menu::MenuItem] item containing choice fields and more
# @return [void]
+
def add_item(item)
@items << item
@help.merge!(item.item_help)
diff --git a/test/test_menu.rb b/test/test_menu.rb
index 777b977..d944794 100644
--- a/test/test_menu.rb
+++ b/test/test_menu.rb
@@ -113,7 +113,7 @@ class TestMenu < Minitest::Test
"? ", @output.string)
end
- def test_menu_item_index_selects_name
+ def test_menu_add_item_index_selects_name
@input << "1\n"
@input.rewind
@@ -127,7 +127,7 @@ class TestMenu < Minitest::Test
"? ", @output.string)
end
- def test_menu_item_selections_matches_name
+ def test_menu_add_item_selections_matches_name
@input << "Sample2\n"
@input.rewind
@@ -141,6 +141,20 @@ class TestMenu < Minitest::Test
"? ", @output.string)
end
+ def test_menu_build_item
+ @input << "Sample2\n"
+ @input.rewind
+
+ selected = @terminal.choose do |menu|
+ menu.add_item(menu.build_item("Sample1", text: "Sample2"))
+ menu.add_item(menu.build_item("Sample2", text: "Sample1"))
+ end
+ assert_equal(selected, "Sample2")
+ assert_equal("1. Sample2\n" +
+ "2. Sample1\n" +
+ "? ", @output.string)
+ end
+
def test_help
@input << "help\nhelp load\nhelp rules\nhelp missing\n"
@input.rewind