Class: Phenomenal::Context

Inherits:
Object
  • Object
show all
Extended by:
ContextCreation
Includes:
ContextRelationships
Defined in:
lib/phenomenal/context/context.rb

Overview

Represents a first class context

Direct Known Subclasses

Feature

Constant Summary collapse

@@total_activations =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContextCreation

create

Methods included from ContextRelationships

#implies, #requires, #suggests

Constructor Details

#initialize(name = nil, manager = nil) ⇒ Context

Instance methods



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/phenomenal/context/context.rb', line 12

def initialize(name=nil, manager=nil)
  @manager = manager || Phenomenal::Manager.instance
  @name = name
  @activation_age = 0
  @activation_count = 0
  @adaptations = Array.new
  @manager.register_context(self)
  @parent=nil
  @forgotten=false
  @priority=nil
end

Instance Attribute Details

#activation_ageObject

Returns the value of attribute activation_age.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def activation_age
  @activation_age
end

#activation_countObject

Returns the value of attribute activation_count.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def activation_count
  @activation_count
end

#activation_frequencyObject

Returns the value of attribute activation_frequency.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def activation_frequency
  @activation_frequency
end

#adaptationsObject

Returns the value of attribute adaptations.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def adaptations
  @adaptations
end

#forgottenObject

Returns the value of attribute forgotten.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def forgotten
  @forgotten
end

#managerObject (readonly)

Returns the value of attribute manager.



10
11
12
# File 'lib/phenomenal/context/context.rb', line 10

def manager
  @manager
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/phenomenal/context/context.rb', line 10

def name
  @name
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def parent
  @parent
end

#priorityObject

Returns the value of attribute priority.



8
9
10
# File 'lib/phenomenal/context/context.rb', line 8

def priority
  @priority
end

Instance Method Details

#activateObject

Activate the context



116
117
118
119
120
121
122
123
# File 'lib/phenomenal/context/context.rb', line 116

def activate
  check_validity
  @@total_activations +=1
  self.activation_age =@@total_activations
  self.activation_count = self.activation_count+1
  manager.activate_context(self)
  self
end

#active?Boolean

True if the context is active

Returns:

  • (Boolean)


140
141
142
# File 'lib/phenomenal/context/context.rb', line 140

def active?
  activation_count>0
end

#adapt(method, &block) ⇒ Object

Adapt a method for @current_adapted_class



90
91
92
# File 'lib/phenomenal/context/context.rb', line 90

def adapt(method,&block)
  add_adaptation(@current_adapted_class,method,true,&block)
end

#adapt_class(method, &block) ⇒ Object

Adapt a class method for @current_adapted_class



95
96
97
# File 'lib/phenomenal/context/context.rb', line 95

def adapt_class(method,&block)
  add_adaptation(@current_adapted_class,method,false,&block)
end

#adaptations_for(klass) ⇒ Object

Set the current adapted class for the next adapt calls



85
86
87
# File 'lib/phenomenal/context/context.rb', line 85

def adaptations_for(klass)
  @current_adapted_class = klass
end

#add_adaptation(klass, method_name, instance, umeth = nil, &implementation) ⇒ Object

Add a new method adaptation to the context Return the adaptation just created



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/phenomenal/context/context.rb', line 42

def add_adaptation(klass, method_name,instance,umeth=nil, &implementation)
  if klass.nil? # Not defined class
    raise(Phenomenal::Error,
      "The class to be adapted wasn't specified. Don't forget to use 'adaptations_for(Klass)' before adapting a method"
    )
  end
  if adaptations.find{ |i| i.concern?(klass,method_name,instance) }
   raise(Phenomenal::Error,
      "Illegal duplicated adaptation in context: #{self} for " + 
      "#{klass.name}:#{method_name}."
    )
  else
    adaptation = Phenomenal::Adaptation.new(
      self, klass, method_name,instance, umeth||implementation
    )
    adaptations.push(adaptation)
    manager.register_adaptation(adaptation)
    adaptation
  end
end

#add_adaptations(&block) ⇒ Object

Add multiple adaptations at definition time



78
79
80
81
82
# File 'lib/phenomenal/context/context.rb', line 78

def add_adaptations(&block)
  instance_eval(&block) if block
  @current_adapted_class=nil #Reset adapted class after context closed
  nil
end

#ageObject

Return the activation age of the context: (The age counter minus the age counter when the context was activated for the last time)



157
158
159
# File 'lib/phenomenal/context/context.rb', line 157

def age
  @@total_activations-activation_age
end

#anonymous?Boolean

True if the context is anonymous

Returns:

  • (Boolean)


150
151
152
# File 'lib/phenomenal/context/context.rb', line 150

def anonymous?
  name.nil?
end

#context(context, *contexts, &block) ⇒ Object Also known as: phen_context

Catch nested context calls and transform them in nested contexts creation



64
65
66
67
# File 'lib/phenomenal/context/context.rb', line 64

def context(context,*contexts,&block)
  check_validity
  Phenomenal::Context.create(true,self,self,context,*contexts,&block)
end

#deactivateObject

Deactivate the context



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/phenomenal/context/context.rb', line 126

def deactivate
  check_validity
  was_active = active?
  if self.activation_count>0
    #Deactivation
    self.activation_count =  self.activation_count-1
  end
  if was_active && !active?
    manager.deactivate_context(self)
  end
  self
end

#feature(feature, *features, &block) ⇒ Object Also known as: phen_feature

Catch nested feature calls and transform them in nested contexts creation



71
72
73
74
# File 'lib/phenomenal/context/context.rb', line 71

def feature(feature,*features, &block)
  check_validity
  Phenomenal::Feature.create(true,self,self,feature,*features,&block)
end

#forgetObject

Unregister the context from the context manager, This context shouldn’t be used after. The context has to be inactive before being forgetted TODO handle relationships references



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/phenomenal/context/context.rb', line 28

def forget
  if active?
    raise(Phenomenal::Error,
      "Active context cannot be forgotten"
    )
  else
    manager.unregister_context(self)
    self.forgotten=true
    nil
  end
end

#informationObject

Return context informations:

  • Name

  • List of the adaptations

  • Active state

  • Activation age

  • Activation count



167
168
169
170
171
172
173
174
175
176
# File 'lib/phenomenal/context/context.rb', line 167

def information
  {
    :name=>name,
    :adaptations=>adaptations,
    :active=>active?,
    :age=>age,
    :activation_count=>activation_count,
    :type=>self.class.name
  }
end

#just_activated?Boolean

True if the context has just became active

Returns:

  • (Boolean)


145
146
147
# File 'lib/phenomenal/context/context.rb', line 145

def just_activated?
  activation_count==1
end

#parent_featureObject

Return the closest parent feature of the context



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/phenomenal/context/context.rb', line 179

def parent_feature
  p = parent
  while p!=nil && !p.is_a?(Phenomenal::Feature) do
    p=p.parent
  end
  if p.nil?
    manager.default_feature
  else
    p
  end
end

#remove_adaptation(klass, method_name, instance) ⇒ Object

Remove a method adaptation from the context



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/phenomenal/context/context.rb', line 100

def remove_adaptation(klass,method_name,instance)
  adaptation_index =
    adaptations.find_index{ |i| i.concern?(klass, method_name,instance) }
  if !adaptation_index
    raise(Phenomenal::Error,
      "Illegal deleting of an inexistent adaptation in context: " +
      "#{self} for #{klass.name}.#{method_name})."
    )
  end
  
  adaptation = adaptations.delete_at(adaptation_index)
  manager.unregister_adaptation(adaptation)
  adaptation
end

#set_priority(p) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/phenomenal/context/context.rb', line 204

def set_priority(p)
  self.priority=p
  return unless manager.shared_contexts[self]
  manager.shared_contexts[self].each do |shared_context|
    shared_context.priority=[priority,shared_context.priority].compact.min
  end
end

#to_sObject

String representation of the context



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/phenomenal/context/context.rb', line 192

def to_s
  if name
    name.to_s
  elsif self==manager.default_feature
    "Default feature"
  elsif manager.combined_contexts[self]
     "#{manager.combined_contexts[self].flatten}"
  else
    "Anonymous context"
  end
end