class TryRb::CLI
Public Instance Methods
conf()
click to toggle source
# File lib/tryrb/cli.rb, line 9 def conf TryRb::Config.instance end
config()
click to toggle source
# File lib/tryrb/cli.rb, line 31 def config default_tmp_path = "~/tmp/tryrb" tmp_dir = ask("Please specify your dir of the tmp files(default: #{default_tmp_path}):") tmp_dir = default_tmp_path if tmp_dir.empty? default_editor = "vim" editor = ask("Please specify your favorite editor(default: #{default_editor}):") editor = default_editor if editor.empty? config = {'tmp_dir' => tmp_dir, 'editor' => editor} rc_path = conf.expanded_rc_path File.open(rc_path, 'w') do |f| f.write(config.to_yaml) end say("The config have been writen to ~/.tryrbrc", :green) end
create(key_name="")
click to toggle source
# File lib/tryrb/cli.rb, line 15 def create(key_name="") @key_name = key_name system([conf.editor, fullpath] * ' ') end
exec(filename=nil)
click to toggle source
# File lib/tryrb/cli.rb, line 23 def exec(filename=nil) file_path = find_file(:filename => filename, :last_n => options[:last]) abort "Can't find the file you want" unless file_path system(['ruby', file_path] * ' ') end
Private Instance Methods
filename_extname()
click to toggle source
# File lib/tryrb/cli.rb, line 74 def filename_extname '.rb' end
filename_prefix()
click to toggle source
# File lib/tryrb/cli.rb, line 70 def filename_prefix Time.now.strftime("%Y%m%d%H%M%S") end
find_file(options={})
click to toggle source
# File lib/tryrb/cli.rb, line 52 def find_file(options={}) filename = options[:filename] last_n = options[:last_n] || 1 names = Dir[File.join(conf.expanded_tmp_dir, '*.rb')] .select { |n| n =~ /\d{14}/ } names = names.select { |n| n =~ /#{filename}\.rb$/ } if filename names[-last_n] end
fullpath()
click to toggle source
# File lib/tryrb/cli.rb, line 61 def fullpath filename = get_filename expanded_path = conf.expanded_tmp_dir FileUtils.mkdir_p expanded_path unless Dir.exists?(expanded_path) File.join conf.tmp_dir, filename rescue Errno::EEXIST => e abort "File #{e.message.split[-1]} exists. The tmp directory can't be created! Please delete the file first." end
get_filename()
click to toggle source
# File lib/tryrb/cli.rb, line 78 def get_filename parts = [filename_prefix] parts << @key_name.gsub(/\.rb$/, '') unless @key_name.empty? parts * '_' + filename_extname end