Class: Squasher::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/squasher/config.rb

Defined Under Namespace

Modules: Render

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



39
40
41
42
43
44
45
# File 'lib/squasher/config.rb', line 39

def initialize
  @root_path = Dir.pwd.freeze
  @migrations_folder = File.join(@root_path, 'db', 'migrate')
  @flags = []
  @databases = []
  set_app_path(@root_path)
end

Instance Attribute Details

#migration_versionObject (readonly)

Returns the value of attribute migration_version.



37
38
39
# File 'lib/squasher/config.rb', line 37

def migration_version
  @migration_version
end

#schema_fileObject (readonly)

Returns the value of attribute schema_file.



37
38
39
# File 'lib/squasher/config.rb', line 37

def schema_file
  @schema_file
end

Instance Method Details

#dbconfig?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/squasher/config.rb', line 88

def dbconfig?
  !dbconfig.nil?
end

#in_app_root(&block) ⇒ Object



112
113
114
# File 'lib/squasher/config.rb', line 112

def in_app_root(&block)
  Dir.chdir(@app_path, &block)
end

#migration_file(timestamp, migration_name) ⇒ Object



80
81
82
# File 'lib/squasher/config.rb', line 80

def migration_file(timestamp, migration_name)
  File.join(migrations_folder, "#{ timestamp }_#{ migration_name }.rb")
end

#migration_filesObject



76
77
78
# File 'lib/squasher/config.rb', line 76

def migration_files
  Dir.glob(File.join(migrations_folder, '**.rb'))
end

#migrations_folder?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/squasher/config.rb', line 84

def migrations_folder?
  Dir.exist?(migrations_folder)
end

#set(key, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/squasher/config.rb', line 47

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
  elsif key == :databases
    @databases = value
  else
    @flags << key
  end
end

#set?(k) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/squasher/config.rb', line 72

def set?(k)
  @flags.include?(k)
end

#stub_dbconfigObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/squasher/config.rb', line 92

def stub_dbconfig
  return unless dbconfig?

  list = [dbconfig_file, schema_file]
  list.each do |file|
    next unless File.exist?(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.exist?("#{ file }.sq")
    FileUtils.mv "#{ file }.sq", file
  end
end