Class: Cosm::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cosm/command/base.rb

Direct Known Subclasses

Feeds, Help, Subscribe, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = [], options = {}) ⇒ Base

Returns a new instance of Base.



13
14
15
16
# File 'lib/cosm/command/base.rb', line 13

def initialize(args=[], options={})
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/cosm/command/base.rb', line 10

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/cosm/command/base.rb', line 11

def options
  @options
end

Class Method Details

.alias_command(new, old) ⇒ Object



54
55
56
57
# File 'lib/cosm/command/base.rb', line 54

def self.alias_command(new, old)
  raise "no such command: #{old}" unless Cosm::Command.commands[old]
  Cosm::Command.command_aliases[new] = old
end

.extract_banner(help) ⇒ Object



94
95
96
# File 'lib/cosm/command/base.rb', line 94

def self.extract_banner(help)
  help.first
end

.extract_description(help) ⇒ Object



102
103
104
105
106
# File 'lib/cosm/command/base.rb', line 102

def self.extract_description(help)
  help.reject do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.join("\n")
end

.extract_help(file, line_number) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cosm/command/base.rb', line 76

def self.extract_help(file, line_number)
  buffer = []
  lines = Cosm::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) ⇒ Object

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 color 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).



67
68
69
70
71
72
73
74
# File 'lib/cosm/command/base.rb', line 67

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) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/cosm/command/base.rb', line 108

def self.extract_options(help)
  help.select do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.inject({}) do |hash, line|
    description = line.split("#", 2).last
    long  = line.match(/--([A-Za-z\- ]+)/)[1].strip
    short = line.match(/-([A-Za-z ])[ ,]/) && $1 && $1.strip
    hash.update(long.split(" ").first => { :desc => description, :short => short, :long => long })
  end
end

.extract_summary(help) ⇒ Object



98
99
100
# File 'lib/cosm/command/base.rb', line 98

def self.extract_summary(help)
  extract_description(help).split("\n")[2].to_s.split("\n").first
end

.inherited(klass) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/cosm/command/base.rb', line 20

def self.inherited(klass)
  unless klass == Cosm::Command::Base
    help = extract_help_from_caller(caller.first)

    Cosm::Command.register_namespace(
      :name => klass.namespace,
      :description => help.first
    )
  end
end

.method_added(method) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cosm/command/base.rb', line 31

def self.method_added(method)
  return if self == Cosm::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

  Cosm::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)
  )
end

.namespaceObject



6
7
8
# File 'lib/cosm/command/base.rb', line 6

def self.namespace
  self.to_s.split("::").last.downcase
end