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]

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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Attribute Details

#dependenciesObject (readonly)

Runtime dependencies defined by the template

Since:

  • 0.9.0



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

def dependencies
  @dependencies
end

#development_dependenciesObject (readonly)

Development dependencies defined by the template

Since:

  • 0.9.0



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

def development_dependencies
  @development_dependencies
end

#directoriesObject (readonly)

The directories within the template directory



25
26
27
# File 'lib/ore/template/directory.rb', line 25

def directories
  @directories
end

#disableObject (readonly)

Other templates to be disabled



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

def disable
  @disable
end

#enableObject (readonly)

Other templates to be enabled



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

def enable
  @enable
end

#filesObject (readonly)

The static files in the template directory



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

def files
  @files
end

#ignoreObject (readonly)

Files to ignore

Since:

  • 0.9.0



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

def ignore
  @ignore
end

#includesObject (readonly)

The include templates in the template directory



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

def includes
  @includes
end

#pathObject (readonly)

The path of the template directory



22
23
24
# File 'lib/ore/template/directory.rb', line 22

def path
  @path
end

#templatesObject (readonly)

The ERb templates in the template directory



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

def templates
  @templates
end

#variablesObject (readonly)

The variables to use when rendering the template files



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

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.



95
96
97
# File 'lib/ore/template/directory.rb', line 95

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.



111
112
113
114
115
116
117
# 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) {|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.



131
132
133
134
135
136
137
# 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

#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.



243
244
245
# 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) ⇒ 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.



259
260
261
# File 'lib/ore/template/directory.rb', line 259

def formatted_like?(path,markup)
  Markup::EXTS[markup].include?(File.extname(path))
end

#load!Object (protected)

Loads template configuration information from template.yml.

Raises:

Since:

  • 0.2.0



149
150
151
152
153
154
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
# 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!Object (protected)

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



194
195
196
197
198
199
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
# 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