Sha256: a06ec15869799c9d4fa728fa521e960b43487eb56cf2773733413accf1c7347d

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require "erb_component/version"
require 'erb'

class ErbComponent
  class Error < StandardError; end

  attr_reader :params, :path, :parent

  def initialize(opts = {})
    @params = opts[:params] || {}
    @path = opts[:path]
    @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(opts)
  end

  def render
    str = ERB.new(template).result(binding)
    parent ? parent.render.gsub("{{VIEW}}", str) : str
  end

  def self.render(opts = {})
    new(opts).render
  end

  def template
    a = "components/#{self.class.name.underscore}.erb"
    return File.read a if File.exists? a
    fail 'not found'
  end

  def method_missing(m, *args, &block)
    possible_const = "#{self.class}::#{m}"
    clazz = if Kernel.const_defined?(possible_const)
              Kernel.const_get possible_const
            else
              Kernel.const_get m.to_s
            end
    opts = {path: path, params: params}
    opts.merge!(args[0]) if args.size > 0
    component = clazz.new(opts)
    component.render
  rescue
    super
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erb_component-0.1.1 lib/erb_component/erb_component.rb