class Sequelizer::GemfileModifier

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 7
def initialize(options = {})
  @options = options
end

Public Instance Methods

modify() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 11
def modify
  check_for_gemfile
  if gemfile_needs_modification?
    modify_gemfile
    run_bundle unless options['skip-bundle']
  else
    puts "Gemfile needs no modification"
  end
end

Private Instance Methods

check_for_gemfile() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 74
def check_for_gemfile
  return if gemfile.exist?
  raise "Could not find Gemfile in current directory: #{Pathname.pwd}"
end
full_gem_line() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 58
def full_gem_line
  [gem_line, gem_line_comment].join(' ')
end
gem_line() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 50
def gem_line
  "gem '#{proper_gem}'"
end
gem_line_comment() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 54
def gem_line_comment
  '# ADDED BY SEQUELIZER'
end
gemfile() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 84
def gemfile
  @gemfile ||= Pathname.new('Gemfile')
end
gemfile_lines() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 66
def gemfile_lines
  @gemfile_lines ||= File.readlines(gemfile).map(&:chomp)
end
gemfile_needs_modification?() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 62
def gemfile_needs_modification?
  !(gemfile_lines.include?(gem_line) || gemfile_lines.include?(full_gem_line))
end
modified_lines() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 70
def modified_lines
  gemfile_lines.select { |l| l !~ Regexp.new(gem_line_comment) } + [full_gem_line]
end
modify_gemfile() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 23
def modify_gemfile
  puts %Q|Adding "#{gem_line}" to Gemfile|
  return if options['dry-run']

  File.write(gemfile, modified_lines.join("\n"))
end
proper_gem() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 30
def proper_gem
  opts = Options.new
  @proper_gem ||= case opts.adapter
  when 'postgres'
    'pg'
  when 'sqlite'
    'sqlite3'
  when 'mysql'
    'mysql2'
  when 'tinytds'
    'tiny_tds'
  when 'oracle'
    'ruby-oci8'
  when nil
    raise "No database adapter defined in your Sequelizer configuration"
  else
    raise "Don't know which database gem to use with adapter: #{opts.adapter}"
  end
end
run_bundle() click to toggle source
# File lib/sequelizer/gemfile_modifier.rb, line 79
def run_bundle
  puts "Running `bundle install` to update dependencies"
  system('bundle install')
end