module Onotole::Helpers

Public Instance Methods

add_gems_from_args() click to toggle source
# File lib/onotole/helpers.rb, line 60
def add_gems_from_args
  ARGV.each do |gem_name|
    next unless gem_name.slice(0, 2) == '--'
    add_to_user_choise gem_name[2..-1].to_sym
  end
end
add_to_user_choise(gem) click to toggle source
# File lib/onotole/helpers.rb, line 113
def add_to_user_choise(gem)
  AppBuilder.user_choice.push gem
end
br(str = nil) click to toggle source
# File lib/onotole/helpers.rb, line 150
def br(str = nil)
  str ? "\n" + str : "\n"
end
choice(selector, variants) click to toggle source
# File lib/onotole/helpers.rb, line 10
def choice(selector, variants)
  return if variant_in_options? variants
  values = variants.keys
  say "\n  #{BOLDGREEN}#{selector}#{COLOR_OFF}"
  variants.each_with_index do |variant, i|
    say "#{i.to_s.rjust(5)}. #{BOLDBLUE}#{variant[1]}#{COLOR_OFF}"
  end
  numeric_answers = (0...variants.length).map(&:to_s)
  answer = ask_stylish('Enter choice:') until numeric_answers.include? answer
  symbol_answer = values[answer.to_i]
  symbol_answer == :none ? nil : symbol_answer
end
clean_by_rubocop() click to toggle source
# File lib/onotole/helpers.rb, line 125
def clean_by_rubocop
  return unless user_choose?(:rubocop)
  bundle_command "exec rubocop --auto-correct #{quiet_suffix}"
end
cleanup_comments(file_name) click to toggle source
# File lib/onotole/helpers.rb, line 67
def cleanup_comments(file_name)
  accepted_content = File.readlines(file_name).reject do |line|
    line =~ /^\s*#.*$/ || line =~ /^$\n/
  end

  File.open(file_name, 'w') do |file|
    accepted_content.each { |line| file.puts line }
  end
end
cover_def_by(file_name, lookup_str, external_def) click to toggle source

does not recognize variable nesting, but now it does not matter

# File lib/onotole/helpers.rb, line 78
def cover_def_by(file_name, lookup_str, external_def)
  expect_end = 0
  found = false
  accepted_content = ''
  File.readlines(file_name).each do |line|
    expect_end += 1 if found && line =~ /\sdo\s/
    expect_end -= 1 if found && line =~ /(\s+end|^end)/
    if line =~ Regexp.new(lookup_str)
      accepted_content += "#{external_def}\n#{line}"
      expect_end += 1
      found = true
    else
      accepted_content += line
    end
    if found && expect_end == 0
      accepted_content += "\nend"
      found = false
    end
  end
  File.open(file_name, 'w') { |file| file.puts accepted_content }
end
heroku_adapter() click to toggle source
# File lib/onotole/helpers.rb, line 52
def heroku_adapter
  @heroku_adapter ||= Adapters::Heroku.new(self)
end
install_from_github(_gem_name) click to toggle source
# File lib/onotole/helpers.rb, line 100
def install_from_github(_gem_name)
  # TODO: in case of bundler update `bundle show` do now work any more
  true

  # return nil unless gem_name
  # path = `cd #{Dir.pwd} && bundle show #{gem_name}`.chomp
  # run "cd #{path} && bundle exec gem build #{gem_name}.gemspec && bundle exec gem install *.gem"
end
mkdir_and_touch(dir) click to toggle source
# File lib/onotole/helpers.rb, line 154
def mkdir_and_touch(dir)
  run "mkdir #{dir}"
  touch "#{dir}/.keep"
end
multiple_choice(selector, variants) click to toggle source
# File lib/onotole/helpers.rb, line 27
def multiple_choice(selector, variants)
  values = []
  result = []
  answers = ''
  say "\n  #{BOLDGREEN}#{selector} Use space as separator#{COLOR_OFF}"
  variants.each_with_index do |variant, i|
    values.push variant[0]
    say "#{i.to_s.rjust(5)}. #{BOLDBLUE}#{variant[0]
          .to_s.ljust(20)}-#{COLOR_OFF} #{variant[1]}"
  end
  loop do
    answers = ask_stylish('Enter choices:').split ' '
    break if answers.any? && (answers - (0...variants.length)
            .to_a.map(&:to_s)).empty?
  end
  answers.delete '0'
  answers.uniq.each { |answer| result.push values[answer.to_i] }
  result
end
pgsql_db_exist?(db_name) click to toggle source
# File lib/onotole/helpers.rb, line 121
def pgsql_db_exist?(db_name)
  system "psql -l | grep #{db_name}"
end
quiet_suffix() click to toggle source
# File lib/onotole/helpers.rb, line 130
def quiet_suffix
  AppBuilder.quiet ? ' > /dev/null' : ''
end
rails_generator(command) click to toggle source
# File lib/onotole/helpers.rb, line 117
def rails_generator(command)
  bundle_command "exec rails generate #{command} -f #{quiet_suffix}"
end
raise_on_missing_translations_in(environment) click to toggle source
# File lib/onotole/helpers.rb, line 47
def raise_on_missing_translations_in(environment)
  config = 'config.action_view.raise_on_missing_translations = true'
  uncomment_lines("config/environments/#{environment}.rb", config)
end
serve_static_files_line() click to toggle source
# File lib/onotole/helpers.rb, line 56
def serve_static_files_line
  "config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?\n"
end
touch(file_name) click to toggle source
# File lib/onotole/helpers.rb, line 146
def touch(file_name)
  `touch #{file_name}`
end
user_choose?(gem) click to toggle source
# File lib/onotole/helpers.rb, line 109
def user_choose?(gem)
  AppBuilder.user_choice.include? gem
end
user_gems_from_args_or_default_set() click to toggle source
# File lib/onotole/helpers.rb, line 134
def user_gems_from_args_or_default_set
  gems_flags = []
  options.each { |gem, usage| gems_flags.push(gem.to_sym) if usage }
  gems = GEMPROCLIST & gems_flags
  if gems.empty?
    AppBuilder.user_choice = DEFAULT_GEMSET
  else
    gems.each { |gem| AppBuilder.user_choice << gem }
  end
  add_user_gems
end
variant_in_options?(variants) click to toggle source
# File lib/onotole/helpers.rb, line 23
def variant_in_options?(variants)
  variants.keys[1..-1].map { |a| options[a] }.include? true
end
yes_no_question(gem_name, gem_description) click to toggle source
# File lib/onotole/helpers.rb, line 4
def yes_no_question(gem_name, gem_description)
  gem_name_color = "#{gem_name.capitalize}.\n"
  variants = { none: 'No', gem_name.to_sym => gem_name_color }
  choice "Use #{gem_name}? #{gem_description}", variants
end