Sha256: 6eaa202e16db2608301eceb7eee9e98561bfccdc7dda93a35bb5a7f6558e53b2

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# An entity is a named node of a given type which may have additional properties
module Analyst
  module Entities
    class Entity

      attr_reader :parent

      def initialize(ast, parent)
        @parent = parent
        @ast = ast
      end

      def handle_send_node(node)
        # raise "Subclass must implement handle_send_node"
        # abstract method.  btw, this feels wrong -- send should be an entity too.  but for now, whatevs.
      end

      # TODO: should every Entity have these accessors? maybe they're mixins... but would that provide any benefit?
      def classes
        @classes ||= begin
          nested_classes = top_level_classes.map(&:classes).flatten
          namespaced_classes = top_level_modules.map(&:classes).flatten
          top_level_classes + nested_classes + namespaced_classes
        end
      end

      def top_level_modules
        @top_level_modules ||= contents_of_type(Entities::Module)
      end

      def top_level_classes
        @top_level_classes ||= contents_of_type(Entities::Class)
      end

      def method_calls
        @method_calls ||= contents_of_type(Entities::MethodCall)
      end

      def full_name
        throw "Subclass #{self.class.name} must implement #full_name"
      end

      def inspect
        "\#<#{self.class}:#{object_id} full_name=#{full_name}>"
      rescue
        "\#<#{self.class}:#{object_id}>"
      end

      private

      attr_reader :ast

      def contents_of_type(klass)
        contents.select { |entity| entity.is_a? klass }
      end

      def contents
        @contents ||= Array(Analyst::Parser.process_node(content_node, self))
      end

      def content_node
        ast.children.last
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
analyst-0.14.1 lib/analyst/entities/entity.rb
analyst-0.14.0 lib/analyst/entities/entity.rb