Sha256: f0b41aa80e8c7ad9b3b509979dd9141ef9e09af28e0a9fb17df7e8b0371ee5a0

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module TkInspect
  module ClassBrowser
    class ClassTreeDataSource
      attr_accessor :class_filter

      def items_for_path(path)
        path = [] if path.blank?
        parent_class = path.last
        subclasses_of(parent_class).sort
      end

      def title_for_path(path, items)
        "#{items.count} #{'class'.pluralize(items.count)}"
      end

      def path_for_class(class_name)
        return unless (klass = Object.const_get(class_name))
        path = [class_name]
        while (klass = klass.superclass) do
          path.unshift(name_for_class(klass))
        end
        path
      end

      private

      def subclasses_of(class_name)
        filtered_classes.select do |k|
          k.superclass&.to_s == class_name
        end.map { |k| name_for_class(k) }
      end

      def filtered_classes
        list = ObjectSpace.each_object(Class).select do |k|
          !k.name.nil?
        end
        return list if @class_filter.blank?
        list.select! do |k|
          k.name.match(/#{@class_filter}/i)
        end
        ancestors = list.reduce(Set.new) do |acum, k|
          an = k
          while !(an = an.superclass).nil? do
            acum << an
          end
          acum
        end
        (list.to_set + ancestors).to_a
      end

      def name_for_class(klass)
        klass.respond_to?(:name) ? klass.name : klass.to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tk_inspect-0.2.1 lib/tk_inspect/class_browser/class_tree_data_source.rb
tk_inspect-0.2.0 lib/tk_inspect/class_browser/class_tree_data_source.rb
tk_inspect-0.1.0 lib/tk_inspect/class_browser/class_tree_data_source.rb