Sha256: fc567e28af25306cfb7bd3714e15bb28628c2493578b00561f6c75753af036d9

Contents?: true

Size: 749 Bytes

Versions: 1

Compression:

Stored size: 749 Bytes

Contents

require 'json'
require "json/merge_patch/version"

module JSON
  MergeError = Class.new(StandardError)

  def self.merge(document, merge_patch)
    document = JSON.parse(document)
    merge_patch = JSON.parse(merge_patch)

    if merge_patch.is_a? Array
      return JSON.dump(merge_patch)
    end

    if document.is_a? Array
      return JSON.dump(merge_patch)
    end

    if merge_patch.is_a? Hash
      document.merge!(merge_patch)
      document.delete_if(&method(:recursive_delete_if))
      return JSON.dump(document)
    end
  rescue
    raise MergeError
  end

  def self.recursive_delete_if(key, value)
    if value.kind_of?(Hash)
      value.delete_if(&method(:recursive_delete_if))
      nil
    else
      value.nil?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json-merge_patch-0.1.0 lib/json/merge_patch.rb