Class: Fmt::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/fmt/renderer.rb

Overview

Renders templates to a formatted string

Constant Summary collapse

PIPELINE_START =

: Regexp – detects start of first pipeline

Regexp.new("(?=%s)" % [Sigils::FORMAT_PREFIX]).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Renderer

Constructor



12
13
14
# File 'lib/fmt/renderer.rb', line 12

def initialize(template)
  @template = template
end

Instance Attribute Details

#templateObject (readonly)

: Template



16
17
18
# File 'lib/fmt/renderer.rb', line 16

def template
  @template
end

Instance Method Details

#render(*args, **kwargs) ⇒ Object

Note:

Positional and Keyword arguments are mutually exclusive

Renders the template to a string

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fmt/renderer.rb', line 23

def render(*args, **kwargs)
  raise Error, "positional and keyword arguments are mutually exclusive" if args.any? && kwargs.any?

  render_embeds(*args, **kwargs) do |embed, result|
    kwargs[embed.key] = result
  end

  rendered = template.source
  render_pipelines(*args, **kwargs) do |pipeline, result|
    rendered = rendered.sub(pipeline.urtext, result.to_s)
  end
  rendered
end