Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-rbac / lib / faculty_rbac / group / registration.rb
Size: Mime:
# frozen_string_literal: true

module FacultyRBAC
  class Group
    NotRegistered = Class.new(StandardError)

    # Groupmanager registration handling
    module Registration
      def registration
        _db[:gm_group].where(group_id: @id)
      end

      def update_registration(...)
        update_fields = _prepare_update(...)
        return if update_fields.empty?

        count = registration.update(**update_fields)
        raise NotRegistered, "Group #{@id} is not registered" if count.zero?
      end

      def description
        registration.get(:description) || "GID ##{@id}"
      end

      def description=(description)
        update_registration description:
      end

      def context
        registration.get(:context) || 'unknown'
      end

      def context=(context)
        update_registration context:
      end

      def type
        _db.from(_type).get(:name)&.to_sym || :unknown
      end

      def type_description
        _db.from(_type).get(:description)&.to_s || 'Unknown'
      end

      def type=(type)
        update_registration type:
      end

      # NB: Registration is done by Group's eigenclass
      def register(type:, context:, description:)
        _db[:gm_group].insert(group_id: @id, **_prepare_update(type:, context:, description:))
      end

      private

      def _prepare_update(description: nil, type: nil, context: nil)
        fields = {}
        fields[:type_id] = _db[:gm_grouptype].where(name: type.to_s).first![:id] if type
        fields[:description] = description.to_s if description
        fields[:context] = context.to_s if context
        fields
      end

      def deregister
        registration.delete
      end

      def _type
        registration.join(:gm_grouptype, id: :type_id).select_all(:gm_grouptype)
      end
    end
  end
end