Sha256: a3d5dbb6cc3f28c7b0741f76dbcb917ee9adc137dd4fc4faa2696d4c5a55af9d

Contents?: true

Size: 1.71 KB

Versions: 12

Compression:

Stored size: 1.71 KB

Contents

#  
#
# Author:: Andre Kullmann
#
module ActiveRecord
  module Bulkoperation
    module Util
      class EntityHash < Hash  
        
        def each_pair_in_detail_hierarchy(&block)
          visited = []

          each_pair do |k, v|

            next if visited.include?(k)

            in_detail_hierarchy(k, visited, &block)

          end
        end
        
        def each_pair_in_master_hierarchy(&block)
          visited = []

          each_pair do |k, v|

            next if visited.include?(k)

            in_master_hierarchy(k, visited, &block)

          end
        end

        private

        def in_detail_hierarchy(record_class, visited, &block)
          return if visited.include?(record_class)

          visited << record_class

          record_class.foreign_detail_tables.each do |table_name|

            if key = find_key_by_table_name(table_name)

              in_detail_hierarchy(key, visited, &block)
            end
          end

          list = self[ record_class] and yield record_class, list
        end

        def in_master_hierarchy(record_class, visited, &block)
          return if visited.include?(record_class)

          visited << record_class

          record_class.foreign_master_tables.each do |table_name|

            if key = find_key_by_table_name(table_name)

              in_master_hierarchy(key, visited, &block)
            end
          end

          list = self[ record_class] and yield record_class, list
        end
      
        def find_key_by_table_name(table_name)
          name = table_name.upcase
          result = keys.select { |k| k.table_name.upcase == name }
          fail '' if result.size > 1
          result.first
        end

      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
activerecord_bulkoperation-0.2.3 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.2.2 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.2.1 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.2.0 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.1.0 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.8 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.7 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.6 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.5 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.4 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.3 lib/activerecord_bulkoperation/util/entity_hash.rb
activerecord_bulkoperation-0.0.2 lib/activerecord_bulkoperation/util/entity_hash.rb