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.



5
6
7
8
# File 'lib/objects/registry/command/group.rb', line 5

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

Instance Attribute Details

#all_commandsObject (readonly)

Returns the value of attribute all_commands.



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

def all_commands
  @all_commands
end

Instance Method Details

#<<(command) ⇒ Object



12
13
14
# File 'lib/objects/registry/command/group.rb', line 12

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



32
33
34
# File 'lib/objects/registry/command/group.rb', line 32

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)


20
21
22
# File 'lib/objects/registry/command/group.rb', line 20

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

#has_commands?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/objects/registry/command/group.rb', line 16

def has_commands?
  all_commands.keys.any?
end

#in_console_session?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/objects/registry/command/group.rb', line 50

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

#item_spacing(item) ⇒ Object



36
37
38
39
40
# File 'lib/objects/registry/command/group.rb', line 36

def item_spacing(item)
  raise ::Bcome::Exception::InvalidRegistryCommandNameLength, "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


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

def menu_item_spacing_length
  16
end

#pretty_printObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/objects/registry/command/group.rb', line 54

def pretty_print
  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 = if in_console_session?
                       command_key.to_s
                     else
                       "bcome #{@node.keyed_namespace.empty? ? '' : "#{@node.keyed_namespace}:"}#{command_key}"
                     end
      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")}" : "\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



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

def tab_spacing
  "\s" * 3
end

#user_registered_console_command_namesObject



24
25
26
# File 'lib/objects/registry/command/group.rb', line 24

def user_registered_console_command_names
  user_registered_console_commands.collect(&:console_command)
end

#user_registered_console_commandsObject



28
29
30
# File 'lib/objects/registry/command/group.rb', line 28

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