-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.rb
64 lines (53 loc) · 1.39 KB
/
extra.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
#!/usr/local/bin/ruby
require './cop'
class ExtraContext < Context
# Add priority
class CIP < CI
attr_accessor :prio
def initialize(ctx, impl, prio)
@ctx = ctx
@impl = impl
@prio = prio
end
end
def initialize
Context.set_vars
@@context_priority = Hash.new do |prios|
prios = Array.new
end
end
# Add a method to the class
# adaptations are stored in a hashmap containing a stack of adaptations
# for each method
def adapt(klass, method, &impl)
# If this is the first adapt, start by adding base methods
self.add_base_methods(klass)
# Define a proceed method
latest = self.proceed(klass, method)
# XXX (´・_・`) hack
hack = impl.to_source(:ignore_nested => true).gsub(/proceed/, latest.to_s)
# Add the adaptation
prio = @@adaptations[klass][method].size
self.push_adapt(klass, method, CIP.new(self, eval(hack), prio))
# Apply
self.dynamic_adapt
end
def activate
@@count += 1
@@context_priority[self].push(@@count)
self.dynamic_adapt
end
def deactivate
if !active?; return end
# Remove adaptations
@@adaptations.each do |klass,methods|
methods.each do |m,impls|
impls.delete_if do |cip|
cip.ctx == self && cip.prio == @@count
end
self.send_method(klass, m, impls.last.impl)
end
end
@@count -= 1
end
end