class Remind101::Command::Base

Attributes

args[R]
options[R]

Public Class Methods

namespace() click to toggle source
# File lib/remind101/command/base.rb, line 7
def self.namespace
  self.to_s.split("::").last.downcase
end
new(args=[], options={}) click to toggle source
# File lib/remind101/command/base.rb, line 14
def initialize(args=[], options={})
  @args = args
  @options = options
end

Protected Class Methods

alias_command(new, old) click to toggle source
# File lib/remind101/command/base.rb, line 61
def self.alias_command(new, old)
  raise "no such command: #{old}" unless Remind101::Command.commands[old]
  Remind101::Command.command_aliases[new] = old
end
extract_banner(help) click to toggle source
# File lib/remind101/command/base.rb, line 106
def self.extract_banner(help)
  help.first
end
extract_description(help) click to toggle source
# File lib/remind101/command/base.rb, line 114
def self.extract_description(help)
  help.reject do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.join("\n")
end
extract_help(file, line_number) click to toggle source
# File lib/remind101/command/base.rb, line 88
def self.extract_help(file, line_number)
  buffer = []
  lines = Remind101::Command.files[file]

  (line_number.to_i-2).downto(0) do |i|
    line = lines[i]
    case line[0..0]
      when ""
      when "#"
        buffer.unshift(line[1..-1])
      else
        break
    end
  end

  buffer
end
extract_help_from_caller(line) click to toggle source

Parse the caller format and identify the file and line number as identified in : www.ruby-doc.org/core/classes/Kernel.html#M001397. This will look for a colon followed by a digit as the delimiter. The biggest complication is windows paths, which have a colon after the drive letter. This regex will match paths as anything from the beginning to a colon directly followed by a number (the line number).

Examples of the caller format :

  • c:/Ruby192/lib/…/lib/heroku/command/addons.rb:8:in ‘<module:Command>’

  • c:/Ruby192/lib/…/heroku-2.0.1/lib/heroku/command/pg.rb:96:in ‘<class:Pg>’

  • /Users/ph7/.…./xray-1.1/lib/xray/thread_dump_signal_handler.rb:9

# File lib/remind101/command/base.rb, line 79
def self.extract_help_from_caller(line)
  # pull out of the caller the information for the file path and line number
  if line =~ /^(.+?):(\d+)/
    extract_help($1, $2)
  else
    raise("unable to extract help from caller: #{line}")
  end
end
extract_options(help) click to toggle source
# File lib/remind101/command/base.rb, line 120
def self.extract_options(help)
  help.select do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.inject([]) do |options, line|
    args = line.split('#', 2).first
    args = args.split(/,\s*/).map {|arg| arg.strip}.sort.reverse
    name = args.last.split(' ', 2).first[2..-1]
    options << { :name => name, :args => args }
  end
end
extract_summary(help) click to toggle source
# File lib/remind101/command/base.rb, line 110
def self.extract_summary(help)
  extract_description(help).split("\n")[2].to_s.split("\n").first
end
inherited(klass) click to toggle source
# File lib/remind101/command/base.rb, line 25
def self.inherited(klass)
  unless klass == Remind101::Command::Base
    help = extract_help_from_caller(caller.first)

    Remind101::Command.register_namespace(
      :name => klass.namespace,
      :description => help.first
    )
  end
end
method_added(method) click to toggle source
# File lib/remind101/command/base.rb, line 36
def self.method_added(method)
  return if self == Remind101::Command::Base
  return if private_method_defined?(method)
  return if protected_method_defined?(method)

  help = extract_help_from_caller(caller.first)
  resolved_method = (method.to_s == "index") ? nil : method.to_s
  command = [ self.namespace, resolved_method ].compact.join(":")
  banner = extract_banner(help) || command

  Remind101::Command.register_command(
    :klass       => self,
    :method      => method,
    :namespace   => self.namespace,
    :command     => command,
    :banner      => banner.strip,
    :help        => help.join("\n"),
    :summary     => extract_summary(help),
    :description => extract_description(help),
    :options     => extract_options(help)
  )

  alias_command command.gsub(/_/, '-'), command if command =~ /_/
end

Public Instance Methods

remind101() click to toggle source
# File lib/remind101/command/base.rb, line 19
def remind101
  Remind101::Client.new(auth_token: Remind101::Auth.token)
end

Protected Instance Methods

current_command() click to toggle source
# File lib/remind101/command/base.rb, line 131
def current_command
  Remind101::Command.current_command
end
escape(value) click to toggle source
# File lib/remind101/command/base.rb, line 151
def escape(value)
  heroku.escape(value)
end
extract_option(key) click to toggle source
# File lib/remind101/command/base.rb, line 135
def extract_option(key)
  options[key.dup.gsub('-','_').to_sym]
end
invalid_arguments() click to toggle source
# File lib/remind101/command/base.rb, line 139
def invalid_arguments
  Remind101::Command.invalid_arguments
end
shift_argument() click to toggle source
# File lib/remind101/command/base.rb, line 143
def shift_argument
  Remind101::Command.shift_argument
end
validate_arguments!() click to toggle source
# File lib/remind101/command/base.rb, line 147
def validate_arguments!
  Remind101::Command.validate_arguments!
end