module RailsBase::FileManipulation

This module provides help to file manipulation

Public Instance Methods

append_to_file(file_name, from) click to toggle source
# File lib/rs-rails-base/file_manipulation.rb, line 10
def append_to_file(file_name, from)
  from_file = File.expand_path("#{__dir__}/#{from}")
  TTY::File.append_to_file(file_name, read_all_content(from_file))
end
create_file(file_name, from) click to toggle source
# File lib/rs-rails-base/file_manipulation.rb, line 15
def create_file(file_name, from)
  from_file = File.expand_path("#{__dir__}/#{from}")
  TTY::File.create_file(file_name, read_all_content(from_file))
end
inject_into_file(file_name, after, from, extra_line_flag = false) click to toggle source
# File lib/rs-rails-base/file_manipulation.rb, line 20
def inject_into_file(file_name, after, from, extra_line_flag = false)
  from_file = File.expand_path("#{__dir__}/#{from}")
  content = read_all_content(from_file)
  content += "\n" if extra_line_flag
  begin
    TTY::File.inject_into_file(file_name, content, after: "#{after}\n")
  rescue StandardError => ex
    say_something('Make sure you are on the root of your project please')
    puts ex
  end
end
install_gem(name, version) click to toggle source
# File lib/rs-rails-base/file_manipulation.rb, line 43
def install_gem(name, version)
  if File.exist?('Gemfile')
    unless read_all_content('Gemfile').include? name
      gem_line = "gem '#{name}', '~> #{version}' \n"
      TTY::File.append_to_file('Gemfile', gem_line)
      system('bundle install')
    end
  else
    say_something('Please go to your root folder :)')
  end
end
read_all_content(file_name) click to toggle source
# File lib/rs-rails-base/file_manipulation.rb, line 36
def read_all_content(file_name)
  file = File.open(file_name, 'rb')
  content = file.read
  file.close
  content
end
replace_in_file(file_name, old_content, new_content) click to toggle source
# File lib/rs-rails-base/file_manipulation.rb, line 32
def replace_in_file(file_name, old_content, new_content)
  TTY::File.replace_in_file(file_name, old_content, new_content)
end