Class: Mothership::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, description = nil) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mothership/command.rb', line 11

def initialize(context, description = nil)
  @context = context
  @description = description
  @aliases = []

  # inputs accepted by command
  @inputs = {}

  # inputs that act as arguments
  @arguments = []

  # flag -> input (e.g. --name -> :name)
  @flags = {}

  # various callbacks
  @before = []
  @after = []
  @around = []
  @filters = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



9
10
11
# File 'lib/mothership/command.rb', line 9

def after
  @after
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



7
8
9
# File 'lib/mothership/command.rb', line 7

def arguments
  @arguments
end

#aroundObject (readonly)

Returns the value of attribute around.



9
10
11
# File 'lib/mothership/command.rb', line 9

def around
  @around
end

#beforeObject (readonly)

Returns the value of attribute before.



9
10
11
# File 'lib/mothership/command.rb', line 9

def before
  @before
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/mothership/command.rb', line 5

def description
  @description
end

#filtersObject (readonly)

Returns the value of attribute filters.



9
10
11
# File 'lib/mothership/command.rb', line 9

def filters
  @filters
end

#flagsObject (readonly)

Returns the value of attribute flags.



7
8
9
# File 'lib/mothership/command.rb', line 7

def flags
  @flags
end

#inputsObject (readonly)

Returns the value of attribute inputs.



7
8
9
# File 'lib/mothership/command.rb', line 7

def inputs
  @inputs
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/mothership/command.rb', line 5

def name
  @name
end

Instance Method Details

#add_input(name, options = {}, &default) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mothership/command.rb', line 83

def add_input(name, options = {}, &default)
  options[:default] = default if default
  options[:description] = options.delete(:desc) if options.key?(:desc)

  @flags["--#{name.to_s.gsub("_", "-")}"] = name

  if options[:singular]
    @flags["--#{options[:singular]}"] = name
  end

  if aliases = options[:aliases] || options[:alias]
    Array(aliases).each do |a|
      @flags[a] = name
    end
  end

  # :argument => true means accept as single argument
  # :argument => :foo is shorthand for :argument => {:type => :foo}
  if opts = options[:argument]
    type =
      case opts
      when true
        :normal
      when Symbol
        opts
      when Hash
        opts[:type]
      end

    options[:argument] = type

    @arguments <<
      { :name => name,
        :type => type,
        :value => options[:value]
      }
  end

  @inputs[name] = options
end

#inspectObject



32
33
34
# File 'lib/mothership/command.rb', line 32

def inspect
  "\#<Command '#{@name}'>"
end

#invoke(inputs) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mothership/command.rb', line 55

def invoke(inputs)
  @before.each { |f, c| c.new.instance_exec(&f) }

  name = @name
  ctx = @context.new(self)
  input = Inputs.new(self, ctx, inputs)

  action = proc do |*given_inputs|
    ctx.send(name, given_inputs.first || input)
  end

  cmd = self
  @around.each do |a, c|
    before = action

    sub = c.new(cmd)
    action = proc do |*given_inputs|
      sub.instance_exec(before, given_inputs.first || input, &a)
    end
  end

  res = ctx.instance_exec(input, &action)

  @after.each { |f, c| c.new.instance_exec(&f) }

  res
end

#usageObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mothership/command.rb', line 36

def usage
  str = @name.to_s.gsub("_", "-")

  @arguments.each do |a|
    name = (a[:value] || a[:name]).to_s.upcase

    case a[:type]
    when :splat
      str << " #{name}..."
    when :optional
      str << " [#{name}]"
    else
      str << " #{name}"
    end
  end

  str
end