Class: Ore::Template::Directory
- Inherits:
-
Object
- Object
- Ore::Template::Directory
- 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
-
#dependencies ⇒ Object
readonly
Runtime dependencies defined by the template.
-
#development_dependencies ⇒ Object
readonly
Development dependencies defined by the template.
-
#directories ⇒ Object
readonly
The directories within the template directory.
-
#disable ⇒ Object
readonly
Other templates to be disabled.
-
#enable ⇒ Object
readonly
Other templates to be enabled.
-
#files ⇒ Object
readonly
The static files in the template directory.
-
#ignore ⇒ Object
readonly
Files to ignore.
-
#includes ⇒ Object
readonly
The include templates in the template directory.
-
#path ⇒ Object
readonly
The path of the template directory.
-
#templates ⇒ Object
readonly
The ERb templates in the template directory.
-
#variables ⇒ Object
readonly
The variables to use when rendering the template files.
Instance Method Summary collapse
-
#each_directory {|path| ... } ⇒ Object
Enumerates through the directories in the template directory.
-
#each_file(markup) {|path| ... } ⇒ Object
Enumerates through every file in the template directory.
-
#each_template(markup) {|path| ... } ⇒ Object
Enumerates over every template within the template directory.
-
#formatted?(path) ⇒ Boolean
protected
Determines whether a file is markup formatted.
-
#formatted_like?(path, markup) ⇒ Boolean
protected
Determines if a file has a specific type of markup formatting.
-
#initialize(path) ⇒ Directory
constructor
Initializes a new template directory.
-
#load! ⇒ Object
protected
Loads template configuration information from
template.yml
. -
#scan! ⇒ Object
protected
Scans the template directory recursively recording the directories, files and partial templates.
Constructor Details
#initialize(path) ⇒ Directory
Initializes a new 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.(path) @directories = [] @files = {} @templates = {} @includes = Hash.new { |hash,key| hash[key] = {} } @disable = [] @enable = [] @ignore = [] @dependencies = {} @development_dependencies = {} @variables = {} load! scan! end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Runtime dependencies defined by the template
59 60 61 |
# File 'lib/ore/template/directory.rb', line 59 def dependencies @dependencies end |
#development_dependencies ⇒ Object (readonly)
Development dependencies defined by the template
64 65 66 |
# File 'lib/ore/template/directory.rb', line 64 def development_dependencies @development_dependencies end |
#directories ⇒ Object (readonly)
The directories within the template directory
31 32 33 |
# File 'lib/ore/template/directory.rb', line 31 def directories @directories end |
#disable ⇒ Object (readonly)
Other templates to be disabled
43 44 45 |
# File 'lib/ore/template/directory.rb', line 43 def disable @disable end |
#enable ⇒ Object (readonly)
Other templates to be enabled
46 47 48 |
# File 'lib/ore/template/directory.rb', line 46 def enable @enable end |
#files ⇒ Object (readonly)
The static files in the template directory
34 35 36 |
# File 'lib/ore/template/directory.rb', line 34 def files @files end |
#ignore ⇒ Object (readonly)
Files to ignore
54 55 56 |
# File 'lib/ore/template/directory.rb', line 54 def ignore @ignore end |
#includes ⇒ Object (readonly)
The include templates in the template directory
40 41 42 |
# File 'lib/ore/template/directory.rb', line 40 def includes @includes end |
#path ⇒ Object (readonly)
The path of the template directory
28 29 30 |
# File 'lib/ore/template/directory.rb', line 28 def path @path end |
#templates ⇒ Object (readonly)
The ERb templates in the template directory
37 38 39 |
# File 'lib/ore/template/directory.rb', line 37 def templates @templates end |
#variables ⇒ Object (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.
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.
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.
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.
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.
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
.
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 |