Sha256: e76fe56c3e037e7a5ca6e97979cc56b2ffa5a6eff43ed68af7992cbd515ced31

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

module Coral
module Util
class Data < Core
   
  #-----------------------------------------------------------------------------
  # Utilities
    
  def self.merge(objects, merge_arrays = false)
    value = nil
    return value unless objects
    
    unless objects.is_a?(Array)
      objects = [ objects ]
    end
            
    objects.each do |object|
      value = recursive_merge(value, object, merge_arrays)  
    end
    return value
  end
    
  #---
    
  def self.recursive_merge(overrides, data, merge_arrays = false)
    return data unless overrides
    return overrides unless data
            
    if overrides.is_a?(Hash)
      overrides.each do |name, override|
        if data.is_a?(Hash)
          if data[name]
            data[name] = recursive_merge(override, data[name])
          else
            begin
              item = override.dup
            rescue TypeError
              item = override
            end
            data[name] = recursive_merge(override, item)
          end
        else
          data = overrides
        end
      end
    elsif merge_arrays && overrides.is_a?(Array)
      if data.is_a?(Array)
        data = data | overrides
      else
        data = overrides
      end
    else
      data = overrides
    end
    return data   
  end
end
end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
coral_core-0.1.10 lib/coral_core/util/data.rb
coral_core-0.1.9 lib/coral_core/util/data.rb
coral_core-0.1.8 lib/coral_core/util/data.rb
coral_core-0.1.7 lib/coral_core/util/data.rb
coral_core-0.1.6 lib/coral_core/util/data.rb
coral_core-0.1.5 lib/coral_core/util/data.rb
coral_core-0.1.4 lib/coral_core/util/data.rb
coral_core-0.1.2 lib/coral_core/util/data.rb