Sha256: 4094b429863712c7576f3701b0b758bd879751c409347fc64bac2d584202da11

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module InstDataShipper
  module DataSources
    # This module contains the logic for processing local AR tables
    module LocalTables
      extend ActiveSupport::Concern

      public

      def import_local_table(*args, **kwargs)
        delayed(:_import_local_table, *args, **kwargs)
      end

      private

      def _import_local_table(model, schema_name: nil)
        model = model.safe_constantize if model.is_a?(String)

        table_def = lookup_table_schema!(schema_name, { model: model })

        inner_block = ->(file) {
          query = model.all
          query = _resolve_model_query(query, table_def[:query])

          if table_is_incremental?(table_def)
            query = _resolve_model_query(query, table_def.dig(:incremental, :scope), string: ->(r, c) { r.where("? > ?", c, incremental_since) })
          end

          query.find_each do |m|
            file << table_def[:columns].map do |c|
              instance_exec(m, &c[:block])
            end
          end
        }

        upload_data(table_def, &inner_block)
      end

      def _resolve_model_query(relation, query, string: nil)
        return relation if query.nil?

        if query.is_a?(Symbol)
          relation.send(query)
        elsif query.is_a?(Proc)
          instance_exec(relation, &query)
        elsif query.is_a?(String) && string.present?
          instance_exec(relation, query, &string)
        else
          raise "Invalid query: #{query.inspect}"
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inst_data_shipper-0.1.0.beta2 lib/inst_data_shipper/data_sources/local_tables.rb