Module: I18nBackendMongoid::Generators::Utils::InstanceMethods

Included in:
InstallGenerator
Defined in:
lib/generators/utils.rb

Constant Summary collapse

@@gemfile_path =
"#{Rails.root}/Gemfile"

Instance Method Summary collapse

Instance Method Details

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/generators/utils.rb', line 40

def file?(path)
  File.exist?(path)
end

#file_include?(path, include) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/generators/utils.rb', line 66

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/utils.rb', line 44

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

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/generators/utils.rb', line 32

def libraries_available?(*gems)
  is_available = true
  gems.each do |gem|
    return false unless library_available?(gem)
  end
  is_available
end

#library_available?(gem_name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/generators/utils.rb', line 25

def library_available?(gem_name)
  require gem_name
  return true
rescue LoadError
  return false
end

#replace(path, pattern, new_line) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/utils.rb', line 55

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)
  is_valid = true
  question += ' [Y/N] '
  while is_valid
    answer = ask(question, :yellow) do |yn|
      yn.limit = 1, yn.validate = /[yn]/i
    end
    answer.downcase!
    is_valid = (answer == 'y' || answer == 'n') ? false : true
  end
  answer
end