class ApplicationController

Attributes

isRenderized[RW]

Public Class Methods

new() click to toggle source
# File lib/hengine/application_controller.rb, line 13
def initialize
  @layouts = {}
  @layout = "application"
end

Public Instance Methods

__render(controllerName, actionName, &block) click to toggle source
# 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) click to toggle source
# File lib/hengine/application_controller.rb, line 18
def _hInit(controllerName, actionName)
  @controllerName = controllerName
  @actionName = actionName
end
_localVariable(args) click to toggle source

Questo metodo permette di avere le variabili locali di locals disponibili nel file erb file .rb

  • es. render(template: “test/index”, layout: 'application', locals: {name: 'herbert', surname: 'bonaffini'})

file .html.erb

  • es. <%= “Name: #{name}, Surname: #{@locals}” %

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/hengine/application_controller.rb, line 66
def content_for(layoutName, &block)

  @layouts[layoutName] = block

end
execLayout(filename) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source

il nome dei parziale deve inziare con _ e non deve contenere _view

# 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) click to toggle source

file .rb

  • es. render(template: “test/index”, layout: 'application', locals: {name: 'herbert', surname: 'bonaffini'})

file .html.erb

  • es. <%= “Name: #{name}, Surname: #{@locals}” %

# 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) click to toggle source
# 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