Class: WebSvr::EmbeddedRenderer
- Inherits:
-
Object
- Object
- WebSvr::EmbeddedRenderer
- Defined in:
- lib/web_svr/embedded_renderer.rb
Constant Summary collapse
- HELPER =
'helper'.freeze
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#web_svr_obj ⇒ Object
readonly
Returns the value of attribute web_svr_obj.
Instance Method Summary collapse
-
#apple_touch_icon_tag(name = 'apple-touch-icon.png', type = 'image/png') ⇒ Object
Render a Apple Touch Icon tag.
-
#autenticity_token_tag ⇒ Object
Embed a hidden field with the autenticity token.
-
#css_tag(name) ⇒ Object
Render a stylesheet tag for the given stylesheet name.
-
#favicon_tag(name = 'favicon.ico') ⇒ Object
Render a favicon tag.
-
#image_tag(img_name, properties = '') ⇒ Object
Render an image tag for the given image name.
-
#initialize(engine, web_svr_obj) ⇒ EmbeddedRenderer
constructor
Set up the web server.
-
#js_tag(name) ⇒ Object
Render a script tag for the given script name.
-
#method_missing(method_name, *args) ⇒ Object
Handle a missing method by looking for a helper function.
-
#render(content, params) ⇒ Object
Render content with the given params.
Constructor Details
#initialize(engine, web_svr_obj) ⇒ EmbeddedRenderer
Set up the web server.
23 24 25 26 27 28 |
# File 'lib/web_svr/embedded_renderer.rb', line 23 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/web_svr/embedded_renderer.rb', line 103 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
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
13 14 15 |
# File 'lib/web_svr/embedded_renderer.rb', line 13 def engine @engine end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
13 14 15 |
# File 'lib/web_svr/embedded_renderer.rb', line 13 def log @log end |
#web_svr_obj ⇒ Object (readonly)
Returns the value of attribute web_svr_obj.
13 14 15 |
# File 'lib/web_svr/embedded_renderer.rb', line 13 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.
51 52 53 54 55 |
# File 'lib/web_svr/embedded_renderer.rb', line 51 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_tag ⇒ Object
Embed a hidden field with the autenticity token.
88 89 90 91 |
# File 'lib/web_svr/embedded_renderer.rb', line 88 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.
79 80 81 82 83 |
# File 'lib/web_svr/embedded_renderer.rb', line 79 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.
40 41 42 43 44 |
# File 'lib/web_svr/embedded_renderer.rb', line 40 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.
61 62 63 64 65 |
# File 'lib/web_svr/embedded_renderer.rb', line 61 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.
70 71 72 73 74 |
# File 'lib/web_svr/embedded_renderer.rb', line 70 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.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/web_svr/embedded_renderer.rb', line 131 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 |