Class: Mothership::Command
- Inherits:
-
Object
- Object
- Mothership::Command
- Defined in:
- lib/mothership/command.rb
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#around ⇒ Object
readonly
Returns the value of attribute around.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#description ⇒ Object
Returns the value of attribute description.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_input(name, options = {}, &default) ⇒ Object
-
#initialize(context, description = nil) ⇒ Command
constructor
A new instance of Command.
- #inspect ⇒ Object
- #invoke(inputs) ⇒ Object
- #usage ⇒ Object
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
#after ⇒ Object (readonly)
Returns the value of attribute after.
9 10 11 |
# File 'lib/mothership/command.rb', line 9 def after @after end |
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
7 8 9 |
# File 'lib/mothership/command.rb', line 7 def arguments @arguments end |
#around ⇒ Object (readonly)
Returns the value of attribute around.
9 10 11 |
# File 'lib/mothership/command.rb', line 9 def around @around end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
9 10 11 |
# File 'lib/mothership/command.rb', line 9 def before @before end |
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/mothership/command.rb', line 5 def description @description end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
9 10 11 |
# File 'lib/mothership/command.rb', line 9 def filters @filters end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
7 8 9 |
# File 'lib/mothership/command.rb', line 7 def flags @flags end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
7 8 9 |
# File 'lib/mothership/command.rb', line 7 def inputs @inputs end |
#name ⇒ Object
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, = {}, &default) [:default] = default if default [:description] = .delete(:desc) if .key?(:desc) @flags["--#{name.to_s.gsub("_", "-")}"] = name if [:singular] @flags["--#{[:singular]}"] = name end if aliases = [:aliases] || [: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 = [:argument] type = case opts when true :normal when Symbol opts when Hash opts[:type] end [:argument] = type @arguments << { :name => name, :type => type, :value => [:value] } end @inputs[name] = end |
#inspect ⇒ Object
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 |
#usage ⇒ Object
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 |