Class: Bcome::Registry::Command::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/objects/registry/command/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Group

Returns a new instance of Group.



3
4
5
6
# File 'lib/objects/registry/command/group.rb', line 3

def initialize(node)
  @all_commands = {}
  @node = node
end

Instance Attribute Details

#all_commandsObject (readonly)

Returns the value of attribute all_commands.



8
9
10
# File 'lib/objects/registry/command/group.rb', line 8

def all_commands
  @all_commands
end

Instance Method Details

#<<(command) ⇒ Object



10
11
12
# File 'lib/objects/registry/command/group.rb', line 10

def <<(command)
  all_commands[command.group] ? (all_commands[command.group] << command) : (all_commands[command.group] = [command])
end

#command_for_console_command_name(command_name) ⇒ Object



30
31
32
# File 'lib/objects/registry/command/group.rb', line 30

def command_for_console_command_name(command_name)
  user_registered_console_commands.select { |command| command.console_command.to_sym == command_name }.first
end

#console_method_name_exists?(proposed_name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/objects/registry/command/group.rb', line 18

def console_method_name_exists?(proposed_name)
  user_registered_console_command_names.include?(proposed_name)
end

#has_commands?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/objects/registry/command/group.rb', line 14

def has_commands?
  all_commands.keys.any?
end

#in_console_session?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/objects/registry/command/group.rb', line 47

def in_console_session?
  ::Bcome::System::Local.instance.in_console_session?
end

#item_spacing(item) ⇒ Object



34
35
36
37
# File 'lib/objects/registry/command/group.rb', line 34

def item_spacing(item)
  raise ::Bcome::Exception::InvalidRegistryCommandNameLength.new "command '#{item}' exceeds length limit of #{menu_item_spacing_length}" if item.length > menu_item_spacing_length
  "\s" * (menu_item_spacing_length - item.length)
end


39
40
41
# File 'lib/objects/registry/command/group.rb', line 39

def menu_item_spacing_length
  16
end

#pretty_printObject

TODO - revisit. This is a mess



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/objects/registry/command/group.rb', line 51

def pretty_print ## TODO - revisit. This is a mess
  puts "\nRegistry commands".title + "\sfor #{@node.class} #{@node.keyed_namespace}".resource_value + "\n\n"
  all_commands.sort.each do |group_name, commands|
    puts tab_spacing + group_name.title + "\n\n"
    commands.each do |command|
      command_key = command.console_command
      description = command.description
      defaults = command.defaults

      puts tab_spacing + command_key.resource_key + item_spacing(command_key) + description.resource_value

      usage_string = in_console_session? ? command_key.to_s : "bcome #{@node.keyed_namespace.empty? ? '' : "#{@node.keyed_namespace}:"}#{command_key}"
      puts tab_spacing + ("\s" * menu_item_spacing_length) + 'usage: '.instructional + usage_string

      if defaults.keys.any?
        defaults_usage = in_console_session? ? "\s\"#{defaults.collect { |key, _value| "#{key}=your-value" }.join(',')}\"" : "\s" + defaults.collect { |key, _value| "#{key}=your-value" }.join("\s")
        puts tab_spacing + ("\s" * menu_item_spacing_length) + "defaults:\s".instructional + defaults.collect { |k, v| "#{k}=#{v}" }.join(', ')
        puts tab_spacing + ("\s" * menu_item_spacing_length) + "override:\s".instructional + usage_string + defaults_usage
      end
      puts "\n"
    end
    puts "\n"
  end
  nil
end

#tab_spacingObject



43
44
45
# File 'lib/objects/registry/command/group.rb', line 43

def tab_spacing
  "\s" * 3
end

#user_registered_console_command_namesObject



22
23
24
# File 'lib/objects/registry/command/group.rb', line 22

def user_registered_console_command_names
  user_registered_console_commands.collect(&:console_command)
end

#user_registered_console_commandsObject



26
27
28
# File 'lib/objects/registry/command/group.rb', line 26

def user_registered_console_commands
  all_commands.collect { |_group, commands| commands }.flatten
end