lib/phenomenal/manager.rb in phenomenal-0.99.0 vs lib/phenomenal/manager.rb in phenomenal-1.0.0

- old
+ new

@@ -3,11 +3,11 @@ class Phenomenal::Manager include Singleton include Phenomenal::ConflictPolicies attr_accessor :active_adaptations, :deployed_adaptations, :contexts, -:default_context, :combined_contexts, :shared_contexts, :rmanager + :default_context, :combined_contexts, :shared_contexts, :rmanager # Register a new context def register_context(context) if context_defined?(context) Phenomenal::Logger.instance.error( @@ -18,30 +18,31 @@ Phenomenal::Logger.instance.error( "There is already a context with name: #{context.name}." + " If you want to have named context it has to be a globally unique name" ) end + # Update the relationships that concern this context + rmanager.update_relationships_references(context) + # Store the context at its ID contexts[context]=context end # Unregister a context (forget) def unregister_context(context) - if context==default_context && !contexts.size==1 + if context==default_context && contexts.size>1 Phenomenal::Logger.instance.error( "Default context can only be forgotten when alone" ) else contexts.delete(context) - # Forgot combined contexts combined_contexts.delete(context) if shared_contexts[context] shared_contexts[context].each do |c| c.forget end end - # Restore default context init_default() if context==default_context end end @@ -63,12 +64,11 @@ # Activate the context 'context' and deploy the related adaptation def activate_context(context) begin # Relationships managment - rmanager.activate_relationships(context) if context.just_activated? - + rmanager.activate_relationships(context) if context.just_activated? # Activation of adaptations context.adaptations.each{ |i| activate_adaptation(i) } #puts "activation of #{context}" if shared_contexts[context] #puts "trigger activation of #{shared_contexts[context].first.information}" @@ -112,25 +112,23 @@ # IMPROVE Problems will also appears if two adaptations are defined on the same # line using the ';' some check needed at add_adaptation ? adaptations_stack = sorted_adaptations_for(calling_adaptation.klass, calling_adaptation.method_name,calling_adaptation.instance_adaptation?) calling_adaptation_index = adaptations_stack.find_index(calling_adaptation) - next_adaptation = adaptations_stack[calling_adaptation_index+1] - next_adaptation.bind(instance,*args, &block) end # Change the conflict resolution policy. # These can be ones from the ConflictPolicies module or other ones # Other one should return -1 or +1 following the resolution order def change_conflict_policy (&block) self.class.class_eval{define_method(:conflict_policy,&block)} end - # Return the corresponding context or raise an error if the context isn't - # currently registered. + # Return the corresponding context (or combined context) or raise an error + # if the context isn't currently registered. # The 'context' parameter can be either a reference to a context instance or # a Symbol with the name of a named (not anonymous) context. def find_context(context, *contexts) if contexts.length==0 find_simple_context(context) @@ -138,11 +136,11 @@ contexts.insert(0,context) find_combined_context(contexts) end end - # Check wether context 'context' exist in the context manager + # Check wether context 'context' (or combined context) exist in the context manager # Context can be either the context name or the context instance itself # Return the context if found, or nil otherwise def context_defined?(context, *contexts) c=nil begin @@ -150,10 +148,10 @@ rescue Phenomenal::Error return nil end return c end - # ==== Private methods ==== # + private def find_simple_context(context) find=nil if !context.kind_of?(Phenomenal::Context) a = contexts.find{|k,v| v.name==context}