Class: Xumlidot::Parsers::Stack::Constants

Inherits:
Object
  • Object
show all
Defined in:
lib/xumlidot/parsers/stack.rb

Defined Under Namespace

Classes: ExternalKlassReferences

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstants

Returns a new instance of Constants.



12
13
14
15
16
# File 'lib/xumlidot/parsers/stack.rb', line 12

def initialize
  # This should be main or object
  @nesting = Xumlidot::Types::Klass.new(nil)
  @last_added = @nesting
end

Instance Attribute Details

#last_addedObject (readonly)

Returns the value of attribute last_added.



10
11
12
# File 'lib/xumlidot/parsers/stack.rb', line 10

def last_added
  @last_added
end

Instance Method Details

#add(c) ⇒ Object

when we add a constant we might want to add to the top of the tree e.g. Module A Moudle B

or we might want to add onto a given const e,g Module A

Module B

add(Module C, Module B)



34
35
36
37
38
39
40
# File 'lib/xumlidot/parsers/stack.rb', line 34

def add(c)
  return if @nesting.constants.find_first(c)

  root = @nesting.constants.root_namespace_for(c)
  (root.nil? ? @nesting.constants : root.constants) <<  c
  @last_added = c
end

#resolve_inheritance(constant = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/xumlidot/parsers/stack.rb', line 52

def resolve_inheritance(constant = nil)
  external_klasses = ExternalKlassReferences.new

  # The first traversal we are going through finding all
  # classes with a superklass. The second traversal we are
  # trying to find the klass which is the superklass of the superklass
  # found in the first traversal.
  #
  # Note Im hacking through this so poor code
  @nesting.constants.traverse do |klass|
    next if klass.definition.superklass.empty?

    # If we reach here we have a superklass
    @nesting.constants.traverse do |other_klass|
      if other_klass.definition.superklass_of?(klass.definition.superklass)
        if ::Xumlidot::Options.debug == true
          STDERR.puts "SETTING SUPERKLASS REFERENCE FOR #{klass} to #{other_klass}"
        end
        klass.superklass.reference = other_klass
        break
      end
    end

    if klass.superklass.reference.nil?
      # See if we have added it already to the list of external_klasses
      found = external_klasses.find do |external_klass|
        klass.definition == external_klass.definition
      end

      if found
        klass.superklass.reference = found
      else
        new_klass = klass.definition.superklass.to_klass
        klass.superklass.reference = new_klass
        external_klasses << new_klass

        add(new_klass)
      end
    end
  end
end

#traverse(&block) ⇒ Object



18
19
20
# File 'lib/xumlidot/parsers/stack.rb', line 18

def traverse(&block)
  @nesting.constants.traverse(&block)
end