lib/rye/box.rb in rye-0.8.4 vs lib/rye/box.rb in rye-0.8.5
- old
+ new
@@ -46,10 +46,11 @@
def stash=(val); @rye_stash = val; end
def nickname=(val); @rye_nickname = val; end
def enable_safe_mode; @rye_safe = true; end
def disable_safe_mode; @rye_safe = false; end
+ def safe?; @rye_safe == true; end
def enable_quiet_mode; @rye_quiet = true; end
def disable_quiet_mode; @rye_quiet = false; end
# The most recent value from Box.cd or Box.[]
@@ -251,21 +252,21 @@
# puts rbox.getenv['HOME'] # => "/home/gloria" (remote)
#
# NOTE: This method should not raise an exception under normal
# circumstances.
#
- def getenv
+ def getenv(key=nil)
if @rye_getenv && @rye_getenv.empty? && self.can?(:env)
vars = self.quietly { env } rescue []
vars.each do |nvpair|
# Parse "GLORIA_HOME=/gloria/lives/here" into a name/value
# pair. The regexp ensures we split only at the 1st = sign
n, v = nvpair.scan(/\A([\w_-]+?)=(.+)\z/).flatten
@rye_getenv[n] = v
end
end
- @rye_getenv
+ key.nil? ? @rye_getenv : @rye_getenv[key.to_s]
end
# Add an environment variable. +n+ and +v+ are the name and value.
# Returns the instance of Rye::Box
def setenv(n, v)
@@ -427,16 +428,26 @@
added_keys
end
# A handler for undefined commands.
# Raises Rye::CommandNotFound exception.
- def method_missing(meth, *args, &block)
- ex = Rye::CommandNotFound.new(meth.to_s)
- raise ex unless @rye_exception_hook.has_key? ex.class
- @rye_exception_hook[Rye::CommandNotFound].call ex
+ def method_missing(cmd, *args, &block)
+ if @rye_safe
+ ex = Rye::CommandNotFound.new(cmd.to_s)
+ raise ex unless @rye_exception_hook.has_key? ex.class
+ @rye_exception_hook[Rye::CommandNotFound].call ex
+ else
+ if block.nil?
+ run_command cmd, *args
+ else
+ ex = Rye::CommandNotFound.new(cmd.to_s)
+ raise ex unless @rye_exception_hook.has_key? ex.class
+ end
+ end
end
-
+ alias :execute :method_missing
+
# Returns the command an arguments as a String.
def preview_command(*args)
prep_args(*args).join(' ')
end
@@ -482,11 +493,18 @@
# uname :a
# end
# OR
# rbox.batch(&block)
#
+ # The batch can also accept arguments.
#
+ # rbox.batch('path/2/file') do |file|
+ # ls :l file
+ # end
+ #
+ # Returns the return value of the block.
+ #
def batch(*args, &block)
self.instance_exec *args, &block
end
# Like batch, except it disables safe mode before executing the block.
@@ -734,10 +752,10 @@
if choice == :retry
retry
elsif choice == :skip
# do nothing
else
- raise ex
+ raise ex, ex.message
end
end
if !@rye_quiet && @rye_post_command_hook.is_a?(Proc)
@rye_post_command_hook.call(rap)