Sha256: 818954b920b8338186ca96bda69b5f539dac6e8bd4bc25e500a460ece8b9bb64

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

module Hotcell
  # Base template resolving class for `include` command.
  # Please inherit your own resolvers from it.
  class Resolver
    def template path, context = nil
      cache(path, context) do
        source = resolve path, context
        Template.parse(source)
      end
    end

    # Returns template source
    # Not implemented by default
    def resolve path, context = nil
      raise NotImplementedError, 'Default resolver`s template resolve function is not implemented'
    end

    # Caches parsed template
    # No cache by default
    def cache path, context = nil, &block
      @cache ||= {}
      @cache[path] ||= block.call
    end
  end

  # Basic file system resolver class.
  # Ex:
  #   resolver = Hotcell::FileSystemResolver.new('/Users/username/work/project/app/views')
  #   resolver.template('articles/new') #=> returns Hotcell::Template instance
  #                                     # for `/Users/username/work/project/app/views/articles/new.hc`
  class FileSystemResolver < Resolver
    attr_reader :root

    def initialize root
      @root = root.to_s
    end

    def resolve path, context = nil
      full_path = File.expand_path path, root
      full_path = "#{full_path}.hc"
      File.read(full_path)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hotcell-0.3.0 lib/hotcell/resolver.rb
hotcell-0.2.0 lib/hotcell/resolver.rb
hotcell-0.1.0 lib/hotcell/resolver.rb