Class: ContextManager
- Inherits:
-
Object
- Object
- ContextManager
- Defined in:
- lib/context_manager.rb
Constant Summary collapse
- @@default =
nil
Instance Attribute Summary collapse
-
#adaptations ⇒ Object
Returns the value of attribute adaptations.
-
#directory ⇒ Object
Returns the value of attribute directory.
Instance Method Summary collapse
- #activate_adaptation(context_adaptation) ⇒ Object
- #adaptation_chain(a_class, a_symbol) ⇒ Object
- #age_resolution_policy ⇒ Object
- #context_activation_age(context) ⇒ Object
- #deactivate_adaptation(context_adaptation) ⇒ Object
- #deploy_best_adaptation_for(a_class, a_symbol) ⇒ Object
- #discard_context(context) ⇒ Object
-
#initialize ⇒ ContextManager
constructor
A new instance of ContextManager.
- #resolution_policy=(policy) ⇒ Object
- #signal_activation_request(context) ⇒ Object
Constructor Details
#initialize ⇒ ContextManager
Returns a new instance of ContextManager.
9 10 11 12 13 14 15 |
# File 'lib/context_manager.rb', line 9 def initialize @directory = Hash.new @adaptations = Array.new @total_activations = 0 @activation_stamps = Hash.new self.resolution_policy= self.age_resolution_policy end |
Instance Attribute Details
#adaptations ⇒ Object
Returns the value of attribute adaptations.
7 8 9 |
# File 'lib/context_manager.rb', line 7 def adaptations @adaptations end |
#directory ⇒ Object
Returns the value of attribute directory.
7 8 9 |
# File 'lib/context_manager.rb', line 7 def directory @directory end |
Instance Method Details
#activate_adaptation(context_adaptation) ⇒ Object
24 25 26 27 |
# File 'lib/context_manager.rb', line 24 def activate_adaptation(context_adaptation) @adaptations.push(context_adaptation) self.deploy_best_adaptation_for(context_adaptation.adapted_class, context_adaptation.adapted_selector) end |
#adaptation_chain(a_class, a_symbol) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/context_manager.rb', line 38 def adaptation_chain(a_class, a_symbol) a = @adaptations.select do |adaptation| adaptation.adapts_class? a_class, a_symbol end raise Exception, "no adaptation found for #{a_class.to_s}.#{a_symbol.to_s}" if a.empty? a.sort do |a1, a2| @resolution_policy.call(a1.context, a2.context) end end |
#age_resolution_policy ⇒ Object
55 56 57 58 59 |
# File 'lib/context_manager.rb', line 55 def age_resolution_policy Proc.new { |a1, a2| (self.context_activation_age(a1) < self.context_activation_age(a2)) ? -1 : 1 } end |
#context_activation_age(context) ⇒ Object
61 62 63 |
# File 'lib/context_manager.rb', line 61 def context_activation_age(context) @total_activations - (@activation_stamps[context].nil? ? 0 : @activation_stamps[context]) end |
#deactivate_adaptation(context_adaptation) ⇒ Object
29 30 31 32 |
# File 'lib/context_manager.rb', line 29 def deactivate_adaptation(context_adaptation) raise Exception, "can't deactivate unmanaged adaptation" if @adaptations.delete(context_adaptation).nil? self.deploy_best_adaptation_for(context_adaptation.adapted_class, context_adaptation.adapted_selector) unless @adaptations.empty? end |
#deploy_best_adaptation_for(a_class, a_symbol) ⇒ Object
34 35 36 |
# File 'lib/context_manager.rb', line 34 def deploy_best_adaptation_for(a_class, a_symbol) self.adaptation_chain(a_class, a_symbol).first.deploy end |
#discard_context(context) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/context_manager.rb', line 17 def discard_context(context) raise Exception "can't discard outside context manager" if context.manager != self raise Exception, "can't discard an active context" if context.active? @directory.delete(self) @activation_stamps.delete(context) end |
#resolution_policy=(policy) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/context_manager.rb', line 48 def resolution_policy=(policy) @resolution_policy = policy @adaptations.each do |adaptation| self.deploy_best_adaptation_for(adaptation.adapted_class, adaptation.adapted_selector) end end |
#signal_activation_request(context) ⇒ Object
65 66 67 68 |
# File 'lib/context_manager.rb', line 65 def signal_activation_request(context) @total_activations += 1 @activation_stamps[context] = @total_activations end |