Module: HammerCLI::Subcommand::ClassMethods

Defined in:
lib/hammer_cli/subcommand.rb

Instance Method Summary collapse

Instance Method Details

#define_subcommand(name, subcommand_class, definition, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hammer_cli/subcommand.rb', line 91

def define_subcommand(name, subcommand_class, definition, &block)
  existing = find_subcommand(name)
  if existing
    raise HammerCLI::CommandConflict, _("Can't replace subcommand %<name>s (%<existing_class>s) with %<name>s (%<new_class>s).") % {
      :name => name,
      :existing_class => existing.subcommand_class,
      :new_class => subcommand_class
    }
  end
  subcommand_class = Class.new(subcommand_class, &block) if block
  declare_subcommand_parameters unless has_subcommands?
  recognised_subcommands << definition
end

#lazy_subcommand(name, description, subcommand_class_name, path, options = {}) ⇒ Object



80
81
82
83
# File 'lib/hammer_cli/subcommand.rb', line 80

def lazy_subcommand(name, description, subcommand_class_name, path, options = {})
  definition = LazyDefinition.new(name, description, subcommand_class_name, path, options)
  define_subcommand(name, Class, definition)
end

#lazy_subcommand!(name, description, subcommand_class_name, path, options = {}) ⇒ Object



85
86
87
88
89
# File 'lib/hammer_cli/subcommand.rb', line 85

def lazy_subcommand!(name, description, subcommand_class_name, path, options = {})
  remove_subcommand(name)
  self.lazy_subcommand(name, description, subcommand_class_name, path, options)
  logger.info "subcommand #{name} (#{subcommand_class_name}) was created."
end

#remove_subcommand(name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/hammer_cli/subcommand.rb', line 58

def remove_subcommand(name)
  self.recognised_subcommands.delete_if do |sc|
    if sc.is_called?(name)
      logger.info "subcommand #{name} (#{sc.subcommand_class}) was removed."
      true
    else
      false
    end
  end
end

#subcommand(name, description, subcommand_class = self, options = {}, &block) ⇒ Object



75
76
77
78
# File 'lib/hammer_cli/subcommand.rb', line 75

def subcommand(name, description, subcommand_class = self, options = {}, &block)
  definition = Definition.new(name, description, subcommand_class, options)
  define_subcommand(name, subcommand_class, definition, &block)
end

#subcommand!(name, description, subcommand_class = self, options = {}, &block) ⇒ Object



69
70
71
72
73
# File 'lib/hammer_cli/subcommand.rb', line 69

def subcommand!(name, description, subcommand_class = self, options = {}, &block)
  remove_subcommand(name)
  subcommand(name, description, subcommand_class, options, &block)
  logger.info "subcommand #{name} (#{subcommand_class}) was created."
end