Menu Worker DSL is a DSL-based Ruby library for effortless creation of dynamic user menus in your programs. With a simple API, you can generate menus based on method invocation.
- Ruby (with DSL implementation);
rainbow
gem.
- proper implementation of DSL approaches;
- handling corner cases;
- functionality expansion.
Just download the menu.rb
to your project and do require
:
require "./menu"
And paste this below example to get started:
# Just some example functions
def first; puts "First function"; end
def second; puts "Second function"; end
def third; puts "Third function"; end
# Build
builder = MenuBuilder.new do
add :second, "My Second Function"
add_at 0, :first, "My First Function" # Add at zero index
add_submenu "My Submenu" do
add :third # Add with default name
end
end
# Get
menu = builder.menu
# Run
runner = MenuRunner.new(menu)
runner.run
Represents a function
with a name
alias.
Designed for internal use.
function
- is either a symbol or a function real name (access: read).name
- an alias for a function in menu (access: read).
Represents a menu object stores all MenuItem
and other MenuMenu
.
name
: The name of the menu (access: read/write).is_submenu
: Indicates whether the menu is a submenu (access: read/write).storage
: An array to store menu items or submenus (access: read).
Adds a function
to the storage with a provided alias
.
Adds a function
to the storage with a provided alias
at index index
.
Adds a menu
to the storage as submenu, where menu
is a MenuMenu
instance.
Removes an item from the menu by index
. Returns true
on success, otherwise false
.
Removes an item from the menu by name
as String. Returns true
on success, otherwise false
.
Returns an index of an item by the name
, otherwise nil
on search failure.
Invokes an item by the index
. Returns true
if success, otherwise false
.
Prints menu items.
Gives an interface to runs the provided menu
.
Runs the provided menu
in CLI mode.
This method shows one of possible ways to create CLI for the menu usage.
Print menu
items in pretty way.
Print additional utility menu items to work with menu.
E.g. it prints exit shortcut and shortcuts to enter remove modes.
Provides a way to configure menu in one block with usage of instance_eval
.
menu
: The MenuMenu instance (access: read/write).
Provides a way to modify the menu
like in initialize
of the class.
Works exactly like MenuMenu
's method.
Works exactly like MenuMenu
's method.
Works like MenuMenu
's method, but creates a submenu instance with a provided name
using &block
first and adds it to menu.
def first; puts "First function"; end
def second; puts "Second function"; end
builder = MenuBuilder.new do
# ...
end
# Do some actions in code
# Then decide to add some items
builder.modify do
add_at 3, :first, "Very Good Function"
add :second
end
def first; puts "First function"; end
def second; puts "Second function"; end
def third; puts "Third function"; end
builder = MenuBuilder.new do
add :first, "My First Function"
add :second, "My Second Function"
add_submenu "My Submenu" do
add :third # Add with default name
end
end
menu = builder.menu
menu.remove_at(0)
menu.remove_at(1)
menu.remove_by("My Third Function")
Notice: This way is less convenient as there is no way to use blocks.
def first; puts "First function"; end
def second; puts "Second function"; end
def third; puts "Third function"; end
builder = MenuBuilder.new do
add :first, "My First Function"
end
menu = builder.menu
menu.add(:second)
menu.add_at(0, :third)
Dmytro Ivashchenko π» π |
Oleksandr Shynkarenko π» π |
-
add custom names for items;
-
remove_at
-
remove_by_name
-
add_at
-
bugfix/add-at-array-bounds
-
feature/messages-about-what-is-happening
-
Add proper system "clear"
-
Move execute() to MenuExecutioner
-
add docs