Module: BetterBatch::ActiveRecord::Transform
- Defined in:
- lib/better_batch/active_record/transform.rb
Class Method Summary collapse
- .assert_inputs_ok!(data, unique_by:) ⇒ Object
- .build_return(returning, rows, query) ⇒ Object
- .hash_rows(returning, rows) ⇒ Object
- .slice_upsert(data, except:) ⇒ Object
Class Method Details
.assert_inputs_ok!(data, unique_by:) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/better_batch/active_record/transform.rb', line 10 def assert_inputs_ok!(data, unique_by:) data_keys = data.first.keys missing = Array(unique_by) - data_keys return if missing.empty? msg = "All unique_by columns must be in the given data, but #{missing.inspect} was missing from #{data_keys}." raise Error, msg end |
.build_return(returning, rows, query) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/better_batch/active_record/transform.rb', line 28 def build_return(returning, rows, query) case returning when Symbol rows.map(&:first) when nil, [] nil else hash_rows(query.returning.map(&:input), rows) end end |
.hash_rows(returning, rows) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/better_batch/active_record/transform.rb', line 39 def hash_rows(returning, rows) # avoid building an entire new hash (which would rehash keys) for each row # we only need to sub in the values indexes = returning.each_with_index.to_h.freeze rows.map do |row| indexes.transform_values { |index| row[index] } end end |
.slice_upsert(data, except:) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/better_batch/active_record/transform.rb', line 19 def slice_upsert(data, except:) case except when nil, [] data else data.map { |datum| datum.except(*except) } end end |