Class: Ore::Template::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/ore/template/directory.rb

Overview

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

Constant Summary collapse

CONFIG_FILE =

The template configuration file

'template.yml'
IGNORE =

Files or directory names to ignore

['.git', CONFIG_FILE]
MARKUPS =

The known markup languages and file extensions

{
  :markdown => %w[.md .markdown],
  :textile  => %w[.tt .textile],
  :rdoc     => %w[.rdoc]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Directory

Initializes a new template directory.

Parameters:

  • path (String)

    The path to the template directory.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ore/template/directory.rb', line 72

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

Instance Attribute Details

#dependenciesObject (readonly)

Runtime dependencies defined by the template

Since:

  • 0.9.0



59
60
61
# File 'lib/ore/template/directory.rb', line 59

def dependencies
  @dependencies
end

#development_dependenciesObject (readonly)

Development dependencies defined by the template

Since:

  • 0.9.0



64
65
66
# File 'lib/ore/template/directory.rb', line 64

def development_dependencies
  @development_dependencies
end

#directoriesObject (readonly)

The directories within the template directory



31
32
33
# File 'lib/ore/template/directory.rb', line 31

def directories
  @directories
end

#disableObject (readonly)

Other templates to be disabled



43
44
45
# File 'lib/ore/template/directory.rb', line 43

def disable
  @disable
end

#enableObject (readonly)

Other templates to be enabled



46
47
48
# File 'lib/ore/template/directory.rb', line 46

def enable
  @enable
end

#filesObject (readonly)

The static files in the template directory



34
35
36
# File 'lib/ore/template/directory.rb', line 34

def files
  @files
end

#ignoreObject (readonly)

Files to ignore

Since:

  • 0.9.0



54
55
56
# File 'lib/ore/template/directory.rb', line 54

def ignore
  @ignore
end

#includesObject (readonly)

The include templates in the template directory



40
41
42
# File 'lib/ore/template/directory.rb', line 40

def includes
  @includes
end

#pathObject (readonly)

The path of the template directory



28
29
30
# File 'lib/ore/template/directory.rb', line 28

def path
  @path
end

#templatesObject (readonly)

The ERb templates in the template directory



37
38
39
# File 'lib/ore/template/directory.rb', line 37

def templates
  @templates
end

#variablesObject (readonly)

The variables to use when rendering the template files



49
50
51
# File 'lib/ore/template/directory.rb', line 49

def variables
  @variables
end

Instance Method Details

#each_directory {|path| ... } ⇒ Object

Enumerates through the directories in the template directory.

Yields:

  • (path)

    The given block will be passed each directory path.

Yield Parameters:

  • path (String)

    The relative path of a directory within the template directory.



101
102
103
# File 'lib/ore/template/directory.rb', line 101

def each_directory(&block)
  @directories.each(&block)
end

#each_file(markup) {|path| ... } ⇒ Object

Enumerates through every file in the template directory.

Parameters:

  • markup (Symbol)

    The markup to look for.

Yields:

  • (path)

    The given block will be passed each file path.

Yield Parameters:

  • path (String)

    A relative path of a file within the template directory.



117
118
119
120
121
122
123
# File 'lib/ore/template/directory.rb', line 117

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) {|path| ... } ⇒ Object

Enumerates over every template within the template directory.

Parameters:

  • markup (Symbol)

    The markup to look for.

Yields:

  • (path)

    The given block will be passed each template path.

Yield Parameters:

  • path (String)

    A relative path of a template within the template directory.



137
138
139
140
141
142
143
# File 'lib/ore/template/directory.rb', line 137

def each_template(markup)
  @templates.each do |dest,file|
    if (formatted_like?(dest,markup) || !formatted?(dest))
      yield dest, file
    end
  end
end

#formatted?(path) ⇒ Boolean (protected)

Determines whether a file is markup formatted.

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (Boolean)

    Specifies whether the file is formatting.



249
250
251
# File 'lib/ore/template/directory.rb', line 249

def formatted?(path)
  MARKUPS.values.any? { |exts| exts.include?(File.extname(path)) }
end

#formatted_like?(path, markup) ⇒ Boolean (protected)

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

Parameters:

  • path (String)

    The path to the file.

  • markup (Symbol)

    The specified type of markup.

Returns:

  • (Boolean)

    Specifies whether the file contains the given formatting.



265
266
267
# File 'lib/ore/template/directory.rb', line 265

def formatted_like?(path,markup)
  MARKUPS[markup].include?(File.extname(path))
end

#load!Object (protected)

Loads template configuration information from template.yml.

Raises:

Since:

  • 0.2.0



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ore/template/directory.rb', line 155

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!Object (protected)

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ore/template/directory.rb', line 200

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