class ErbComponent

Constants

VERSION

Attributes

parent[R]
req[R]

Public Class Methods

call(req, opts = {}) click to toggle source
# File lib/erb_component/erb_component.rb, line 42
def self.call(req, opts = {})
  new(req, **opts).render
end
current_file() click to toggle source
# File lib/erb_component/erb_component.rb, line 54
def self.current_file
  @current_file
end
current_file=(file) click to toggle source
# File lib/erb_component/erb_component.rb, line 50
def self.current_file=(file)
  @current_file = file
end
new(req, opts = {}) click to toggle source
# File lib/erb_component/erb_component.rb, line 16
def initialize(req, opts = {})
  @req = req
  @template = opts[:template]
  begin
    @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(req)
    if @parent && !(parent.template['{{VIEW}}'] || parent.template['{{view}}'])
      @parent = parent.parent
    end
  rescue ArgumentError
  end
end

Public Instance Methods

call() click to toggle source
# File lib/erb_component/erb_component.rb, line 46
def call
  self.render
end
render() click to toggle source
# File lib/erb_component/erb_component.rb, line 28
def render
  str = ERB.new(template).result(binding)
  res = if parent
          parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str)
        else
          str
        end
  if res.respond_to? :html_safe
    res.html_safe
  else
    res
  end
end
template() click to toggle source
# File lib/erb_component/erb_component.rb, line 62
def template
  return @template if @template
  return File.read(template_file_path) if template_file_path
  fail "not found: #{template_file_path}"
end
template_file_path() click to toggle source
# File lib/erb_component/erb_component.rb, line 58
def template_file_path
  self.class.current_file.gsub('.rb', '.erb')
end