Module: Greenhouse::Commands::Command::ClassMethods

Defined in:
lib/greenhouse/commands/command.rb

Instance Method Summary collapse

Instance Method Details

#after_hooksObject



53
54
55
56
# File 'lib/greenhouse/commands/command.rb', line 53

def after_hooks
  @after_hooks ||= []
  @after_hooks
end

#append_after_hook(&block) ⇒ Object Also known as: after_hook



42
43
44
45
# File 'lib/greenhouse/commands/command.rb', line 42

def append_after_hook(&block)
  @after_hooks ||= []
  @after_hooks << block
end

#append_before_hook(&block) ⇒ Object Also known as: before_hook



58
59
60
61
# File 'lib/greenhouse/commands/command.rb', line 58

def append_before_hook(&block)
  @before_hooks ||= []
  @before_hooks << block
end

#before_hooksObject



69
70
71
72
# File 'lib/greenhouse/commands/command.rb', line 69

def before_hooks
  @before_hooks ||= []
  @before_hooks
end

#command_name(n = nil) ⇒ Object



13
14
15
16
17
# File 'lib/greenhouse/commands/command.rb', line 13

def command_name(n=nil)
  @command_name = n unless n.nil?
  return @command_name unless @command_name.nil?
  self.name.underscore.split("/").last
end

#command_summary(summary = nil) ⇒ Object Also known as: summary



19
20
21
22
# File 'lib/greenhouse/commands/command.rb', line 19

def command_summary(summary=nil)
  @command_summary = summary unless summary.nil?
  @command_summary
end

#prepend_after_hook(&block) ⇒ Object



48
49
50
51
# File 'lib/greenhouse/commands/command.rb', line 48

def prepend_after_hook(&block)
  @after_hooks ||= []
  @after_hooks.unshift block
end

#prepend_before_hook(&block) ⇒ Object



64
65
66
67
# File 'lib/greenhouse/commands/command.rb', line 64

def prepend_before_hook(&block)
  @before_hooks ||= []
  @before_hooks.unshift block
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/greenhouse/commands/command.rb', line 29

def run
  begin
    @command ||= new
    before_hooks.each { |block| @command.instance_eval(&block) }
    @command.run
    after_hooks.each { |block| @command.instance_eval(&block) }
  rescue Exception => e
    puts e.message
    @command.usage if e.is_a?(Scripts::InvalidArgument)
    return
  end
end

#to_argObject



74
75
76
# File 'lib/greenhouse/commands/command.rb', line 74

def to_arg
  Scripts::Argument.new(command_name, :summary => command_summary)
end

#usageObject



25
26
27
# File 'lib/greenhouse/commands/command.rb', line 25

def usage
  puts "usage: #{::Greenhouse::CLI.command_name} #{command_name} #{valid_arguments.to_s}"
end