sul-dlss/cocina-models

View on GitHub
lib/cocina/models/mapping/from_mods/alt_rep_group.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

module Cocina
  module Models
    module Mapping
      module FromMods
        # Helper class: splits nodes by altRepGroup ids
        class AltRepGroup
          # @param [Array<Nokogiri::XML::Element>] nodes to split
          # @return [Array<Array<Nokogiri::XML::Element>>, Array<Nokogiri::XML::Element>] nodes grouped by altRepGroup, other nodes
          def self.split(nodes:)
            all_nodes_with_altrepgroup = nodes.reject { |node| node[:altRepGroup].blank? }
            grouped_altrepgroup_nodes = all_nodes_with_altrepgroup
                                        .group_by { |node| node[:altRepGroup] }
                                        .values
                                        .reject { |group_nodes| group_nodes.size == 1 }

            other_nodes = nodes.reject { |node| grouped_altrepgroup_nodes.flatten.include?(node) }

            [grouped_altrepgroup_nodes, other_nodes]
          end
        end
      end
    end
  end
end