Class: Benelux::MethodPacker

Inherits:
Object
  • Object
show all
Includes:
InstanceExecHelper, Selectable::Object
Defined in:
lib/benelux/packer.rb

Direct Known Subclasses

MethodCounter, MethodTimer

Defined Under Namespace

Modules: InstanceExecHelper

Instance Attribute Summary collapse

Attributes included from Selectable::Object

#tags

Instance Method Summary collapse

Methods included from Selectable::Object

#add_tags, #add_tags_quick, #init_tags!, #remove_tags, #tag_values

Constructor Details

#initialize(k, m, aliaz, &blk) ⇒ MethodPacker

  • k is a class

  • m is the name of an instance method in k

  • aliaz is an optional name for

This method makes the following changes to class k.

  • Add a timeline attic to and include Benelux

  • Rename the method to something like: __benelux_execute_2151884308_2165479316

  • Install a new method with the name m.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/benelux/packer.rb', line 24

def initialize(k,m,aliaz,&blk)
  Benelux.ld "%20s: %s#%s" % [self.class, k, m]
  if Benelux.packed_method? k, m
    raise SelectableError, "Already defined (#{k} #{m})"
  end
  @klass, @meth, @aliaz, @blk = k, m, aliaz, blk
  @aliaz ||= meth
  @klass.extend Attic  unless @klass.kind_of?(Attic)
  unless @klass.kind_of?(Benelux)
    @klass.attic :timeline
    @klass.send :include, Benelux
  end
  ## NOTE: This is commented out so we can include  
  ## Benelux definitions before all classes are loaded. 
  ##unless obj.respond_to? meth
  ##  raise NoMethodError, "undefined method `#{meth}' for #{obj}:Class"
  ##end
  thread_id, call_id = Thread.current.object_id.abs, @klass.object_id.abs
  @methorig = methorig = :"__benelux_#{@meth}_#{thread_id}_#{call_id}"
  Benelux.ld "%20s: %s" % ['Alias', @methorig] 
  @klass.module_eval do
    alias_method methorig, m  # Can't use the instance variables
  end
  install_method  # see generate_packed_method
  self.add_tags :class => @klass.to_s.to_sym, 
                :meth  => @meth.to_sym,
                :kind  => self.class.to_s.to_sym

  Benelux.packed_methods[@klass] ||= {}
  Benelux.packed_methods[@klass][@meth] = self
  Benelux.packed_methods[:all] ||= []
  Benelux.packed_methods[:all] << self
  
end

Instance Attribute Details

#aliazObject

Returns the value of attribute aliaz.



8
9
10
# File 'lib/benelux/packer.rb', line 8

def aliaz
  @aliaz
end

#blkObject (readonly)

Returns the value of attribute blk.



11
12
13
# File 'lib/benelux/packer.rb', line 11

def blk
  @blk
end

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/benelux/packer.rb', line 9

def klass
  @klass
end

#methObject (readonly)

Returns the value of attribute meth.



10
11
12
# File 'lib/benelux/packer.rb', line 10

def meth
  @meth
end

#methorigObject

Returns the value of attribute methorig.



7
8
9
# File 'lib/benelux/packer.rb', line 7

def methorig
  @methorig
end

Instance Method Details

#install_methodObject



58
59
60
# File 'lib/benelux/packer.rb', line 58

def install_method
  raise "You need to implement this method"
end

#instance_exec(*args, &block) ⇒ Object

!> method redefined; discarding old instance_exec



66
67
68
69
70
71
72
73
74
75
# File 'lib/benelux/packer.rb', line 66

def instance_exec(*args, &block) # !> method redefined; discarding old instance_exec
  mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
  InstanceExecHelper.module_eval{ define_method(mname, &block) }
  begin
    ret = send(mname, *args)
  ensure
    InstanceExecHelper.module_eval{ undef_method(mname) } rescue nil
  end
  ret
end

#run_block(*args) ⇒ Object



77
78
79
# File 'lib/benelux/packer.rb', line 77

def run_block(*args)
  raise "must implement"
end