Class: Charyf::Command::Base

Inherits:
Thor
  • Object
show all
Includes:
Actions
Defined in:
lib/charyf/utils/command/base.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

#load_generators, #require_application_and_environment!, #start_interfaces!, #start_pipeline!

Class Method Details



75
76
77
# File 'lib/charyf/utils/command/base.rb', line 75

def banner(*)
  "#{executable} #{arguments.map(&:usage).join(' ')} [options]".squish
end

.base_nameObject

Sets the base_name taking into account the current class namespace.

Charyf::Command::TestCommand.base_name # => 'charyf'


82
83
84
85
86
87
88
# File 'lib/charyf/utils/command/base.rb', line 82

def base_name
  @base_name ||= begin
    if base = name.to_s.split("::").first
      base.underscore
    end
  end
end

.command_nameObject

Return command name without namespaces.

Charyf::Command::TestCommand.command_name # => 'test'


93
94
95
96
97
98
99
100
# File 'lib/charyf/utils/command/base.rb', line 93

def command_name
  @command_name ||= begin
    if command = name.to_s.split("::").last
      command.chomp!("Command")
      command.underscore
    end
  end
end

.desc(usage = nil, description = nil, options = {}) ⇒ Object

Tries to get the description from a USAGE file one folder above the command root.



20
21
22
23
24
25
26
# File 'lib/charyf/utils/command/base.rb', line 20

def desc(usage = nil, description = nil, options = {})
  if usage
    super
  else
    @desc ||= ERB.new(File.read(desc_file)).result(binding) if desc_file
  end
end

.desc_file(desc_file = nil) ⇒ Object



28
29
30
31
32
# File 'lib/charyf/utils/command/base.rb', line 28

def desc_file(desc_file = nil)
  @desc_file = desc_file if desc_file

  @desc_file if @desc_file && File.exist?(@desc_file)
end

.executableObject



71
72
73
# File 'lib/charyf/utils/command/base.rb', line 71

def executable
  "bin/charyf #{command_name}"
end

.hide_command!Object

Convenience method to hide this command from the available ones when running charyf help command.



47
48
49
# File 'lib/charyf/utils/command/base.rb', line 47

def hide_command!
  Charyf::Command.hidden_commands << self
end

.inherited(base) ⇒ Object

:nodoc:



51
52
53
54
55
56
57
# File 'lib/charyf/utils/command/base.rb', line 51

def inherited(base) #:nodoc:
  super

  if base.name && base.name !~ /Base$/
    Charyf::Command.subclasses << base
  end
end

.namespace(name = nil) ⇒ Object

Convenience method to get the namespace from the class name. It’s the same as Thor default except that the Command at the end of the class is removed.



37
38
39
40
41
42
43
# File 'lib/charyf/utils/command/base.rb', line 37

def namespace(name = nil)
  if name
    super
  else
    @namespace ||= super.chomp("_command").sub(/:command:/, ":")
  end
end

.perform(command, args, config) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
# File 'lib/charyf/utils/command/base.rb', line 59

def perform(command, args, config) # :nodoc:
  if Charyf::Command::HELP_MAPPINGS.include?(args.first)
    command, args = "help", []
  end

  dispatch(command, args.dup, nil, config)
end

.printing_commandsObject



67
68
69
# File 'lib/charyf/utils/command/base.rb', line 67

def printing_commands
  namespaced_commands
end

Instance Method Details

#helpObject



128
129
130
131
132
133
134
# File 'lib/charyf/utils/command/base.rb', line 128

def help
  if command_name = self.class.command_name
    self.class.command_help(shell, command_name)
  else
    super
  end
end