Class: ActiveRecordGraphExtractor::Importer
- Inherits:
-
Object
- Object
- ActiveRecordGraphExtractor::Importer
- Defined in:
- lib/activerecord_graph_extractor/importer.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #import(data, options = {}) ⇒ Object
- #import_from_file(file_path, options = {}) ⇒ Object
-
#initialize(config = ActiveRecordGraphExtractor.configuration) ⇒ Importer
constructor
A new instance of Importer.
Constructor Details
#initialize(config = ActiveRecordGraphExtractor.configuration) ⇒ Importer
Returns a new instance of Importer.
9 10 11 |
# File 'lib/activerecord_graph_extractor/importer.rb', line 9 def initialize(config = ActiveRecordGraphExtractor.configuration) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/activerecord_graph_extractor/importer.rb', line 7 def config @config end |
Instance Method Details
#import(data, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/activerecord_graph_extractor/importer.rb', line 13 def import(data, = {}) validate_data_structure!(data) records = data['records'] raise ImportError, "No records found in data" if records.empty? start_time = Time.now pk_mapper = PrimaryKeyMapper.new(config.primary_key_strategy) begin imported_count = 0 skipped_count = 0 errors = [] use_transaction = [:transaction] || config.use_transactions batch_size = [:batch_size] || 1000 skip_existing = [:skip_existing] || false custom_finders = [:custom_finders] || {} if use_transaction ActiveRecord::Base.transaction do imported_count, skipped_count, errors = import_records_in_order( records, pk_mapper, skip_existing, custom_finders, batch_size ) end else imported_count, skipped_count, errors = import_records_in_order( records, pk_mapper, skip_existing, custom_finders, batch_size ) end import_duration = Time.now - start_time { 'metadata' => (start_time, imported_count, skipped_count, errors, import_duration, data['records'].size), 'imported_records' => imported_count, 'skipped_records' => skipped_count, 'errors' => errors, 'primary_key_mappings' => pk_mapper.get_all_mappings } rescue StandardError => e raise ImportError, "Failed to import records: #{e.}" end end |
#import_from_file(file_path, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/activerecord_graph_extractor/importer.rb', line 58 def import_from_file(file_path, = {}) unless File.exist?(file_path) raise FileError, "File not found: #{file_path}" end begin file_content = File.read(file_path) data = JSON.parse(file_content) import(data, ) rescue JSON::ParserError => e raise JSONError, "Invalid JSON in file #{file_path}: #{e.}" rescue => e raise FileError, "Error reading file #{file_path}: #{e.}" end end |