Class: Controller

Inherits:
Object
  • Object
show all
Defined in:
app/core/Controller.rb

Overview

Controls the data flow into an item object and updates the view whenever data changes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Initialisation



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/core/Controller.rb', line 13

def initialize ()

	@title       = "MyApp"
	@width       = 900
	@height      = 500
	@borderWidth = 0
	@resizable   = true
	@position 	 = "CENTER"

	## Create content variable sent from controller to view called
	@content = Hash.new(0)

	if Core::DEBUG
		puts "Main controller instanciation"
	end

end

Class Method Details

.inherited(subclass) ⇒ Object

Invoke methods when inherited

Parameters:

  • subclass

    The subclass

Returns:

  • Itself



38
39
40
41
42
# File 'app/core/Controller.rb', line 38

def self.inherited(subclass)
	super

	return self
end

Instance Method Details

#loadFile(filePath, debugInfo) ⇒ Object

Loads a file.

Parameters:

  • filePath

    The file path

  • debugInfo

    The debug information

Returns:

  • Itself



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/core/Controller.rb', line 52

def loadFile(filePath, debugInfo)
	begin
		require filePath
	rescue LoadError
		
		if Core::DEBUG
			puts debugInfo + ": " + File.basename(filePath) + " not found in " + Core::ROOT + debugInfo.downcase
		end

		exit(1) 
	end

	return self
end

#loadModel(name) ⇒ Object

Loads a model.

Parameters:

  • name

    The model to load

Returns:

  • Model instance



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/core/Controller.rb', line 150

def loadModel(name)
	
	filePath = Core::modelPath(name)

	self.loadFile(filePath, "Model")

	## Will retrieve class constant name for dynamic instanciation
	modelName = Object.const_get(name)

	## Make an only one instance of model (Singleton pattern)
	## to ensure date integrity.
	model = modelName.instance()

	self.instance_variable_set("@" + name, model)

	return model

end

#render(name, **args) ⇒ Object

Render view called by controller

Parameters:

  • name

    The view to render



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/core/Controller.rb', line 72

def render(name, **args)
	
	if Core::DEBUG
		puts "Loading view..."
	end

	name = Core::VIEW + name

	filePath = Core::viewPath(name)

	self.loadFile(filePath, "View")

	## Will retrieve class constant name for dynamic instanciation
	viewName = Object.const_get(name)

	view = viewName.new()

	## Force children controller and view
	## to run parent initialize if overriden.
	Core::forceParentInit(self)	
	Core::forceParentInit(view)

	## Set values sent from previous view in
	## content of current view.
	args.each{
		|key, value|
		@content[key.to_s] = value
	}

	self.set_properties(view)

	## Collect content from controller and send it to view
	view.controller = self
	view.controller.run()
	view.content    = @content.clone()

	## Refer controller methods in view for easier
	## call.
	self.class.instance_methods(false).each() do |method|
		if !view.class.method_defined?(method)
			view.define_singleton_method(method) do |*arguments|
				self.controller.send(method, *arguments)
			end
		end
	end

	## Will render view with content retrieved in controller
	view.setInstanceVars()
	view.run()

	Fenetre::css(:priorite => "PRIORITY_APPLICATION")

	## Display content builded in view with Gtk
	view.window.show_all
end

#runObject

Invoke all method in controller for collecting contents to send on view.

Returns:

  • itself



173
174
175
176
177
178
# File 'app/core/Controller.rb', line 173

def run()
	if Core::DEBUG
		raise "Controller #{self.class.name} can't collect content because run method is not redefined."
	end
	return self
end

#set_properties(view) ⇒ Object

Sets the properties of window



131
132
133
134
135
136
137
138
139
140
141
# File 'app/core/Controller.rb', line 131

def set_properties(view)
	## Set window properties
   	
   	view.headerBar.title = @title
   	view.window.set_titlebar (view.headerBar)
   	view.window.set_size_request(@width, @height)
   	view.window.border_width = @borderWidth
   	view.window.set_resizable(@resizable)
   	view.window.set_window_position(Object.const_get("Gtk::WindowPosition::" + @position))
   	
end