Class: ActiveRecordDataLoader::FileOutputAdapter
- Inherits:
-
Object
- Object
- ActiveRecordDataLoader::FileOutputAdapter
- Defined in:
- lib/active_record_data_loader/file_output_adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #copy(table:, columns:, data:, row_numbers:) ⇒ Object
-
#initialize(options) ⇒ FileOutputAdapter
constructor
A new instance of FileOutputAdapter.
- #insert(command) ⇒ Object
- #write_command(command) ⇒ Object
Constructor Details
#initialize(options) ⇒ FileOutputAdapter
Returns a new instance of FileOutputAdapter.
14 15 16 17 18 19 |
# File 'lib/active_record_data_loader/file_output_adapter.rb', line 14 def initialize() @filename = .fetch(:filename, "active_record_data_loader_script.sql") @file_basename = File.basename(@filename, File.extname(@filename)) @path = File.(File.dirname(@filename)) File.open(@filename, File::TRUNC) if File.exist?(@filename) end |
Class Method Details
.with_output_options(options) {|adapter| ... } ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/active_record_data_loader/file_output_adapter.rb', line 5 def self.() adapter = new() pre_command = [:pre_command] adapter.write_command(pre_command) if pre_command yield adapter post_command = [:post_command] adapter.write_command(post_command) if post_command end |
Instance Method Details
#copy(table:, columns:, data:, row_numbers:) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/active_record_data_loader/file_output_adapter.rb', line 21 def copy(table:, columns:, data:, row_numbers:) data_filename = data_filename(table, row_numbers) File.open(data_filename, "w") { |f| f.puts(data) } File.open(filename, "a") do |file| file.puts("\\COPY #{table} (#{columns}) FROM '#{data_filename}' WITH (FORMAT CSV);") end end |
#insert(command) ⇒ Object
29 30 31 |
# File 'lib/active_record_data_loader/file_output_adapter.rb', line 29 def insert(command) write_command(command) end |
#write_command(command) ⇒ Object
33 34 35 |
# File 'lib/active_record_data_loader/file_output_adapter.rb', line 33 def write_command(command) File.open(filename, "a") { |f| f.puts("#{command.gsub("\n", ' ')};") } end |