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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 41

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

#render(content, params) ⇒ Object

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gloo/web_svr/embedded_renderer.rb', line 69

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