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

Class Method Summary collapse

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
58
59
60
61
62
63
64
65
66
67
# 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
    Benelux.ld [:def, self, m, 
      MethodPacker.method_defined?(self, m),
      MethodPacker.class_method_defined?(self, m)].inspect
    if MethodPacker.method_defined?(self, m)
      alias_method methorig, m  # Can't use the instance variables
    elsif MethodPacker.class_method_defined?(self, m)
      eval "class << self; alias #{methorig} #{m}; end"
    else
      raise BeneluxError, "No such method: #{self}.#{m}"
    end
  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

Class Method Details

.class_method_defined?(klass, meth) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/benelux/packer.rb', line 74

def self.class_method_defined?(klass, meth)
  meth &&= meth.to_s if RUBY_VERSION < "1.9"
  klass.singleton_methods.member? meth
end

.method_defined?(klass, meth) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.method_defined?(klass, meth)
  meth &&= meth.to_s if RUBY_VERSION < "1.9"
  klass.method_defined?(meth) || 
  klass.private_method_defined?(meth) ||
  klass.protected_method_defined?(meth)
end

Instance Method Details

#install_methodObject



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

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

#instance_exec(*args, &block) ⇒ Object

!> method redefined; discarding old instance_exec



86
87
88
89
90
91
92
93
94
95
# File 'lib/benelux/packer.rb', line 86

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



97
98
99
# File 'lib/benelux/packer.rb', line 97

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