Class: Edgestitch::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/edgestitch/exporter.rb

Overview

This class is responsible for exporting an engine’s owned tables and migrations to a SQL file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Exporter

Returns a new instance of Exporter.



21
22
23
24
25
26
# File 'lib/edgestitch/exporter.rb', line 21

def initialize(engine)
  @engine = engine
  @database_directory_path = engine.root.join("db")
  @extra_tables_path = database_directory_path.join("extra_tables")
  @structure_file_path = database_directory_path.join("structure-self.sql")
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



19
20
21
# File 'lib/edgestitch/exporter.rb', line 19

def engine
  @engine
end

Class Method Details

.export(engine, dump) ⇒ Object

Exports an engine using a dump helper (@see Edgestitch::Mysql::Dump)

Parameters:

  • engine (Class<Rails::Engine>)

    the engine to export

  • dump (<#export_tables,#export_migrations>)

    the dump helper



15
16
17
# File 'lib/edgestitch/exporter.rb', line 15

def self.export(engine, dump)
  new(engine).export(dump)
end

Instance Method Details

#export(dump, to: structure_file_path) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/edgestitch/exporter.rb', line 28

def export(dump, to: structure_file_path)
  StringIO.open do |buffer|
    buffer.puts dump.export_tables(tables)
    buffer.puts
    buffer.puts dump.export_migrations(migrations)
    File.write to, "#{buffer.string.strip}\n"
  end
end

#migrationsObject



37
38
39
40
41
42
43
44
# File 'lib/edgestitch/exporter.rb', line 37

def migrations
  @migrations ||= begin
    migrations_glob = database_directory_path.join("{migrate,migrate.archive}/*.rb")
    Dir[migrations_glob]
      .map { |filename| File.basename(filename).to_i }
      .sort
  end
end

#tablesObject



46
47
48
# File 'lib/edgestitch/exporter.rb', line 46

def tables
  component_tables + extra_tables
end