<commit_before>module Groupify
  module ActiveRecord

    class NamedGroupCollection < Set
      def initialize(member)
        @member = member
        @named_group_memberships = member.group_memberships_as_member.named
        @group_names = @named_group_memberships.pluck(:group_name).map(&:to_sym)

        super(@group_names)
      end

      def add(named_group, opts = {})
        named_group = named_group.to_sym
        membership_type = opts[:as].present? ? opts[:as].to_s : nil

        # always add a nil membership type and then a specific one (if specified)
        membership_types = [nil, membership_type].uniq

        @member.transaction do
          membership_types.each do |membership_type|
            if @member.new_record?
              @member.group_memberships_as_member.build(group_name: named_group, membership_type: membership_type)
            else
              @member.group_memberships_as_member.where(group_name: named_group, membership_type: membership_type).first_or_create!
            end
          end
        end

        super(named_group)
      end

      alias_method :push, :add
      alias_method :<<, :add

      def merge(*named_groups)
        opts = named_groups.extract_options!

        named_groups.flatten.each do |named_group|
          add(named_group, opts)
        end
      end

      alias_method :concat, :merge

      def include?(named_group, opts = {})
        named_group = named_group.to_sym

        if opts[:as]
          as(opts[:as]).include?(named_group)
        else
          super(named_group)
        end
      end

      def delete(*named_groups)
        opts = named_groups.extract_options!

        remove(named_groups.flatten.compact, :delete_all, opts[:as])
      end

      def destroy(*named_groups)
        opts = named_groups.extract_options!

        remove(named_groups.flatten.compact, :destroy_all, opts[:as])
      end

      def clear
        @named_group_memberships.delete_all
        super
      end

      alias_method :delete_all, :clear
      alias_method :destroy_all, :clear

      # Criteria to filter by membership type
      def as(membership_type)
        if membership_type.present?
          @named_group_memberships.as(membership_type).pluck(:group_name).map(&:to_sym)
        else
          to_a
        end
      end

    protected

      def remove(named_groups, destruction_type, membership_type = nil)
        if named_groups.present?<commit_msg>Fix up some consistency in the code<commit_after>module Groupify
  module ActiveRecord

    class NamedGroupCollection < Set
      def initialize(member)
        @member = member
        @named_group_memberships = member.group_memberships_as_member.named
        @group_names = @named_group_memberships.pluck(:group_name).map(&:to_sym)

        super(@group_names)
      end

      def add(named_group, opts = {})
        named_group = named_group.to_sym
        membership_type = opts[:as].to_s if opts[:as].present?

        # always add a nil membership type and then a specific one (if specified)
        membership_types = [nil, membership_type].uniq

        @member.transaction do
          membership_types.each do |membership_type|
            if @member.new_record?
              @member.group_memberships_as_member.build(group_name: named_group, membership_type: membership_type)
            else
              @member.group_memberships_as_member.where(group_name: named_group, membership_type: membership_type).first_or_create!
            end
          end
        end

        super(named_group)
      end

      alias_method :push, :add
      alias_method :<<, :add

      def merge(*named_groups)
        opts = named_groups.extract_options!

        named_groups.flatten.each do |named_group|
          add(named_group, opts)
        end
      end

      alias_method :concat, :merge

      def include?(named_group, opts = {})
        named_group = named_group.to_sym

        if opts[:as]
          as(opts[:as]).include?(named_group)
        else
          super(named_group)
        end
      end

      def delete(*named_groups)
        membership_type = named_groups.extract_options![:as]

        remove(named_groups.flatten.compact, :delete_all, membership_type)
      end

      def destroy(*named_groups)
        membership_type = named_groups.extract_options![:as]

        remove(named_groups.flatten.compact, :destroy_all, membership_type)
      end

      def clear
        @named_group_memberships.delete_all
        super
      end

      alias_method :delete_all, :clear
      alias_method :destroy_all, :clear

      # Criteria to filter by membership type
      def as(membership_type)
        if membership_type.present?
          @named_group_memberships.as(membership_type).pluck(:group_name).map(&:to_sym)
        else
          to_a.map(&:to_sym)
        end
      end

    protected

      def remove(named_groups, destruction_type, membership_type = nil)
        if named_groups.present?
{'fraction_non_alphanumeric': 0.07883650952858576, 'fraction_numerical': 0.0, 'mean_word_length': 2.4291609353507564, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4903085', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1424, 'n_words': 293}