class PUNK::Command

Attributes

args[RW]
opts[RW]

Public Class Methods

commander() click to toggle source
# File lib/punk/framework/command.rb, line 41
def self.commander
  PUNK.store.commands.each_value do |command|
    command.send(:_commander)
  end
end
create(name, &block) click to toggle source
# File lib/punk/framework/command.rb, line 10
def self.create(name, &block)
  command = new
  command.send(:_init, name)
  PUNK.store.commands ||= {}
  PUNK.store.commands[name] = command
  command.instance_eval(&block)
end
pry() click to toggle source
# File lib/punk/framework/command.rb, line 35
def self.pry
  PUNK.store.commands.each_value do |command|
    command.send(:_pry)
  end
end
spec(scope) click to toggle source
# File lib/punk/framework/command.rb, line 47
def self.spec(scope)
  PUNK.store.commands.each_value do |command|
    command.send(:_spec, scope)
  end
end

Public Instance Methods

description(text) click to toggle source
# File lib/punk/framework/command.rb, line 22
def description(text)
  instance_variable_set(:@description, text)
end
option(name:, description:, shortcut: nil, type: String) click to toggle source
# File lib/punk/framework/command.rb, line 26
def option(name:, description:, shortcut: nil, type: String)
  @options[name] = {
    name: name,
    description: description,
    shortcut: shortcut,
    type: type
  }
end
process() click to toggle source
# File lib/punk/framework/command.rb, line 53
def process
  raise NotImplemented, "command must provide process method"
end
shortcut(name) click to toggle source
# File lib/punk/framework/command.rb, line 18
def shortcut(name)
  instance_variable_set(:@shortcut, name)
end

Private Instance Methods

_commander() click to toggle source
# File lib/punk/framework/command.rb, line 92
def _commander
  command @name do |c|
    c.description = @description
    @options.each_value do |option|
      c.option "-#{option[:shortcut]}", option[:description], "--#{option[:name]} #{option[:type].to_s.upcase}", option[:type]
    end
    c.action do |args, opts|
      @args = args
      @opts = opts.__hash__
      PUNK.exec
      result = process
      SemanticLogger.flush
      puts result # rubocop:disable Rails/Output
    end
  end
  return unless @shortcut
  alias_command @shortcut, @name
end
_init(name) click to toggle source
# File lib/punk/framework/command.rb, line 59
def _init(name)
  @name = name
  @shortcut = nil
  @description = nil
  @options = {}
end
_pry() click to toggle source
# File lib/punk/framework/command.rb, line 66
def _pry
  command = Pry::Commands.create_command(@name) {} # rubocop:disable Lint/EmptyBlock
  command.description = @description
  command.instance_variable_set(:@group, "punk")
  command.class_eval do
    define_method(:options) do |opt|
      punk_command = PUNK.store.commands[match]
      punk_command.instance_variable_get(:@options).each_value do |option|
        if option[:type].present?
          opt.on option[:shortcut], option[:name], option[:description], argument: true, as: option[:type]
        else
          opt.on option[:shortcut], option[:name], option[:description]
        end
      end
    end
    define_method(:process) do
      punk_command = PUNK.store.commands[match]
      punk_command.instance_variable_set(:@args, args)
      punk_command.instance_variable_set(:@opts, opts.to_h)
      result = punk_command.process
      SemanticLogger.flush
      output.puts result
    end
  end
end
_spec(scope) click to toggle source
# File lib/punk/framework/command.rb, line 111
def _spec(scope)
  this = self
  SpecHelpers.send(:define_method, "command_#{@name}") do |*args, **kwargs|
    this.instance_variable_set(:@args, args)
    this.instance_variable_set(:@opts, kwargs)
    this.process
  end
  scope.include SpecHelpers
end