Class: ContextAdaptation

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapted_classObject

Returns the value of attribute adapted_class.



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

def adapted_class
  @adapted_class
end

#adapted_implementationObject

Returns the value of attribute adapted_implementation.



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

def adapted_implementation
  @adapted_implementation
end

#adapted_selectorObject

Returns the value of attribute adapted_selector.



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

def adapted_selector
  @adapted_selector
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

Class Method Details

.in_context(a_context, a_class, a_selector, a_method) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/context_adaptation.rb', line 9

def self.in_context(a_context, a_class, a_selector, a_method)
  ca = self.new
  ca.context= a_context
  ca.adapted_class= a_class
  ca.adapted_selector= a_selector
  ca.adapted_implementation= a_method
  ca
end

Instance Method Details

#adapts_class?(a_class, a_symbol) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/context_adaptation.rb', line 33

def adapts_class?(a_class, a_symbol)
  self.adapted_class == a_class and self.adapted_selector == a_symbol
end

#deployObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/context_adaptation.rb', line 18

def deploy
  x = @adapted_implementation
  y = self
  if @context != Context.default
    @adapted_class.send(:define_method, @adapted_selector, lambda do |args = x.parameters, ca = y|
      Context.stack= Context.stack.push(ca)
	r = x.call(args)
	Context.stack.pop()
	r
    end)
  else
    @adapted_class.send(:define_method, @adapted_selector, @adapted_implementation)
  end
end

#same_target?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/context_adaptation.rb', line 37

def same_target?(other)
  self.adapts_class? other.adapted_class, other.adapted_selector
end

#to_sObject



41
42
43
# File 'lib/context_adaptation.rb', line 41

def to_s
  "for #{@adapted_selector.to_s} of #{@adapted_class.to_s} using #{@adapted_implementation.nil? ? "no implementation" : @adapted_implementation.to_s} in #{@context.to_s}"
end