Class: MethodLog::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/method_log/scope.rb

Direct Known Subclasses

Null, Root

Defined Under Namespace

Classes: Null, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil, singleton = false) ⇒ Scope

Returns a new instance of Scope.



29
30
31
32
33
34
# File 'lib/method_log/scope.rb', line 29

def initialize(name, parent = nil, singleton = false)
  @name = name
  @parent = parent
  @singleton = singleton
  @modules = {}
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



27
28
29
# File 'lib/method_log/scope.rb', line 27

def parent
  @parent
end

Instance Method Details

#define(name) ⇒ Object



48
49
50
# File 'lib/method_log/scope.rb', line 48

def define(name)
  @modules[name] = Scope.new(name, parent = self)
end

#for(modules) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/method_log/scope.rb', line 36

def for(modules)
  scope = self
  if modules.first == :root
    scope = root
    modules.unshift
  end
  modules.each do |mod|
    scope = scope.lookup(mod) || scope.define(mod)
  end
  scope
end

#lookup(name) ⇒ Object



52
53
54
# File 'lib/method_log/scope.rb', line 52

def lookup(name)
  @modules.fetch(name) { @parent.lookup(name) }
end

#method_identifier(name) ⇒ Object



60
61
62
# File 'lib/method_log/scope.rb', line 60

def method_identifier(name)
  [names.join('::'), separator, name].join
end

#rootObject



64
65
66
# File 'lib/method_log/scope.rb', line 64

def root
  parent.root
end

#singletonObject



56
57
58
# File 'lib/method_log/scope.rb', line 56

def singleton
  Scope.new(@name, parent = self, singleton = true)
end