Sha256: bcda956a7224a32f2bd0176a971d201a7419555d0fb5f67244bc9006c89cf1c6

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require_relative "../i18n"

module Skala::I18n::DeepMerger
  def self.deep_merge!(destination, source, options = {})
    if destination.kind_of?(Array)
      if source.kind_of?(Array)
        # array into array
        source.each do |element|
          self.deep_merge!(destination, element, options)
        end
      elsif source.kind_of?(Hash)
        # hash into array
        destination.push(source)
      else
        # literal into array
        destination.push(source)
      end
    elsif destination.kind_of?(Hash)
      if source.kind_of?(Array)
        # array into hash
        raise ArgumentError, "Cannot merge array into hash!"
      elsif source.kind_of?(Hash)
        # hash into hash
        destination.merge!(source) do |key, destination_value, source_value|
          self.deep_merge!(destination_value, source_value, options)
          destination_value
        end
      else
        # literal into hash
        raise ArgumentError, "Cannot merge literal into hash!"
      end
    else
      if source.kind_of?(Array)
        # array into literal
        raise ArgumentError, "Cannot merge array into literal!"
      elsif source.kind_of?(Hash)
        # hash into literal
        raise ArgumentError, "Cannot merge hash into literal!"
      else
        # literal into literal
        destination = source # in fact a useless assignment to fit into the overall semantic
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skala-0.2.0 lib/skala/i18n/deep_merger.rb