Class: Context::Context
Overview
The Context is the Presenter in the Model, View, Presentor (MVP) model. It is an object that holds the logic for the UI scenario that the application is currently in. Context is an abstract class.
A Context is made up of views, model objects and other sub-contexts. One of the views should be a UI widget container that contains all of the views for the Context. The concrete classes should define the logic for the Context that is either called by enter() or called from one of the contained views.
Note that views are usually only instantiated in createViews, which is called on enter(), not on Context creation. However, there is no requirement for this.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#mainView ⇒ Object
Returns the value of attribute mainView.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#addView(view) ⇒ Object
Adds a view to the mainView Since the mainView is intended to be a UI container that contains all the views, this method is called by a sub-context’s enter() method to allow the parent’s mainView to contain the sub-context’s views.
-
#createViews ⇒ Object
This method should be overriden by the concrete class.
-
#destroyViews ⇒ Object
This method should be overriden by the concrete class.
-
#enter(parent) ⇒ Object
Enters the Context.
-
#exit ⇒ Object
Exits the Context.
-
#getKeyBinding(key) ⇒ Object
Returns the existing binding for a key.
-
#initialize(viewBridge) ⇒ Context
constructor
Create a new Context.
-
#isEntered? ⇒ Boolean
Returns true if the context has been entered, but not exited.
-
#notifyKey(view, key) ⇒ Object
Called by the view to indicated that a key has been pressed.
-
#peekAtView ⇒ Object
Creates views and returns the main View.
-
#setKeyBinding(key, &binding) ⇒ Object
Sets a binding for a key.
-
#setupViews ⇒ Object
This is intended to be private (how do I do that again?) Just so that it doesn’t create a view if it is already created.
Constructor Details
#initialize(viewBridge) ⇒ Context
Create a new Context. Takes a Bridge that is used to create the View s in using the correct namespace.
26 27 28 29 30 31 32 |
# File 'lib/Context/Context.rb', line 26 def initialize(viewBridge) @parent = nil @mainView = nil @viewBridge = viewBridge @keyMap = KeyMap.new @entered = false end |
Instance Attribute Details
#mainView ⇒ Object
Returns the value of attribute mainView.
21 22 23 |
# File 'lib/Context/Context.rb', line 21 def mainView @mainView end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
21 22 23 |
# File 'lib/Context/Context.rb', line 21 def parent @parent end |
Instance Method Details
#addView(view) ⇒ Object
Adds a view to the mainView Since the mainView is intended to be a UI container that contains all the views, this method is called by a sub-context’s enter() method to allow the parent’s mainView to contain the sub-context’s views.
72 73 74 |
# File 'lib/Context/Context.rb', line 72 def addView(view) @mainView.addView(view) unless @mainView.nil? end |
#createViews ⇒ Object
This method should be overriden by the concrete class. It should instantiate all the views and set @mainView
38 39 40 |
# File 'lib/Context/Context.rb', line 38 def createViews # Nothing to do here end |
#destroyViews ⇒ Object
This method should be overriden by the concrete class. It should destroy all the views and set @mainView to nil
63 64 65 |
# File 'lib/Context/Context.rb', line 63 def destroyViews @mainView = nil end |
#enter(parent) ⇒ Object
Enters the Context. After it is called, the Context is then active and can be interacted with. This method automatically calls createViews() and adds the mainView to the parent’s view.
Usually this method will be overriden by the concrete class. However, it should be careful to call super() in the appropriate place.
83 84 85 86 87 88 89 90 |
# File 'lib/Context/Context.rb', line 83 def enter(parent) @parent = parent if (@parent != nil) @entered = true setupViews parent.addView(@mainView) unless @mainView.nil? end end |
#exit ⇒ Object
Exits the Context. After it is called, the context is no longer active and can’t be interacted with. This method automatically removes the mainView from the parent’s view and calls destroyViews().
Usually this method will be overriden by the concrete class. However, it should be careful to call super() in the appropriate place.
106 107 108 109 110 111 112 |
# File 'lib/Context/Context.rb', line 106 def exit() @entered = false if !@parent.nil? && !@parent.mainView.nil? @parent.mainView.removeView(@mainView) unless @mainView.nil? end destroyViews end |
#getKeyBinding(key) ⇒ Object
Returns the existing binding for a key.
Warning: This key binding mechanism is likely to be depracated, or at least heavily modified soon.
131 132 133 134 |
# File 'lib/Context/Context.rb', line 131 def getKeyBinding(key) a = @keyMap.findKey(key) a.unless_nil.action end |
#isEntered? ⇒ Boolean
Returns true if the context has been entered, but not exited. Returns false if the context has never been entered, or if it has been entered and then exited.
95 96 97 |
# File 'lib/Context/Context.rb', line 95 def isEntered? @entered end |
#notifyKey(view, key) ⇒ Object
Called by the view to indicated that a key has been pressed. Runs the binding in the keymap, or returns nil if there isn’t one.
Warning: This key binding mechanism is likely to be depracated, or at least heavily modified soon.
142 143 144 |
# File 'lib/Context/Context.rb', line 142 def notifyKey(view, key) @keyMap.unless_nil do press(key) end end |
#peekAtView ⇒ Object
Creates views and returns the main View. This is intended to be used by test code where entering the context executes code and you need to know what the view will be ahead of time. I can think of no reason to use this in production code.
54 55 56 57 |
# File 'lib/Context/Context.rb', line 54 def peekAtView setupViews @mainView end |
#setKeyBinding(key, &binding) ⇒ Object
Sets a binding for a key. Whenever a key is pressed, the context is notified. This sets up a binding between the key and the block passed in. After that point, the block will be called everytime the key is pressed.
Warning: This key binding mechanism is likely to be depracated, or at least heavily modified soon.
122 123 124 125 |
# File 'lib/Context/Context.rb', line 122 def setKeyBinding(key, &binding) a = KeyAssignment.new(key, &binding) @keyMap.add(a) end |
#setupViews ⇒ Object
This is intended to be private (how do I do that again?) Just so that it doesn’t create a view if it is already created
44 45 46 47 48 |
# File 'lib/Context/Context.rb', line 44 def setupViews if @mainView.nil? createViews end end |