Module: Rubycom::Commands
- Defined in:
- lib/rubycom/commands.rb
Class Method Summary collapse
-
.get_commands(base, all = true) ⇒ Hash
Retrieves the singleton methods in the given base and included Modules.
-
.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.
-
.get_top_level_commands(base) ⇒ Array
Discovers the commands specified in the given base without considering the commands contained in sub-modules.
-
.index_commands(base) ⇒ Hash
Discovers the commands specified in the given base and included Modules.
Class Method Details
.get_commands(base, all = true) ⇒ Hash
Retrieves the singleton methods in the given base and included Modules
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
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
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
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 |