Module: Streamlined::Renderable

Includes:
Helpers
Defined in:
lib/streamlined/renderable.rb

Defined Under Namespace

Classes: OutputBuffer

Instance Method Summary collapse

Methods included from Helpers

#attribute_segment, #dashed, #html, #html_attributes, #html_map, #text

Instance Method Details

#capture(*args, &block) ⇒ Object



48
49
50
# File 'lib/streamlined/renderable.rb', line 48

def capture(*args, &block)
  helpers ? helpers.capture(*args, &block) : yield(*args)
end

#helpersObject



44
45
46
# File 'lib/streamlined/renderable.rb', line 44

def helpers
  @_view_context
end

#render(item = nil, **options, &block) ⇒ Object

rubocop:disable Metrics



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/streamlined/renderable.rb', line 19

def render(item = nil, **options, &block) # rubocop:disable Metrics
  return @_rbout if !block && options.empty? && item.nil?

  if item.is_a?(Proc) || (block && item.nil?)
    result = item.is_a?(Proc) ? item.to_s : yield
    return result if result.is_a?(OutputBuffer)

    result = text(result) if result.is_a?(String) && !result.html_safe?

    @_rbout ||= OutputBuffer.new
    @_rbout << result.to_s

    return @_rbout
  end

  if item.respond_to?(:render_in)
    result = item.render_in(self, &block)
    result&.to_s&.html_safe
  else
    raise Error, "You must implement a `partial' implementation yourself" unless respond_to?(:partial)

    partial(item, **options, &block)&.html_safe
  end
end

#render_in(view_context, &block) ⇒ Object



14
15
16
17
# File 'lib/streamlined/renderable.rb', line 14

def render_in(view_context, &block)
  @_view_context = view_context
  template(&block).to_s.strip
end