Class: Blocks::BlockRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/blocks/renderers/block_renderer.rb

Class Method Summary collapse

Class Method Details

.render(runtime_context) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/blocks/renderers/block_renderer.rb', line 5

def self.render(runtime_context)
  output_buffer = runtime_context.output_buffer
  render_item = runtime_context.render_item
  # TODO: should convert anything that passes a runtime context back to the user to a normal hash
  if render_item.is_a?(String) || render_item.respond_to?(:to_partial_path)
    output_buffer << PartialRenderer.render(runtime_context)

    # TODO: should the same approach be utilized for Procs & Methods?
    #  Can the capture method both invoke a method and pass a block to it?
  elsif render_item.is_a?(Proc)
    # TODO: should the runtime_context be the first argument?
    args = runtime_context.runtime_args + [runtime_context.to_hash]
    if runtime_context.runtime_block && runtime_context.runtime_block != render_item
      args = args.unshift runtime_context.runtime_block
    end
    output_buffer << runtime_context.capture(*args, &render_item)

  elsif render_item.is_a?(Method)
    # TODO: should the runtime_context be the first argument?
    args = runtime_context.runtime_args + [runtime_context.to_hash]
    if render_item.arity >= 0
      args = args[0, render_item.arity]
    end
    output_buffer << runtime_context.capture do
      render_item.call(*args, &runtime_context.runtime_block)
    end
  end
end