class Kojo::Collection

The Collection class is a wrapper around the {Template} object. It provides a mechanism for processing an entire directory of templates.

Attributes

dir[R]
import_base[RW]

Public Class Methods

new(dir) click to toggle source
# File lib/kojo/collection.rb, line 10
def initialize(dir)
  @dir = dir
  @import_base = dir
end

Public Instance Methods

render(args = {}, &block) click to toggle source
# File lib/kojo/collection.rb, line 15
def render(args = {}, &block)
  files.each do |file|
    handle file, args, &block
  end
end
size() click to toggle source
# File lib/kojo/collection.rb, line 21
def size
  files.size
end

Private Instance Methods

files() click to toggle source
# File lib/kojo/collection.rb, line 36
def files
  return @files if @files
  raise Kojo::NotFoundError, "Directory not found: #{dir}" unless Dir.exist? dir
  raise Kojo::NotFoundError, "Directory is empty: #{dir}" if Dir.empty? dir

  @files = Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH)
    .reject { |f| File.directory? f }
    .sort
end
handle(file, args = {}) { |path, render| ... } click to toggle source
# File lib/kojo/collection.rb, line 27
def handle(file, args = {})
  template = Template.new file
  template.import_base = import_base

  path = file.sub(/#{dir}\//, '').resolve args

  yield path, template.render(args)
end