Sha256: b2ac232b5152c98cb2bcfc0ab22e752f476f455c6268d993c09036c2cb2c6882

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

module Trestle
  class Registry
    include Enumerable

    # The admins hash is left exposed for backwards compatibility
    attr_reader :admins

    def initialize
      reset!
    end

    def each(&block)
      @admins.values.sort_by(&:admin_name).each(&block)
    end

    def reset!
      @admins = {}
      @models = {}
    end

    def empty?
      none?
    end

    def register(admin, register_model: true)
      @admins[admin.admin_name] = admin

      if register_model && register_admin_for_model_loookup?(admin)
        @models[admin.model.name] ||= admin
      end

      admin
    end

    def lookup_admin(admin)
      # Given object is already an admin class
      return admin if admin.is_a?(Class) && admin < Trestle::Admin

      @admins[admin.to_s]
    end
    alias lookup lookup_admin

    def lookup_model(model)
      # Lookup each class in the model's ancestor chain
      while model
        admin = @models[model.name]
        return admin if admin

        model = model.superclass
      end

      # No admin found
      nil
    end

  private
    def register_admin_for_model_loookup?(admin)
      admin.respond_to?(:model) && !(admin.respond_to?(:singular?) && admin.singular?)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
trestle-0.10.0 lib/trestle/registry.rb
trestle-0.10.0.pre2 lib/trestle/registry.rb
trestle-0.10.0.pre lib/trestle/registry.rb
trestle-0.9.8 lib/trestle/registry.rb
trestle-0.9.7 lib/trestle/registry.rb
trestle-0.9.6 lib/trestle/registry.rb