Sha256: df1f3490d083b6991b9778488de95d877a50d3cb0fc76020a0c8107c21c8214c

Contents?: true

Size: 598 Bytes

Versions: 2

Compression:

Stored size: 598 Bytes

Contents

module HubLink
  class Insert
    delegate :primary_key, to: :target

    def initialize(row:, target:)
      @row = row
      @target = target
    end

    def run
      record.update! importable_attributes
    end

    private

      def record
        target.find_or_initialize_by(primary_key => primary_key_value)
      end

      def primary_key_value
        row[primary_key]
      end

      def importable_attributes
        row.slice(*target_columns)
      end

      def target_columns
        target.columns.map(&:name).map(&:to_sym)
      end

      attr_reader :row, :target
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hub_link-0.13.0 lib/hub_link/insert.rb
hub_link-0.12.0 lib/hub_link/insert.rb