Class: Binder::Strategy
- Inherits:
-
Object
- Object
- Binder::Strategy
- Defined in:
- lib/cli/command.rb
Overview
Public: A Strategy is a way to declare a specific command. You need to subclass the Binder::Strategy class and declare an ‘execute` method and a `description` method.
Examples
class Migrate < Binder::Strategy
def execute args
# Parse args and do migration stuff
"Migration Done." # <= Binder::Command.new automaticaly renders the return value of an `execute` call
end
#
# The description call is mainly used by the `help` command.
def description
"Easier Migration. Use the " + "--directory".colorize(:orange) + " option to pass in a directory to load."
# Yeah, notice the "colorize" String method that helps you write a string in a beatiful color.
end
end
Class Method Summary collapse
-
.alias_class(_alias) ⇒ Object
Public: creates an alias for the command.
Instance Method Summary collapse
-
#description ⇒ Object
Public: provides a default description if not defined.
-
#justify_size ⇒ Object
Public: returns the String size of the biggest command or option group.
-
#merge_options_aliases ⇒ Object
Public: merge options and aliases “-m, –migrate, -h, –help”, etc…
Class Method Details
Instance Method Details
#description ⇒ Object
Public: provides a default description if not defined.
Returns a “No description found” String.
99 |
# File 'lib/cli/command.rb', line 99 def description; "No description found" end |
#justify_size ⇒ Object
Public: returns the String size of the biggest command or option group.
Returns a Numeric.
64 65 66 67 68 69 70 |
# File 'lib/cli/command.rb', line 64 def justify_size if @options.nil? Binder::Strategy.subclasses.map(&:to_s).group_by(&:size).max.flatten.first - "Binder".length else @options.group_by(&:size).max.flatten.first end end |
#merge_options_aliases ⇒ Object
Public: merge options and aliases “-m, –migrate, -h, –help”, etc…
Uses an array of commands : [“-h” “-m”, “–help”, “–migrate”],
and concatenate the aliases : [“-m, –migrate”, “-h, –help”]
Returns an Array of merged aliases.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cli/command.rb', line 79 def commands = Binder::Strategy.subclasses.map { |command| command.to_s.gsub!('Binder::', '').downcase }.sort previous_cmd = "" = [] commands.each_with_index do |cmd, i| if cmd.length == 1 previous_cmd = cmd elsif not previous_cmd.empty? cmd_prefixes = [previous_cmd.length == 1 ? '-' : '--', cmd.length == 1 ? '-' : '--'] << "#{cmd_prefixes.first}#{previous_cmd}, #{cmd_prefixes.last}#{cmd}" previous_cmd = "" end end @options = end |