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, NotPrepared, RenderError

Constant Summary collapse

VERSION =
"0.7.0".freeze

Instance Method Summary collapse

Instance Method Details

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

Yields:

  • (self.config)


28
29
30
# File 'lib/humid.rb', line 28

def configure
  yield self.config
end

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



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
59
60
61
62
63
64
65
# File 'lib/humid.rb', line 32

def prepare(ctx, options = {})
  return ctx if ctx.respond_to?(:humid_prepared?) && ctx.humid_prepared?

  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 = remove_functions + 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)

  def ctx.humid_prepared?
    true
  end

  ctx
end

#render(ctx, *args) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/humid.rb', line 67

def render(ctx, *args)
  is_prepared = ctx.respond_to?(:humid_prepared?) && ctx.humid_prepared?
  raise Humid::NotPrepared, "Context was not prepared with Humid.prepare" unless is_prepared

  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