Class: ActiveRecordJsonImporter::Importer

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

Constant Summary collapse

RECORDS_AT_ONCE =
5000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Importer

Returns a new instance of Importer.



12
13
14
15
16
17
# File 'lib/active_record_json_importer.rb', line 12

def initialize(options = {})
	@options = options
	initialize_active_record_class
	initialize_bulk_input_file
	initialize_records_at_once
end

Instance Attribute Details

#active_record_classObject (readonly)

Returns the value of attribute active_record_class.



7
8
9
# File 'lib/active_record_json_importer.rb', line 7

def active_record_class
  @active_record_class
end

#bulk_input_fileObject (readonly)

Returns the value of attribute bulk_input_file.



7
8
9
# File 'lib/active_record_json_importer.rb', line 7

def bulk_input_file
  @bulk_input_file
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/active_record_json_importer.rb', line 7

def options
  @options
end

#records_at_onceObject

Returns the value of attribute records_at_once.



8
9
10
# File 'lib/active_record_json_importer.rb', line 8

def records_at_once
  @records_at_once
end

Instance Method Details

#importObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record_json_importer.rb', line 19

def import
	records = []
	bulk_input_file.each_with_index do |line, index|
		records << JSON.parse(line)
		if (index % records_at_once == 0)
			import_records records
			records = []
		end
	end
	bulk_input_file.close
	import_records records if records.any?
	return true
end