Sha256: 414a20cbc7a7bb06ea28930b4a5cc2d35aff410c6316fb408fd27d0f05d7476d

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module DeepPluck
  module DataCombiner
    class << self
      def combine_data(parent, children, primary_key, column_name, foreign_key, reverse, collection)
        source =  reverse ? parent : children
        target = !reverse ? parent : children
        data_hash = make_data_hash(collection, source, primary_key, column_name)
        assign_values_to_parent(collection, target, data_hash, column_name, foreign_key, reverse: reverse)
        return children
      end
    private
      def make_data_hash(collection, parent, primary_key, column_name)
        return parent.map{|s| [s[primary_key], s]}.to_h if !collection
        hash = {}
        parent.each do |model_hash|
          key = model_hash[primary_key]
          array = (hash[key] ? hash[key][column_name] : []) #share the children if id is duplicated
          model_hash[column_name] = array
          hash[key] = model_hash
        end
        return hash
      end
      def assign_values_to_parent(collection, parent, children_hash, column_name, foreign_key, reverse: false)
        parent.each{|s|
          next if (id = s[foreign_key]) == nil
          left   =  reverse ? children_hash[id] : s
          right  = !reverse ? children_hash[id] : s
          if collection
            left[column_name] << right
          else
            left[column_name] = right
          end
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
deep_pluck-1.0.3 lib/deep_pluck/data_combiner.rb
deep_pluck-1.0.2 lib/deep_pluck/data_combiner.rb
deep_pluck-1.0.1 lib/deep_pluck/data_combiner.rb
deep_pluck-1.0.0 lib/deep_pluck/data_combiner.rb