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]
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.
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.(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
53 54 55 |
# File 'lib/ore/template/directory.rb', line 53 def dependencies @dependencies end |
#development_dependencies ⇒ Object (readonly)
Development dependencies defined by the template
58 59 60 |
# File 'lib/ore/template/directory.rb', line 58 def development_dependencies @development_dependencies end |
#directories ⇒ Object (readonly)
The directories within the template directory
25 26 27 |
# File 'lib/ore/template/directory.rb', line 25 def directories @directories end |
#disable ⇒ Object (readonly)
Other templates to be disabled
37 38 39 |
# File 'lib/ore/template/directory.rb', line 37 def disable @disable end |
#enable ⇒ Object (readonly)
Other templates to be enabled
40 41 42 |
# File 'lib/ore/template/directory.rb', line 40 def enable @enable end |
#files ⇒ Object (readonly)
The static files in the template directory
28 29 30 |
# File 'lib/ore/template/directory.rb', line 28 def files @files end |
#ignore ⇒ Object (readonly)
Files to ignore
48 49 50 |
# File 'lib/ore/template/directory.rb', line 48 def ignore @ignore end |
#includes ⇒ Object (readonly)
The include templates in the template directory
34 35 36 |
# File 'lib/ore/template/directory.rb', line 34 def includes @includes end |
#path ⇒ Object (readonly)
The path of the template directory
22 23 24 |
# File 'lib/ore/template/directory.rb', line 22 def path @path end |
#templates ⇒ Object (readonly)
The ERb templates in the template directory
31 32 33 |
# File 'lib/ore/template/directory.rb', line 31 def templates @templates end |
#variables ⇒ Object (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.
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.
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.
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.
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.
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
.
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 |