Class: ASpaceImport::RecordBatch
- Inherits:
-
Object
- Object
- ASpaceImport::RecordBatch
- Defined in:
- backend/app/converters/lib/parse_queue.rb
Overview
Manages the JSON object batch set
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
-
#each_open_file_path {|@working_file.path| ... } ⇒ Object
-
#flush ⇒ Object
-
#flush_last ⇒ Object
This URI check stops regular JSONModels from going through.
-
#get_output_path ⇒ Object
-
#initialize(opts = {}) ⇒ RecordBatch
constructor
A new instance of RecordBatch.
-
#record_filter=(predicate) ⇒ Object
-
#working_area ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ RecordBatch
Returns a new instance of RecordBatch.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'backend/app/converters/lib/parse_queue.rb', line 11 def initialize(opts = {}) opts.each do |k, v| instance_variable_set("@#{k}", v) end @must_be_unique = ['subject'] @record_filter = ->(record) { true } @uri_remapping = {} @seen_records = {} @working_file = opts[:working_file] || ASUtils.tempfile("import_batch_working_file") @working_area = [] end |
Class Method Details
.dedupe_subrecords(obj) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'backend/app/converters/lib/parse_queue.rb', line 28 def self.dedupe_subrecords(obj) ASpaceImport::FIELDS_TO_DEDUPE.each do |subrecord| if obj.respond_to?(subrecord) && obj.send(subrecord).is_a?(Array) hashes = [] obj.send(subrecord).map! { |json| hash = json.to_hash.hash if hashes.include?(hash) nil else hashes << hash json end } obj.send(subrecord).compact! end end end |
Instance Method Details
#<<(obj) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'backend/app/converters/lib/parse_queue.rb', line 54 def <<(obj) self.class.dedupe_subrecords(obj) raise "Imported object can't be nil!" unless obj # If the record's JSON schema contains a URI (i.e. this is a top-level # record), then blow up if it isn't provided. if obj.class.is_a?(JSONModelType) && obj.class.schema['uri'] && !obj.uri Log.debug("Can't import object: #{obj.inspect}") raise "Imported object must have a URI!" end @working_area.push(obj) end |
#each_open_file_path {|@working_file.path| ... } ⇒ Object
94 95 96 97 |
# File 'backend/app/converters/lib/parse_queue.rb', line 94 def each_open_file_path yield @working_file.path if @working_file && @working_file.path yield @batch_file.path if @batch_file && @batch_file.path end |
#flush ⇒ Object
70 71 72 73 74 |
# File 'backend/app/converters/lib/parse_queue.rb', line 70 def flush while !@working_area.empty? flush_last end end |
#flush_last ⇒ Object
This URI check stops regular JSONModels from going through. JSONModelWrap is what puts that hereā¦
79 80 81 82 83 84 85 |
# File 'backend/app/converters/lib/parse_queue.rb', line 79 def flush_last last = @working_area.pop if last.class.method_defined? :uri and !last.uri.nil? _push(last) end end |
#get_output_path ⇒ Object
88 89 90 91 |
# File 'backend/app/converters/lib/parse_queue.rb', line 88 def get_output_path close @batch_file.path end |
#record_filter=(predicate) ⇒ Object
100 101 102 |
# File 'backend/app/converters/lib/parse_queue.rb', line 100 def record_filter=(predicate) @record_filter = predicate end |
#working_area ⇒ Object
49 50 51 |
# File 'backend/app/converters/lib/parse_queue.rb', line 49 def working_area @working_area end |