module Venomi::Generators::Utils::InstanceMethods
Public Instance Methods
file?(path)
click to toggle source
# File lib/generators/utils.rb, line 42 def file?(path) File.exist?(path) end
file_include?(path, include)
click to toggle source
# File lib/generators/utils.rb, line 68 def file_include?(path, include) File.open(path, 'r') do |f| f.each_line do |line| return true if line.include? include end end false end end
install_gem(gem_name, version = nil)
click to toggle source
# File lib/generators/utils.rb, line 46 def install_gem(gem_name, version = nil) gem = "gem '" + gem_configurename + "'" gem += ( ", '~> " + version +"'") if version unless file_include?(@@gemfile_path, gem) open(@@gemfile_path, 'a') { |f| f.puts gem } end system 'bundle install' end
libraries_available?(*gems)
click to toggle source
# File lib/generators/utils.rb, line 34 def libraries_available?(*gems) isAvailable = true gems.each do |gem| return false unless library_available?(gem) end isAvailable end
library_available?(gem_name)
click to toggle source
# File lib/generators/utils.rb, line 25 def library_available?(gem_name) begin require gem_name return true rescue LoadError return false end end
replace(path, pattern, new_line)
click to toggle source
# File lib/generators/utils.rb, line 57 def replace(path, pattern, new_line) t_file = Tempfile.new('temp.rb') File.open(path, 'r') do |f| f.each_line do |line| t_file.puts (line.include? pattern)? new_line : line end end t_file.close FileUtils.mv(t_file.path, path) end
yes_no(question)
click to toggle source
# File lib/generators/utils.rb, line 12 def yes_no(question) isValid = true question += " [Y/N] " while isValid answer = ask(question, :yellow) do |yn| yn.limit = 1, yn.validate = /[yn]/i end answer.downcase! isValid = (answer == "y" or answer == "n")? false : true end answer end