Sha256: 799a0f2c837d7bc63826480da920b7b91b1aca8079d1b607bcbc9be782fe7f74

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

require 'tree_graph'

module Constree
  Node = Struct.new :constant, :name, :parent do

    include TreeGraph

    def label_for_tree_graph
      display_name + ' ' + type
    end

    def children_for_tree_graph
      @sub_consts ||= []
    end

    attr_accessor :ref

    def sub_nodes
      return [] unless constant.is_a? Module
      constant.constants.map do |name|
        Node.new(constant.const_get(name), name, self)
      end
    end

    def top?
      parent ? false : true
    end

    def display_name
      (name || constant.name).to_s
    end

    def full_name
      top? ? constant.name : "#{parent.full_name}::#{name}"
    end

    def == other
      return false unless other.is_a? Node
      constant == other.constant
    end

    def type
      ref ? "→ #{ref.full_name}" : "(#{constant.class.to_s})"
    end

    def not_yet? seen
      i = seen.find_index self
      if i == seen.count - 1
       true
      else
        self.ref = seen[i]
        false
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
constree-0.1.3 lib/constree/node.rb