Sha256: 8d14fc69e6354461cbb386a97e49bcccc1bac043280bca0276fad382d0a0e690

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

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(raw_selector, raw_setter)
      @selector = raw_selector.inject({}) do |memo, (k, v)|
        memo[k.to_s] = v
        memo
      end

      @setter = raw_setter.inject({}) do |memo, (k, v)|
        memo[k.to_s] = 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

8 entries across 8 versions & 1 rubygems

Version Path
upsert-1.2.0 lib/upsert/row.rb
upsert-1.1.7 lib/upsert/row.rb
upsert-1.1.6 lib/upsert/row.rb
upsert-1.1.5 lib/upsert/row.rb
upsert-1.1.4 lib/upsert/row.rb
upsert-1.1.3 lib/upsert/row.rb
upsert-1.1.1 lib/upsert/row.rb
upsert-1.1.0 lib/upsert/row.rb