Module: Venomi::Generators::Utils::InstanceMethods
- Included in:
- InstallGenerator, RailsAdmin
- Defined in:
- lib/generators/utils.rb
Constant Summary collapse
- @@gemfile_path =
"#{Rails.root.to_s}/Gemfile"
Instance Method Summary collapse
- #file?(path) ⇒ Boolean
- #file_include?(path, include) ⇒ Boolean
- #install_gem(gem_name, version = nil) ⇒ Object
- #libraries_available?(*gems) ⇒ Boolean
- #library_available?(gem_name) ⇒ Boolean
- #replace(path, pattern, new_line) ⇒ Object
- #yes_no(question) ⇒ Object
Instance Method Details
#file?(path) ⇒ Boolean
42 43 44 |
# File 'lib/generators/utils.rb', line 42 def file?(path) File.exist?(path) end |
#file_include?(path, include) ⇒ Boolean
68 69 70 71 72 73 74 75 |
# 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 |
#install_gem(gem_name, version = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# 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) ⇒ Boolean
34 35 36 37 38 39 40 |
# 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) ⇒ Boolean
25 26 27 28 29 30 31 32 |
# 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) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# 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) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 |