Class: Humid
- Inherits:
-
Object
show all
- Defined in:
- lib/humid.rb,
lib/humid/version.rb,
lib/humid/log_subscriber.rb,
lib/humid/controller_runtime.rb
Defined Under Namespace
Modules: ControllerRuntime
Classes: FileNotFound, LogSubscriber, RenderError
Constant Summary
collapse
- VERSION =
"0.2.0".freeze
- @@context =
nil
Class Method Summary
collapse
Class Method Details
27
28
29
|
# File 'lib/humid.rb', line 27
def configure
yield config
end
|
.context ⇒ Object
55
56
57
|
# File 'lib/humid.rb', line 55
def context
@@context
end
|
.create_context ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/humid.rb', line 66
def create_context
ctx = MiniRacer::Context.new(**config.context_options)
if logger
fmt = config.log_formatter || proc { |_level, message, *_rest| message }
ctx.attach("console.log", proc { |*args| logger.debug(fmt.call(:debug, *args)) })
ctx.attach("console.info", proc { |*args| logger.info(fmt.call(:info, *args)) })
ctx.attach("console.error", proc { |*args| logger.error(fmt.call(:error, *args)) })
ctx.attach("console.warn", proc { |*args| logger.warn(fmt.call(:warn, *args)) })
end
js = ""
js << remove_functions
js << renderer
ctx.eval(js)
source_path = config.application_path
map_path = config.source_map_path
if map_path
ctx.attach("readSourceMap", proc { File.read(map_path) })
end
filename = File.basename(source_path.to_s)
@@current_filename = filename
ctx.eval(File.read(source_path), filename: filename)
@@context = ctx
end
|
.dispose ⇒ Object
59
60
61
62
63
64
|
# File 'lib/humid.rb', line 59
def dispose
if @@context
@@context.dispose
@@context = nil
end
end
|
.logger ⇒ Object
42
43
44
|
# File 'lib/humid.rb', line 42
def logger
config.logger
end
|
.remove_functions ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/humid.rb', line 31
def remove_functions
<<~JS
delete this.setTimeout;
delete this.setInterval;
delete this.clearTimeout;
delete this.clearInterval;
delete this.setImmediate;
delete this.clearImmediate;
JS
end
|
.render(*args) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/humid.rb', line 96
def render(*args)
ActiveSupport::Notifications.instrument("render.humid") do
context.call("__renderer", *args)
rescue MiniRacer::RuntimeError => e
message = ([e.message] + e.backtrace.filter { |x| x.starts_with? "JavaScript" }).join("\n")
render_error = Humid::RenderError.new(message)
if config.raise_render_errors
raise render_error
else
config.logger.error(render_error.inspect)
""
end
end
end
|
.renderer ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/humid.rb', line 46
def renderer
<<~JS
var __renderer;
function setHumidRenderer(fn) {
__renderer = fn;
}
JS
end
|