Sha256: 4227dcca486c1607a77adc4da76b21975a1b6c0ca9eece456562b8d1f6e80d15

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

require 'sanford/logger'
require 'sanford/template_engine'

module Sanford

  class TemplateSource

    DISALLOWED_ENGINE_EXTS = [ 'rb' ]

    DisallowedEngineExtError = Class.new(ArgumentError)

    attr_reader :path, :engines

    def initialize(path, logger = nil)
      @path = path.to_s
      @default_opts = {
        'source_path' => @path,
        'logger'      => logger || Sanford::NullLogger.new
      }
      @engines = Hash.new{ |h,k| Sanford::NullTemplateEngine.new(@default_opts) }
    end

    def engine(input_ext, engine_class, registered_opts = nil)
      if DISALLOWED_ENGINE_EXTS.include?(input_ext)
        raise DisallowedEngineExtError, "`#{input_ext}` is disallowed as an"\
                                        " engine extension."
      end
      engine_opts = @default_opts.merge(registered_opts || {})
      @engines[input_ext.to_s] = engine_class.new(engine_opts)
    end

    def engine_for?(ext)
      @engines.keys.include?(ext)
    end

    def engine_for_template?(template_name)
      self.engine_for?(get_template_ext(template_name))
    end

    def render(template_path, service_handler, locals)
      engine = @engines[get_template_ext(template_path)]
      engine.render(template_path, service_handler, locals)
    end

    private

    def get_template_ext(template_path)
      files = Dir.glob("#{File.join(@path, template_path.to_s)}.*")
      files = files.reject{ |p| !@engines.keys.include?(parse_ext(p)) }
      parse_ext(files.first.to_s || '')
    end

    def parse_ext(template_path)
      File.extname(template_path)[1..-1]
    end

  end

  class NullTemplateSource < TemplateSource

    def initialize
      super('')
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sanford-0.16.0 lib/sanford/template_source.rb
sanford-0.15.1 lib/sanford/template_source.rb
sanford-0.15.0 lib/sanford/template_source.rb
sanford-0.14.0 lib/sanford/template_source.rb
sanford-0.13.0 lib/sanford/template_source.rb