Class: Brash::Command
- Inherits:
-
Object
show all
- Defined in:
- lib/brash.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(command, *args) ⇒ Command
Returns a new instance of Command.
28
29
30
31
|
# File 'lib/brash.rb', line 28
def initialize(command, *args)
@command = command
@args = Array(args).join("")
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
25
26
27
|
# File 'lib/brash.rb', line 25
def method_missing(method, *args, &block)
@compound_command = Command.new(method, *args, &block).tap{|c| c.context = context}
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
24
25
26
|
# File 'lib/brash.rb', line 24
def context
@context
end
|
Instance Method Details
#arg(arg) ⇒ Object
41
42
43
44
|
# File 'lib/brash.rb', line 41
def arg(arg)
@args += " " + escape_arg(arg)
self
end
|
#dash(*args) ⇒ Object
36
37
38
39
40
|
# File 'lib/brash.rb', line 36
def dash(*args)
@args += "-#{args.first}" if args.length > 0
@args += " " + args[1..-1].map{|arg| escape_arg(arg)}.join(" ") if args.length > 1
self
end
|
#escape(bash, depth = context.depth) ⇒ Object
53
54
55
56
57
|
# File 'lib/brash.rb', line 53
def escape(bash, depth=context.depth)
escaped = bash
escaped = "\\"*(depth-1) + "\"" + "#{escaped}" + "\\"*(depth-1) + "\"" if depth > 0
escaped
end
|
#escape_arg(arg) ⇒ Object
45
46
47
48
49
50
51
52
|
# File 'lib/brash.rb', line 45
def escape_arg(arg)
if arg.is_a?(Proc)
CommandContext.surround(context.depth + 1, &arg).command.to_bash
elsif arg.is_a?(String) && arg.include?(" ")
escape(arg, context.depth+1)
else arg
end
end
|
#pipe_to(&block) ⇒ Object
32
33
34
35
|
# File 'lib/brash.rb', line 32
def pipe_to(&block)
command = Brash.build(&block)
@args += " | " + command.to_bash
end
|
#to_bash ⇒ Object
58
59
60
61
62
63
|
# File 'lib/brash.rb', line 58
def to_bash
bash = @command.to_s
bash += " " + @args unless @args == ""
bash += " " + @compound_command.to_bash if @compound_command
escape(bash)
end
|