Class: Gloo::WebSvr::EmbeddedRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/web_svr/embedded_renderer.rb

Constant Summary collapse

HELPER =
'helper'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, web_svr_obj) ⇒ EmbeddedRenderer

Set up the web server.



24
25
26
27
28
29
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 24

def initialize( engine, web_svr_obj )
  @engine = engine
  @log = @engine.log

  @web_svr_obj = web_svr_obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Handle a missing method by looking for a helper function. If there is one, then call it and return the result. If not, log an error and return nil.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 104

def method_missing( method_name, *args )
  @log.debug "missing method '#{method_name}' with args #{args}"

  helper_pn = "#{HELPER}.#{method_name}"
  @log.debug "looking for function: #{helper_pn}"

  pn = Gloo::Core::Pn.new( @engine, helper_pn )
  obj = pn.resolve
  if obj
    @log.debug "found obj: #{obj.pn}"
    return obj.invoke args
  else
    @log.error "Function not found: #{helper_pn}"        
  end

  return nil
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



14
15
16
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 14

def engine
  @engine
end

#logObject (readonly)

Returns the value of attribute log.



14
15
16
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 14

def log
  @log
end

#web_svr_objObject (readonly)

Returns the value of attribute web_svr_obj.



14
15
16
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 14

def web_svr_obj
  @web_svr_obj
end

Instance Method Details

#apple_touch_icon_tag(name = 'apple-touch-icon.png', type = 'image/png') ⇒ Object

Render a Apple Touch Icon tag. By default the name is ‘apple-touch-icon.png’ and does not need to be provided if that is the correct file name.



52
53
54
55
56
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 52

def apple_touch_icon_tag( name = 'apple-touch-icon.png', type = 'image/png' )
  icon_path = "/#{Asset::ASSET_FOLDER}/#{Asset::IMAGE_FOLDER}/#{name}"
  published_name = @engine.running_app.obj.asset.published_name( icon_path )
  return "<link rel='apple-touch-icon' type='#{type}' href='#{published_name}' />"
end

#autenticity_token_tagObject

Embed a hidden field with the autenticity token.



89
90
91
92
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 89

def autenticity_token_tag
  session_id = @engine.running_app.obj&.session&.get_session_id
  return Gloo::Objs::CsrfToken.get_csrf_token_hidden_field( session_id )
end

#css_tag(name) ⇒ Object

Render a stylesheet tag for the given stylesheet name.



80
81
82
83
84
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 80

def css_tag( name )
  css_path = "/#{Asset::ASSET_FOLDER}/#{Asset::STYLESHEET_FOLDER}/#{name}"
  published_name = @engine.running_app.obj.asset.published_name( css_path )
  return "<link rel='stylesheet' media='all' href='#{published_name}' />"
end

#favicon_tag(name = 'favicon.ico') ⇒ Object

Render a favicon tag. By default the name is ‘favicon.ico’ and does not need to be provided if that is the correct file name.



41
42
43
44
45
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 41

def favicon_tag( name = 'favicon.ico' )
  icon_path = "/#{Asset::ASSET_FOLDER}/#{Asset::IMAGE_FOLDER}/#{name}"
  published_name = @engine.running_app.obj.asset.published_name( icon_path )
  return "<link rel='shortcut icon' type='image/x-icon' href='#{published_name}' />"
end

#image_tag(img_name, properties = '') ⇒ Object

Render an image tag for the given image name. Include optional proterties as part of the tag.



62
63
64
65
66
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 62

def image_tag( img_name, properties = '' )
  image_path = "/#{Asset::ASSET_FOLDER}/#{Asset::IMAGE_FOLDER}/#{img_name}"
  published_name = @engine.running_app.obj.asset.published_name( image_path )
  return "<image src='#{published_name}' #{properties} />"
end

#js_tag(name) ⇒ Object

Render a script tag for the given script name.



71
72
73
74
75
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 71

def js_tag( name )
  js_path = "/#{Asset::ASSET_FOLDER}/#{Asset::JAVASCRIPT_FOLDER}/#{name}"
  published_name = @engine.running_app.obj.asset.published_name( js_path )
  return "<script src='#{published_name}'></script>"
end

#render(content, params) ⇒ Object

Render content with the given params. Params might be nil, in which case the content is returned with no changes.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 132

def render content, params
  # If the params is nil, let's make it an empty hash.
  params = {} unless params

  # Get the binding context for this render.
  b = binding

  # Add the params to the binding context.
  params.each_pair do |key, value|
    b.local_variable_set key.to_sym, value
  end

  # Render in the current binding content.
  renderer = ERB.new( content )
  content = renderer.result( b )

  return content
end