Class: ApplicationController
- Inherits:
-
Object
- Object
- ApplicationController
- Defined in:
- lib/hengine/application_controller.rb
Instance Attribute Summary collapse
-
#isRenderized ⇒ Object
Returns the value of attribute isRenderized.
Instance Method Summary collapse
- #__render(controllerName, actionName, &block) ⇒ Object
- #_hInit(controllerName, actionName) ⇒ Object
-
#_localVariable(args) ⇒ Object
Questo metodo permette di avere le variabili locali di locals disponibili nel file erb file .rb - es.
- #_readFile(filename) ⇒ Object
- #_render(controllerName, actionName) ⇒ Object
- #_setDefaultLayout(filename) ⇒ Object
- #content_for(layoutName, &block) ⇒ Object
- #execLayout(filename) ⇒ Object
- #herb(filename) ⇒ Object
- #heval(jsonStr) ⇒ Object
-
#initialize ⇒ ApplicationController
constructor
A new instance of ApplicationController.
-
#layout(layout = nil) ⇒ Object
by this method you can set another layout as on rails.
-
#partialFileName(controllerName, actionName) ⇒ Object
il nome dei parziale deve inziare con _ e non deve contenere _view.
-
#render(template: nil, action: nil, file: nil, text: nil, json: nil, layout: false, status: "200 OK", nothing: false, partial: nil, object: nil, locals: nil, collection: nil, spacer_template: nil) ⇒ Object
file .rb - es.
- #templateFileName(controllerName, actionName) ⇒ Object
Constructor Details
#initialize ⇒ ApplicationController
Returns a new instance of ApplicationController.
13 14 15 16 |
# File 'lib/hengine/application_controller.rb', line 13 def initialize @layouts = {} @layout = "application" end |
Instance Attribute Details
#isRenderized ⇒ Object
Returns the value of attribute isRenderized.
11 12 13 |
# File 'lib/hengine/application_controller.rb', line 11 def isRenderized @isRenderized end |
Instance Method Details
#__render(controllerName, actionName, &block) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/hengine/application_controller.rb', line 122 def __render(controllerName, actionName, &block) templateFileName = self.templateFileName(controllerName, actionName) layoutFileName = "./app/views/layouts/#{self.layout}.html.erb" self._setDefaultLayout(templateFileName) self.execLayout(templateFileName) hl << @layouts[:default].call return self.herb(layoutFileName, &block) end |
#_hInit(controllerName, actionName) ⇒ Object
18 19 20 21 |
# File 'lib/hengine/application_controller.rb', line 18 def _hInit(controllerName, actionName) @controllerName = controllerName @actionName = actionName end |
#_localVariable(args) ⇒ Object
Questo metodo permette di avere le variabili locali di locals disponibili nel file erb file .rb
-
es. render(template: “test/index”, layout: ‘application’, locals: ‘herbert’, surname: ‘bonaffini’)
file .html.erb
-
es. <%= “Name: #name, Surname: #href=":surname">locals” %
56 57 58 59 60 61 62 63 64 |
# File 'lib/hengine/application_controller.rb', line 56 def _localVariable(args) return "" unless args str = "" args.each { |key, value| str += "#{key}=heval('#{value.to_json}');"; } hl << "ApplicationController::_localVariable: #{str}" return "<% #{str} %>" end |
#_readFile(filename) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/hengine/application_controller.rb', line 23 def _readFile(filename) file = File.open(filename,'r') content = self._localVariable(@locals) + file.read file.close() hl << content return content end |
#_render(controllerName, actionName) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/hengine/application_controller.rb', line 132 def _render(controllerName, actionName) return self.__render(controllerName, actionName) do |layoutName| if(layoutName) @layouts[layoutName].call else @layouts[:default].call end end end |
#_setDefaultLayout(filename) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/hengine/application_controller.rb', line 94 def _setDefaultLayout(filename) result = self.herb(filename) self.content_for(:default) do result end end |
#content_for(layoutName, &block) ⇒ Object
66 67 68 69 70 |
# File 'lib/hengine/application_controller.rb', line 66 def content_for(layoutName, &block) @layouts[layoutName] = block end |
#execLayout(filename) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hengine/application_controller.rb', line 79 def execLayout(filename) content = self._readFile(filename).gsub(/content_for.*(do|{)/) { |token| "#{token} _herbout=''" } # To more information http://apidock.com/ruby/v1_9_3_392/ERB/Compiler compiler = ERB::Compiler.new("<>") compiler.pre_cmd = ["_herbout=''"] compiler.put_cmd = "_herbout.concat" compiler.insert_cmd = "_herbout.concat" compiler.post_cmd = [""] code, enc = compiler.compile(content) hl << code eval(code) end |
#herb(filename) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/hengine/application_controller.rb', line 72 def herb(filename) content = self._readFile(filename) erb = ERB.new(content) #erb = Erubis::Eruby.new(content) return erb.result(binding()) end |
#heval(jsonStr) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hengine/application_controller.rb', line 31 def heval(jsonStr) # Quando converto un oggetto OpenStruct in json usando l'ambiente IRB ottengo il formato: # "\"#<OpenStruct name=\\\"_herbert_\\\", bonaffini=\\\"_bonaffini_\\\">\"" if(jsonStr.index("#<OpenStruct")) hl << "[application_controllet::heval]#: =========> first case".red osn = eval(jsonStr).gsub(/#<OpenStruct.*>/) { |token| "{#{token.gsub('"', "'").gsub("=", ":").gsub("#<OpenStruct", "")[0..-2]}}" } hash = eval(osn) return OpenStruct.new hash end # Quando converto un oggetto OpenStruct in json usando l'ambiente rails ottengo il formato: # '{"table":{"name":"_herbert_","bonaffini":"_bonaffini_"},"modifiable":true}' if(jsonStr.index('},"modifiable":true}')) hl << "[application_controllet::heval]#: =========> second case".red hash = eval(jsonStr)[:table] return OpenStruct.new hash end return eval(jsonStr) end |
#layout(layout = nil) ⇒ Object
by this method you can set another layout as on rails. It is enough to call layout in your controller example1: layout(“layout_name”) # static example2: layout :determine_layout # dynamic where determine_layout is a method name
106 107 108 109 110 |
# File 'lib/hengine/application_controller.rb', line 106 def layout(layout = nil) @layout = layout if(layout) # it is required to work as a getter and setter return @layout if(@layout.class == String) return eval(@layout.to_s) end |
#partialFileName(controllerName, actionName) ⇒ Object
il nome dei parziale deve inziare con _ e non deve contenere _view
118 119 120 |
# File 'lib/hengine/application_controller.rb', line 118 def partialFileName (controllerName, actionName) return "./app/views/#{controllerName}/#{actionName}.html.erb" end |
#render(template: nil, action: nil, file: nil, text: nil, json: nil, layout: false, status: "200 OK", nothing: false, partial: nil, object: nil, locals: nil, collection: nil, spacer_template: nil) ⇒ Object
file .rb
-
es. render(template: “test/index”, layout: ‘application’, locals: ‘herbert’, surname: ‘bonaffini’)
file .html.erb
-
es. <%= “Name: #name, Surname: #href=":surname">locals” %
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/hengine/application_controller.rb', line 157 def render(template: nil, action: nil, file: nil, text: nil, json: nil, layout: false, status: "200 OK", nothing: false, partial: nil, object: nil, locals: nil, collection: nil, spacer_template: nil) hl << "isRenderized: #{@isRenderized}" @isRenderized = true # <%= render(partial: "article", collection: %w{ ant bee cat dog elk }, spacer_template: 'spacer') %> if partial @locals[partial] = object if object return self.herb(self.partialFileName(@controllerName, "_#{partial}")) unless collection result = "" collection.each do |value| @locals = {} @locals[partial] = value result += self.herb(self.partialFileName(@controllerName, "_#{partial}")) result += self.herb(self.partialFileName(@controllerName, "_#{spacer_template}")) if spacer_template end return result end @locals = locals return "" if nothing @layout = layout if layout.class == String if(file) @file = file return self.render if layout return self.herb(file) end return text if text return json.to_json if json return self._render(template.split('/')[0], template.split('/')[1]) if template return self._render(@controllerName, action) if action return self._render(@controllerName, @actionName) end |
#templateFileName(controllerName, actionName) ⇒ Object
112 113 114 115 |
# File 'lib/hengine/application_controller.rb', line 112 def templateFileName (controllerName, actionName) return @file if(@file) return "./app/views/#{controllerName}/#{actionName}_view.html.erb" end |