Module: Rubycom::Commands

Defined in:
lib/rubycom/commands.rb

Class Method Summary collapse

Class Method Details

.get_commands(base, all = true) ⇒ Hash

Retrieves the singleton methods in the given base and included Modules

Parameters:

  • base (Module)

    the module which invoked ‘include Rubycom’

  • all (Boolean) (defaults to: true)

    if true recursively search for included modules’ commands, if false return only top level commands

Returns:

  • (Hash)

    a Hash of Symbols representing the command methods in the given base and it’s included modules (if all=true)



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubycom/commands.rb', line 9

def self.get_commands(base, all=true)
  return {} if base.nil? || !base.respond_to?(:singleton_methods) || !base.respond_to?(:included_modules)
  {
      base.name.to_sym => {
          commands: base.singleton_methods(true).select { |sym| ![:included, :extended].include?(sym) },
          inclusions: base.included_modules.select { |mod|
            ![:Rubycom].include?(mod.name.to_sym)
          }.map { |mod|
            all ? self.get_commands(mod) : mod.name.to_sym
          }
      }
  }
end

.get_longest_command_name(base) ⇒ String

Looks up the commands which will be available on the given base Module and returns the longest command name Used in arranging the command list format

Parameters:

  • base (Module)

    the base Module to look up

Returns:

  • (String)

    the longest command name which will show in a list of commands for the given base Module



56
57
58
59
60
# File 'lib/rubycom/commands.rb', line 56

def self.get_longest_command_name(base)
  return '' if base.nil?
  self.get_commands(base, false).map { |_, mod_hash|
    mod_hash[:commands] + mod_hash[:inclusions].flatten }.flatten.max_by(&:size) or ''
end

.get_top_level_commands(base) ⇒ Array

Discovers the commands specified in the given base without considering the commands contained in sub-modules

Parameters:

  • base (Module)

    the base Module to search

Returns:

  • (Array)

    a list of command name symbols which are defined in the given Module



27
28
29
30
31
32
33
# File 'lib/rubycom/commands.rb', line 27

def self.get_top_level_commands(base)
  return {} if base.nil? || !base.respond_to?(:singleton_methods) || !base.respond_to?(:included_modules)
  excluded_commands = [:included, :extended]
  excluded_modules = [:Rubycom]
  base.singleton_methods(true).select { |sym| !excluded_commands.include?(sym) } +
      base.included_modules.select { |mod| !excluded_modules.include?(mod.name.to_sym) }.map { |mod| mod.name.to_sym }.flatten
end

.index_commands(base) ⇒ Hash

Discovers the commands specified in the given base and included Modules

Parameters:

  • base (Module)

    the base Module to search

Returns:

  • (Hash)

    a set of command name symbols mapped to containing Modules



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubycom/commands.rb', line 39

def self.index_commands(base)
  excluded_commands = [:included, :extended]
  excluded_modules = [:Rubycom]
  Hash[base.singleton_methods(true).select { |sym| !excluded_commands.include?(sym) }.map { |sym|
    [sym, base]
  }].merge(
      base.included_modules.select { |mod| !excluded_modules.include?(mod.name.to_sym) }.map { |mod|
        self.index_commands(mod)
      }.reduce(&:merge) || {}
  )
end