Class: Babl::Rendering::CompiledTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/babl/rendering/compiled_template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, preloader: NoopPreloader, pretty: true) ⇒ CompiledTemplate

Returns a new instance of CompiledTemplate.



8
9
10
11
12
13
14
# File 'lib/babl/rendering/compiled_template.rb', line 8

def initialize(node, preloader: NoopPreloader, pretty: true)
    @node = node
    @dependencies = node.dependencies
    @documentation = node.documentation
    @preloader = preloader
    @pretty = pretty
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



6
7
8
# File 'lib/babl/rendering/compiled_template.rb', line 6

def dependencies
  @dependencies
end

#documentationObject (readonly)

Returns the value of attribute documentation.



6
7
8
# File 'lib/babl/rendering/compiled_template.rb', line 6

def documentation
  @documentation
end

#nodeObject (readonly)

Returns the value of attribute node.



6
7
8
# File 'lib/babl/rendering/compiled_template.rb', line 6

def node
  @node
end

#preloaderObject (readonly)

Returns the value of attribute preloader.



6
7
8
# File 'lib/babl/rendering/compiled_template.rb', line 6

def preloader
  @preloader
end

#prettyObject (readonly)

Returns the value of attribute pretty.



6
7
8
# File 'lib/babl/rendering/compiled_template.rb', line 6

def pretty
  @pretty
end

Instance Method Details

#json(root) ⇒ Object



16
17
18
19
# File 'lib/babl/rendering/compiled_template.rb', line 16

def json(root)
    data = render(root)
    Oj.dump(data, indent: pretty ? 4 : 0, mode: :strict)
end

#render(root) ⇒ Object



21
22
23
24
25
# File 'lib/babl/rendering/compiled_template.rb', line 21

def render(root)
    preloaded_data = preloader.preload([root], dependencies).first
    ctx = Babl::Rendering::Context.new(preloaded_data)
    node.render(ctx)
end