Sha256: 5ca95d17a2978dd9f2e44a0f3c6d510f8d725452d262182c662578b0577072a5

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

require 'impartial/configuration'
require 'impartial/error'

require 'active_support/inflector'
require 'handlebars'

module Impartial
  class Base
    attr_accessor *Configuration::VALID_OPTIONS_KEYS

    def initialize(options={})
      options = Impartial.options.merge(options)
      Configuration::VALID_OPTIONS_KEYS.each do |key|
        send("#{key}=", options[key])
      end
    end

    def render(name, *args)
      cls = Impartial::Base.find_class name
      view = cls.new *args
      path = cls.name.underscore
      shortname = cls.name.demodulize.underscore
      paths.each do |load_path|
        t = File.join(load_path, path, "#{shortname}.html.hbs")
        if File.exist? t
          return Handlebars.compile(File.open(t).read).call(view).to_s
        end
      end
      raise Impartial::Exceptions::TemplateMissing.new
    end

    def self.find_class(name)
      name.to_s.camelize.constantize
    rescue NameError => e
      raise Impartial::Exceptions::ClassNotFound.new e.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
impartial-0.0.1 lib/impartial/base.rb