class Ore::Template::Directory

Represents a template directory and the static files, ‘.erb` files and sub-directories within it.

Constants

CONFIG_FILE

The template configuration file

IGNORE

Files or directory names to ignore

Attributes

dependencies[R]

Runtime dependencies defined by the template

@since 0.9.0

development_dependencies[R]

Development dependencies defined by the template

@since 0.9.0

directories[R]

The directories within the template directory

disable[R]

Other templates to be disabled

enable[R]

Other templates to be enabled

files[R]

The static files in the template directory

ignore[R]

Files to ignore

@since 0.9.0

includes[R]

The include templates in the template directory

path[R]

The path of the template directory

templates[R]

The ERb templates in the template directory

variables[R]

The variables to use when rendering the template files

Public Class Methods

new(path) click to toggle source

Initializes a new template directory.

@param [String] path

The path to the template directory.
# File lib/ore/template/directory.rb, line 66
def initialize(path)
  @path = File.expand_path(path)

  @directories = []
  @files       = {}
  @templates   = {}
  @includes    = Hash.new { |hash,key| hash[key] = {} }

  @disable = []
  @enable  = []

  @ignore                   = []
  @dependencies             = {}
  @development_dependencies = {}
  @variables                = {}

  load!
  scan!
end

Public Instance Methods

each_directory(&block) click to toggle source

Enumerates through the directories in the template directory.

@yield [path]

The given block will be passed each directory path.

@yieldparam [String] path

The relative path of a directory within the template directory.
# File lib/ore/template/directory.rb, line 95
def each_directory(&block)
  @directories.each(&block)
end
each_file(markup) { |dest, file| ... } click to toggle source

Enumerates through every file in the template directory.

@param [Symbol] markup

The markup to look for.

@yield [path]

The given block will be passed each file path.

@yieldparam [String] path

A relative path of a file within the template directory.
# File lib/ore/template/directory.rb, line 111
def each_file(markup)
  @files.each do |dest,file|
    if (formatted_like?(dest,markup) || !formatted?(dest))
      yield dest, file
    end
  end
end
each_template(markup) { |dest, file| ... } click to toggle source

Enumerates over every template within the template directory.

@param [Symbol] markup

The markup to look for.

@yield [path]

The given block will be passed each template path.

@yieldparam [String] path

A relative path of a template within the template directory.
# File lib/ore/template/directory.rb, line 131
def each_template(markup)
  @templates.each do |dest,file|
    if (formatted_like?(dest,markup) || !formatted?(dest))
      yield dest, file
    end
  end
end

Protected Instance Methods

formatted?(path) click to toggle source

Determines whether a file is markup formatted.

@param [String] path

The path to the file.

@return [Boolean]

Specifies whether the file is formatting.
# File lib/ore/template/directory.rb, line 243
def formatted?(path)
  Markup::EXTS.values.any? { |exts| exts.include?(File.extname(path)) }
end
formatted_like?(path,markup) click to toggle source

Determines if a file has a specific type of markup formatting.

@param [String] path

The path to the file.

@param [Symbol] markup

The specified type of markup.

@return [Boolean]

Specifies whether the file contains the given formatting.
# File lib/ore/template/directory.rb, line 259
def formatted_like?(path,markup)
  Markup::EXTS[markup].include?(File.extname(path))
end
load!() click to toggle source

Loads template configuration information from ‘template.yml`.

@raise [InvalidTemplate]

The `template.yml` file did not contain a YAML Hash.

@since 0.2.0

# File lib/ore/template/directory.rb, line 149
def load!
  config_path = File.join(@path,CONFIG_FILE)
  return false unless File.file?(config_path)

  config = YAML.load_file(config_path)
  return false unless config.kind_of?(Hash)

  @disable = Array(config['disable']).map(&:to_sym)
  @enable  = Array(config['enable']).map(&:to_sym)

  @ignore = Array(config['ignore'])

  case (dependencies = config['dependencies'])
  when Hash
    @dependencies.merge!(dependencies)
  when nil
  else
    raise(InvalidTemplate,"template dependencies must be a Hash: #{config_path.dump}")
  end

  case (dependencies = config['development_dependencies'])
  when Hash
    @development_dependencies.merge!(dependencies)
  when nil
  else
    raise(InvalidTemplate,"template dependencies must be a Hash: #{config_path.dump}")
  end

  case (variables = config['variables'])
  when Hash
    variables.each do |name,value|
      @variables[name.to_sym] = value
    end
  when nil
  else
    raise(InvalidTemplate,"template variables must be a Hash: #{config_path.dump}")
  end

  return true
end
scan!() click to toggle source

Scans the template directory recursively recording the directories, files and partial templates.

# File lib/ore/template/directory.rb, line 194
def scan!
  Dir.chdir(@path) do
    Find.find('.') do |file|
      next if file == '.'

      # ignore the ./
      file = file[2..-1]
      name = File.basename(file)

      # ignore certain files/directories
      Find.prune if IGNORE.include?(name)

      if File.directory?(file)
        @directories << file
      elsif File.file?(file)
        src = File.join(@path,file)

        case File.extname(name)
        when '.erb'
          # erb template
          if name.start_with?('_')
            # partial template
            template_dir  = File.dirname(file)
            template_name = name[1..-1].chomp('.erb').to_sym

            @includes[template_dir][template_name] = src
          else
            dest = file.chomp('.erb')

            @templates[dest] = src
          end
        else
          # static file
          @files[file] = src
        end
      end
    end
  end
end