Class: Context::Context

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

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

Gtk::App

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



24
25
26
27
28
29
30
# File 'lib/Context/Context.rb', line 24

def initialize(viewBridge)
	@parent = nil
	@mainView = nil
	@viewBridge = viewBridge
	@entered = false
          @onExitBlock = nil
end

Instance Attribute Details

#mainViewObject

Returns the value of attribute mainView.



19
20
21
# File 'lib/Context/Context.rb', line 19

def mainView
  @mainView
end

#parentObject (readonly)

Returns the value of attribute parent.



19
20
21
# File 'lib/Context/Context.rb', line 19

def parent
  @parent
end

#viewBridgeObject (readonly)

Returns the value of attribute viewBridge.



19
20
21
# File 'lib/Context/Context.rb', line 19

def viewBridge
  @viewBridge
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.



70
71
72
# File 'lib/Context/Context.rb', line 70

def addView(view)
	@mainView.addView(view) unless @mainView.nil?
end

#createViewsObject

This method should be overriden by the concrete class. It should instantiate all the views and set @mainView



36
37
38
# File 'lib/Context/Context.rb', line 36

def createViews
    # Nothing to do here
end

#destroyViewsObject

This method should be overriden by the concrete class. It should destroy all the views and set @mainView to nil



61
62
63
# File 'lib/Context/Context.rb', line 61

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.



81
82
83
84
85
86
87
88
# File 'lib/Context/Context.rb', line 81

def enter(parent)
	@parent = parent
	if (@parent != nil)
	    @entered = true
	    setupViews
		parent.addView(@mainView) unless @mainView.nil?
	end
end

#exitObject

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.



113
114
115
116
117
118
119
120
121
122
# File 'lib/Context/Context.rb', line 113

def exit()
    @entered = false
          if !@parent.nil? && !@parent.mainView.nil?
              @parent.mainView.removeView(@mainView) unless @mainView.nil?
          end
          destroyViews
          if !@onExitBlock.nil?
              @onExitBlock.call
          end
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.

Returns:

  • (Boolean)


93
94
95
# File 'lib/Context/Context.rb', line 93

def isEntered?
    @entered
end

#onExit(&block) ⇒ Object

Set a block to be called when the context exits. Often a context is exited by the result of some UI activity at some unknown point in the future. The caller of the context may want to do something after the context exits. This block will be called at the end of the exit method.



102
103
104
# File 'lib/Context/Context.rb', line 102

def onExit(&block)
    @onExitBlock = block
end

#peekAtViewObject

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.



52
53
54
55
# File 'lib/Context/Context.rb', line 52

def peekAtView
    setupViews
    @mainView
end

#setupViewsObject

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



42
43
44
45
46
# File 'lib/Context/Context.rb', line 42

def setupViews
    if @mainView.nil?
        createViews
    end
end