Class: Import_Module::Scope

Inherits:
Object show all
Defined in:
lib/algebra/import-module.rb,
lib/algebra/import-module-single-thread.rb

Overview

kk

Constant Summary collapse

@@cm_no =
-1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, source) ⇒ Scope



117
118
119
120
121
122
# File 'lib/algebra/import-module.rb', line 117

def initialize(target, source)
  @target = target
  @klass = target.klass
  @source = source
  @mod = source.mod
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



102
103
104
# File 'lib/algebra/import-module.rb', line 102

def klass
  @klass
end

#modObject (readonly)

Returns the value of attribute mod.



102
103
104
# File 'lib/algebra/import-module.rb', line 102

def mod
  @mod
end

#sourceObject (readonly)

Returns the value of attribute source.



102
103
104
# File 'lib/algebra/import-module.rb', line 102

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



102
103
104
# File 'lib/algebra/import-module.rb', line 102

def target
  @target
end

Class Method Details

.create(klass, mod) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/algebra/import-module.rb', line 105

def self.create(klass, mod)
  target = Target.enclose(klass)
  source = Source.enclose(mod)
  scope = enclose(target, source)
  scope.set_methods
  scope
end

.enclose(target, source) ⇒ Object



113
114
115
# File 'lib/algebra/import-module.rb', line 113

def self.enclose(target, source)
  target.scopes[source.mod] ||= new(target, source)
end

Instance Method Details

#activateObject



124
125
126
127
128
129
130
131
# File 'lib/algebra/import-module.rb', line 124

def activate
  push
  begin
    yield
  ensure
   pop
  end
end

#inspectObject



142
143
144
# File 'lib/algebra/import-module.rb', line 142

def inspect
  "Scope(#{@target.inspect}, #{@source.inspect})"
end

#method_code(meth) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/algebra/import-module.rb', line 182

def method_code(meth)
  param = @source.param(meth)
  no = @target.meth_no[meth]
  line_no = __LINE__ + 1
  s = "def #{meth}(#{param})\n"
  s << "  modid = Thread.current.__IMPORT_MODULE_PREFIX_proxy[#{no}]\n"
  i = 0
  @target.scopes.each_key do |mod|
  	s << (i == 0 ? "  " : "  els") << "if modid == #{mod.object_id}\n"
  	s << "    #{Import_Module.name(meth, mod)}(#{param})\n"
  	i += 1
  end
  s << "  else\n" if i > 0
  s << "    #{Import_Module.name(meth, :orig)}(#{param})\n"
  s << "  end\n" if i > 0
  s << "end\n"
  s << "protected(:#{meth})\n" if @target.protecteds.include?(meth)
  s << "private(:#{meth})\n" if @target.privates.include?(meth)
  [s, __FILE__, line_no]
end

#popObject



151
152
153
# File 'lib/algebra/import-module.rb', line 151

def pop
  Thread.current.__IMPORT_MODULE_PREFIX_stack.pop
end

#pushObject



146
147
148
149
# File 'lib/algebra/import-module.rb', line 146

def push()
  c = Thread.current.__IMPORT_MODULE_PREFIX_stack.current
  Thread.current.__IMPORT_MODULE_PREFIX_stack.push(update(c))
end

#set_meth_noObject

private ### this declearation makes it slow



157
158
159
160
161
# File 'lib/algebra/import-module.rb', line 157

def set_meth_no
  @source.methods.__each__ do |meth|
   @target.meth_no[meth] || @target.meth_no[meth] = (@@cm_no += 1)
  end
end

#set_methodsObject



133
134
135
136
137
138
139
140
# File 'lib/algebra/import-module.rb', line 133

def set_methods
  meths = @target.get_orig_methods(@source)
  @target.def_orig_methods(meths)
  set_meth_no
  def_methods
  mod = @mod
  @klass.class_eval do include mod end
end