Sha256: f42cc8aac19de13d17c17b41996c49add283fa5744c17aa4d4fd8704d655f30b

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Metacrunch
  class Db::Writer

    def initialize(database_connection_or_url, dataset_proc, options = {})
      @use_upsert = options.delete(:use_upsert) || false
      @id_key     = options.delete(:id_key)     || :id

      @db = if database_connection_or_url.is_a?(String)
        Sequel.connect(database_connection_or_url, options)
      else
        database_connection_or_url
      end

      @dataset = dataset_proc.call(@db)
    end

    def write(data)
      if data.is_a?(Array)
        @db.transaction do
          data.each{|d| insert_or_upsert(d) }
        end
      else
        insert_or_upsert(data)
      end
    end

    def close
      @db.disconnect
    end

  private

    def insert_or_upsert(data)
      @use_upsert ? upsert(data) : insert(data)
    end

    def insert(data)
      @dataset.insert(data) if data
    end

    def upsert(data)
      if data
        rec = @dataset.where(@id_key => data[@id_key])
        if 1 != rec.update(data)
          insert(data)
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metacrunch-3.0.3 lib/metacrunch/db/writer.rb