lib/sxmrb.rb in sxmrb-0.1.1 vs lib/sxmrb.rb in sxmrb-0.2.0
- old
+ new
@@ -6,13 +6,24 @@
require 'shellwords'
require 'singleton'
require 'forwardable'
+begin
+ require 'backports/3.0.0/symbol'
+rescue LoadError
+ unless Symbol.method_defined? :name
+ # Symbol#name hack
+ class Symbol
+ alias name to_s
+ end
+ end
+end
+
# SXMO user scripts in ruby.
class Sxmrb
- include Singleton
+ include ::Singleton
# Normally when using Singleton there is no way to pass arguments
# when instantiating. The renew hack below allows the singleton
# instance to be re-instantiated and can pass arguments at that time;
# this was added as an experiment to assist with testing.
@@ -47,58 +58,26 @@
cmd << " -p #{prompt.inspect}" if prompt
}
end
- # Run the awk command.
- #
- # @example
- # print(sh('ls -l') | awk('{print $1}').to_s)
- def awk(arg = '')
- sh("awk -e #{Shellwords.escape arg.chomp}")
- end
-
- # Run the cat command.
- #
- # @example
- # print cat('/etc/passwd').to_s
- def cat(arg = '')
- sh("cat #{arg.chomp}")
- end
-
- # Run the column command.
- #
- # @example
- # print(sh('ls -l') | column('-t').to_s)
- def column(arg = '')
- sh("column #{arg.chomp}")
- end
-
- # Run the shell echo command.
- #
- # @example
- # print echo('Hello World').to_s
- def echo(arg = '')
- sh("echo #{Shellwords.escape arg.chomp}")
- end
-
- # Run the tac command.
- #
- # @example
- # print tac('/etc/passwd').to_s
- def tac(arg = '')
- sh("tac #{arg.chomp}")
- end
-
# Use a blank #menu to prompt for a value.
#
# @example
# value = input(prompt: 'Amount?').to_i
def input(prompt:)
(echo | menu(prompt: prompt)).to_s.chomp
end
+ def method_missing(cmd, *args, &_block)
+ sh(*([cmd.name] + args))
+ end
+
+ def respond_to_missing?(cmd, include_private = false)
+ sh('which', cmd.name).empty? ? super : true
+ end
+
class <<self
# This evil hack experiment is intended to allow swapping out the
# Shell instance when testing.
def renew_instance(...) # :nodoc:
@singleton__mutex__.synchronize {
@@ -106,10 +85,10 @@
}
end
# Forward instance methods to the Singleton instance; false here
# excludes ancestor methods.
- extend Forwardable
+ extend ::Forwardable
def_delegators :instance, *Sxmrb.instance_methods(false)
end
class Error < ::StandardError; end
end