Class: Crumpet::Renderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper, ERB::Util
Defined in:
lib/crumpet/renderer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crumbs, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



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

def initialize(crumbs, options = {})
  @crumbs  = crumbs
  @options = options
end

Instance Attribute Details

#crumbsObject (readonly)

Returns the value of attribute crumbs.



6
7
8
# File 'lib/crumpet/renderer.rb', line 6

def crumbs
  @crumbs
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/crumpet/renderer.rb', line 6

def options
  @options
end

Class Method Details

.render(crumbs, options = {}) ⇒ Object



8
9
10
# File 'lib/crumpet/renderer.rb', line 8

def self.render(crumbs, options = {})
  new(crumbs, options).render
end

Instance Method Details

#renderObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/crumpet/renderer.rb', line 17

def render
  return '' if crumbs.empty? && !option_or_default(:render_when_blank)

  case option_or_default(:format)
  when :html
    output = crumbs.map{ |crumb| render_html(crumb) }.join(option_or_default(:separator)).html_safe
    output = (option_or_default(:container).to_sym, output, build_container_options) if option_or_default(:container).present?
    output
  when :xml
    output = crumbs.map{ |crumb| render_xml(crumb) }.join
    output = (:crumbs, output)
    output
  when :json
    crumbs.map{ |crumb| render_json(crumb) }.to_json
  else
    raise NotImplementedError, "unsupported format: #{option_or_default(:format)}"
  end
end