Sha256: 8d4bc74d091d58cf874e20e6e2a1252cce520ee0270ca21fdad1ae880d377fc6

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'alephant/logger'
require 'mustache'
require 'i18n'

module Alephant
  module Renderer
    class Mustache
      include ::Alephant::Logger

      attr_reader :template_file

      def initialize(template_file, base_path, model)
        @template_file = template_file
        @base_path     = base_path
        @model         = model

        load_translations_from base_path

        logger.info("Renderer.initialize: end with @base_path set to #{@base_path}")
      end

      def render
        logger.info("Renderer.render: rendered template #{template_file}")
        ::Mustache.render(template, @model)
      end

      def template
        template_location = File.join(@base_path, 'templates', "#{template_file}.mustache")

        begin
          logger.info("Renderer.template: #{template_location}")
          File.open(template_location).read
        rescue Exception => e
          logger.error("Renderer.template: view template #{template_file} not found")
        end
      end

      private

      def load_translations_from(base_path)
        if I18n.load_path.empty?
          I18n.config.enforce_available_locales = false
          I18n.load_path << i18n_load_path_from(base_path)
          I18n.backend.load_translations
        end
      end

      def i18n_load_path_from(base_path)
        Dir[
          File.join(
            Pathname.new(base_path).parent,
            'locale',
            '*.yml')
        ]
        .flatten
        .uniq
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-renderer-0.0.6 lib/alephant/renderer/engines/mustache.rb