Class: Riml::ClassMap

Inherits:
Object
  • Object
show all
Defined in:
lib/riml/class_map.rb

Overview

Map of => ClassDefinitionNode Can also query object for superclass of a named class, etc…

Ex : class_map.superclass(“g:SomeClass”) => “g:SomeClassBase”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClassMap

Returns a new instance of ClassMap.



12
13
14
15
16
# File 'lib/riml/class_map.rb', line 12

def initialize
  @map = {}
  # list of ImportedClass objects that are #globbed?
  @globbed_imports = []
end

Instance Attribute Details

#globbed_importsObject (readonly)

Returns the value of attribute globbed_imports.



10
11
12
# File 'lib/riml/class_map.rb', line 10

def globbed_imports
  @globbed_imports
end

Instance Method Details

#[](key) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/riml/class_map.rb', line 18

def [](key)
  ensure_key_is_string!(key)
  klass = @map[key]
  return klass if klass
  if key[0..1] == 'g:'
    globbed_imports.each do |imported_class|
      if imported_class.match?(key)
        return @map[key] = ImportedClass.new(key)
      end
    end
  end
  raise ClassNotFound, "class #{key.inspect} not found."
end

#[]=(key, val) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/riml/class_map.rb', line 32

def []=(key, val)
  ensure_key_is_string!(key)
  if class_node = @map[key]
    if !class_node.instance_variable_get("@registered_state") &&
       !val.instance_variable_get("@registered_state")
      class_redefinition!(key)
    end
  end
  @map[key] = val
end

#class_namesObject



52
53
54
# File 'lib/riml/class_map.rb', line 52

def class_names
  @map.keys
end

#classesObject



48
49
50
# File 'lib/riml/class_map.rb', line 48

def classes
  @map.values
end

#has_global_import?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/riml/class_map.rb', line 60

def has_global_import?
  @globbed_imports.any? { |import| import.global_import? }
end

#safe_fetch(key) ⇒ Object



56
57
58
# File 'lib/riml/class_map.rb', line 56

def safe_fetch(key)
  @map[key]
end

#superclass(key) ⇒ Object



43
44
45
46
# File 'lib/riml/class_map.rb', line 43

def superclass(key)
  super_key = self[key].superclass_name
  self[super_key]
end