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.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#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.
-
#interactions ⇒ Object
Returns the value of attribute interactions.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_input(name, options = {}, &interact) ⇒ Object
- #display_name ⇒ Object
-
#initialize(context, description = nil) ⇒ Command
constructor
A new instance of Command.
- #inspect ⇒ Object
- #invoke(inputs = {}, given = {}, global = {}) ⇒ 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 |
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/mothership/command.rb', line 7 def context @context 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 |
#interactions ⇒ Object
Returns the value of attribute interactions.
5 6 7 |
# File 'lib/mothership/command.rb', line 5 def interactions @interactions 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 = {}, &interact) ⇒ Object
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mothership/command.rb', line 93 def add_input(name, = {}, &interact) [:interact] = interact if interact [:description] = .delete(:desc) if .key?(:desc) [:type] ||= case [:default] when true, false :boolean when Integer :integer when Float :floating end unless [:hidden] @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 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 |
#display_name ⇒ Object
36 37 38 |
# File 'lib/mothership/command.rb', line 36 def display_name @name.to_s.gsub("_", "-") end |
#inspect ⇒ Object
32 33 34 |
# File 'lib/mothership/command.rb', line 32 def inspect "\#<Command '#{@name}'>" end |
#invoke(inputs = {}, given = {}, global = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mothership/command.rb', line 59 def invoke(inputs = {}, given = {}, global = {}) @before.each { |f, c| c.new.instance_exec(&f) } name = @name ctx = @context.new(self) ctx.extend @interactions if @interactions ctx.input = Inputs.new(self, ctx, inputs, given, global) action = proc do |*given_inputs| ctx.input = given_inputs.first || ctx.input ctx.run(name) end cmd = self @around.each do |a, c| before = action sub = c.new(cmd, ctx.input) action = proc do |*given_inputs| ctx.input = given_inputs.first || ctx.input sub.instance_exec(before, ctx.input, &a) end end res = ctx.instance_exec(ctx.input, &action) @after.each { |f, c| c.new.instance_exec(&f) } res end |
#usage ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mothership/command.rb', line 40 def usage args = [display_name] @arguments.each do |a| name = (a[:value] || a[:name]).to_s.upcase input = @inputs[a[:name]] if a[:type] == :splat args << "#{name}..." elsif a[:type] == :optional || input.key?(:default) || \ input.key?(:interact) args << "[#{name}]" else args << name end end args.join(" ") end |