Sha256: af124cf46e690d8dae56a6b0c8789e72af5d6946d1b83d4bd53f461024202c60

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'upsert/row/mysql2_client'
require 'upsert/row/pg_connection'
require 'upsert/row/sqlite3_database'

class Upsert
  # @private
  class Row
    if RUBY_VERSION >= '1.9'
      OrderedHash = ::Hash
    else
      begin
        require 'orderedhash'
      rescue LoadError
        raise LoadError, "[upsert] If you're using upsert on Ruby 1.8, you need to add 'orderedhash' to your Gemfile."
      end
      OrderedHash = ::OrderedHash
    end

    attr_reader :selector
    attr_reader :setter


    def initialize(parent, raw_selector, raw_setter)
      connection = parent.connection
      cell_class = parent.cell_class

      @selector = raw_selector.inject({}) do |memo, (k, v)|
        memo[k.to_s] = cell_class.new(connection, k, v)
        memo
      end

      @setter = raw_setter.inject({}) do |memo, (k, v)|
        memo[k.to_s] = cell_class.new(connection, k, v)
        memo
      end

      (selector.keys - setter.keys).each do |missing|
        setter[missing] = selector[missing]
      end

      # there is probably a more clever way to incrementally sort these hashes
      @selector = sort_hash selector
      @setter = sort_hash setter
    end

    private

    def sort_hash(original)
      original.keys.sort.inject(OrderedHash.new) do |memo, k|
        memo[k] = original[k]
        memo
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
upsert-0.5.0 lib/upsert/row.rb