class Squasher::Config

Attributes

dbconfig_file[R]
migration_version[R]
migrations_folder[R]
schema_file[R]

Public Class Methods

new() click to toggle source
# File lib/squasher/config.rb, line 38
def initialize
  @root_path = Dir.pwd.freeze
  @migrations_folder = File.join(@root_path, 'db', 'migrate')
  @flags = []
  set_app_path(@root_path)
end

Public Instance Methods

dbconfig?() click to toggle source
# File lib/squasher/config.rb, line 84
def dbconfig?
  !dbconfig.nil?
end
in_app_root(&block) click to toggle source
# File lib/squasher/config.rb, line 108
def in_app_root(&block)
  Dir.chdir(@app_path, &block)
end
migration_file(timestamp, migration_name) click to toggle source
# File lib/squasher/config.rb, line 76
def migration_file(timestamp, migration_name)
  File.join(migrations_folder, "#{ timestamp }_#{ migration_name }.rb")
end
migration_files() click to toggle source
# File lib/squasher/config.rb, line 72
def migration_files
  Dir.glob(File.join(migrations_folder, '**.rb'))
end
migrations_folder?() click to toggle source
# File lib/squasher/config.rb, line 80
def migrations_folder?
  Dir.exists?(migrations_folder)
end
set(key, value) click to toggle source
# File lib/squasher/config.rb, line 45
def set(key, value)
  if key == :engine
    base = value.nil? ? @root_path : File.expand_path(value, @root_path)
    list = Dir.glob(File.join(base, '**', '*', 'config', 'application.rb'))
    case list.size
    when 1
      set_app_path(File.expand_path('../..', list.first))
    when 0
      Squasher.error(:cannot_find_dummy, base: base)
    else
      Squasher.error(:multi_dummy_case, base: base)
    end
  elsif key == :migration
    Squasher.error(:invalid_migration_version, value: value) unless value.to_s =~ /\A\d.\d\z/
    @migration_version = "[#{value}]"
  elsif key == :sql
    @schema_file = File.join(@app_path, 'db', 'structure.sql')
    @flags << key
  else
    @flags << key
  end
end
set?(k) click to toggle source
# File lib/squasher/config.rb, line 68
def set?(k)
  @flags.include?(k)
end
stub_dbconfig() { || ... } click to toggle source
# File lib/squasher/config.rb, line 88
def stub_dbconfig
  return unless dbconfig?

  list = [dbconfig_file, schema_file]
  list.each do |file|
    next unless File.exists?(file)
    FileUtils.mv file, "#{ file }.sq"
  end

  File.open(dbconfig_file, 'wb') { |stream| stream.write dbconfig.to_yaml }

  yield

ensure
  list.each do |file|
    next unless File.exists?("#{ file }.sq")
    FileUtils.mv "#{ file }.sq", file
  end
end

Private Instance Methods

dbconfig() click to toggle source
# File lib/squasher/config.rb, line 116
def dbconfig
  return @dbconfig if defined?(@dbconfig)
  return @dbconfig = nil unless File.exists?(dbconfig_file)

  @dbconfig = nil

  begin
    content, soft_error = Render.process(dbconfig_file)
    if content.has_key?('development')
      @dbconfig = { 'development' => content['development'].merge('database' => 'squasher') }
    end
  rescue
  end

  if soft_error && @dbconfig
    exit unless Squasher.ask(:use_dbconfig, config: @dbconfig.fetch('development'))
  end
  @dbconfig
end
set_app_path(path) click to toggle source
# File lib/squasher/config.rb, line 136
def set_app_path(path)
  @app_path = path
  @schema_file = File.join(path, 'db', 'schema.rb')
  @dbconfig_file = File.join(path, 'config', 'database.yml')
end