Module: Humid

Extended by:
Humid
Included in:
Humid
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.5.0".freeze

Instance Method Summary collapse

Instance Method Details

#configure {|self.config| ... } ⇒ Object

Yields:

  • (self.config)


25
26
27
# File 'lib/humid.rb', line 25

def configure
  yield self.config
end

#prepare(ctx, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/humid.rb', line 29

def prepare(ctx, options = {})
  effective_config = config.merge(options)
  logger = effective_config.logger
  log_formatter = effective_config.log_formatter

  if logger
    fmt = 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 = effective_config.application_path
  map_path = effective_config.source_map_path

  if map_path
    ctx.attach("readSourceMap", proc { File.read(map_path) })
  end

  filename = File.basename(source_path.to_s)
  ctx.eval(File.read(source_path), filename: filename)

  ctx
end

#render(ctx, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/humid.rb', line 60

def render(ctx, *args)
  ActiveSupport::Notifications.instrument("render.humid") do
    ctx.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