Module: EasyAppHelper::Scripts::SubCommandManager

Defined in:
lib/easy_app_helper/scripts/sub_command_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_nameObject



34
35
36
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 34

def self.by_name
  @by_name ||= {}
end

.by_providerObject



30
31
32
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 30

def self.by_provider
  @by_provider ||= {}
end

.register(sub_command_class) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 6

def self.register(sub_command_class)
  raise 'Please specify a sub_command class when registering' if sub_command_class.nil?
  raise "Already registered sub_command '#{sub_command_class.to_s}' !" if sub_command_classes.include? sub_command_class
  msg = "Registering handler '#{sub_command_class.to_s}' for sub-command '#{sub_command_class::NAME}'"
  unless sub_command_class::PROVIDER == EasyAppHelper::Scripts::SubCommandBase::PROVIDER
    msg += " (provided by '#{sub_command_class::PROVIDER}' plugin)"
  end
  EasyAppHelper.logger.debug msg
  sub_command_classes << sub_command_class
  by_provider[sub_command_class::PROVIDER] ||= []
  raise 'A provider cannot provide the same sub-command multiple times' if by_provider[sub_command_class::PROVIDER].include?(sub_command_class)
  by_provider[sub_command_class::PROVIDER] << sub_command_class
  by_name[sub_command_class::NAME] ||= []
  by_name[sub_command_class::NAME] << sub_command_class
  sub_command_class::ALIASES.each do |command_alias|
    by_name[command_alias] ||= []
    by_name[command_alias] << sub_command_class
  end
end

.sub_command_class(command_name_or_alias, preferred_provider = EasyAppHelper::Scripts::SubCommandBase::PROVIDER) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 38

def self.sub_command_class(command_name_or_alias, preferred_provider=EasyAppHelper::Scripts::SubCommandBase::PROVIDER)
  candidates = by_name[command_name_or_alias].select do |sub_command_class|
    command_classes_for_command = by_name[command_name_or_alias]
    raise "There is no provider declared for command '#{command_name_or_alias}'" if command_classes_for_command.nil?
    command_classes_for_command.include? sub_command_class
  end
  if candidates.size > 1
    candidates.select! do |sub_command_class|
      sub_command_class::PROVIDER == preferred_provider
    end
    raise "Cannot determine provider to use for '#{command_name_or_alias}'. Multiple providers exist !"
  end
  candidates.first
end

.sub_command_classesObject



26
27
28
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 26

def self.sub_command_classes
  @sub_command_classes ||= []
end

Instance Method Details

#default_headerObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 78

def default_header
  <<EOF

This is the '#{script_name}' tool version #{config.app_version} (AKA '#{self.class::NAME}').
#{self.class::DESCRIPTION}

It has some sub-commands, each taking its own options.

You can do '#{script_name} <sub-command> --help' for more information on sub-modules.

Options

  --help/-h    : This help.
  --version    : Echoes dm version

Sub-commands:

EOF
end

#delegate_to_sub_command(preferred_provider = EasyAppHelper::Scripts::SubCommandBase::PROVIDER) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 53

def delegate_to_sub_command(preferred_provider=EasyAppHelper::Scripts::SubCommandBase::PROVIDER)
  sub_command_name = extra_parameters.shift
  sub_command = EasyAppHelper::Scripts::SubCommandManager.sub_command_class(sub_command_name, preferred_provider).new
  sub_command.pre_process
  raise 'You have to implement \'do_process\'' unless sub_command.respond_to? :do_process
  sub_command.do_process
end

#display_helpObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/easy_app_helper/scripts/sub_command_manager.rb', line 62

def display_help
  result = [default_header]

  EasyAppHelper::Scripts::SubCommandManager.sub_command_classes.group_by do |sub_command_classes|
    sub_command_classes::CATEGORY
  end .each_pair do |category, sub_command_classes_for_category|
    result << ('  %s:' % category)
    result << ''
    sub_command_classes_for_category.each do |sub_command_class|
      result << sub_command_class.help_line
    end
    result << ''
  end
  result
end