lib/ripl/play.rb in ripl-play-0.1.1 vs lib/ripl/play.rb in ripl-play-0.2.0
- old
+ new
@@ -1,9 +1,9 @@
require 'ripl'
module Ripl::Play
- VERSION = '0.1.1'
+ VERSION = '0.2.0'
def before_loop
super
play_back
config[:play_quiet] = false
@@ -46,13 +46,41 @@
rescue SocketError
abort "ripl can't play `#{url}'"
end
def play_back_string(str)
+ Ripl::Play.install_gems(str) if config[:play_install]
str.split("\n").each {|input|
@play_input = input
loop_once
}
+ end
+
+ class << self
+ def install_gems(str)
+ gems = gems_to_install(str)
+ return if gems.empty?
+ print "Can I install the following gems: #{gems.join(', ')} ? ([y]/n)"
+ if $stdin.gets.to_s[/^n/]
+ abort "Please install these gems manually: #{gems.join(' ')}"
+ else
+ system(ENV['GEM'] || 'gem', 'install', *gems)
+ end
+ end
+
+ def gems_to_install(str)
+ gems = str.scan(/require\s*['"]([^'"\s]+)['"]/).flatten
+ gems.reject {|e| requireable(e) }.map {|e|
+ e.include?('/') ? e[/^[^\/]+/] : e
+ }.uniq.map {|e| e[/^active_/] ? e.sub('_', '') : e }
+ end
+
+ def requireable(lib)
+ require lib
+ true
+ rescue LoadError
+ false
+ end
end
end
Ripl::Shell.send :include, Ripl::Play
Ripl.config[:readline] = false