Class: FileSet
- Inherits:
-
Object
- Object
- FileSet
- Defined in:
- lib/fileset.rb
Instance Method Summary collapse
- #add_dir(path) ⇒ Object
- #add_file(path, data = nil) ⇒ Object
- #add_item(path, item) ⇒ Object
- #align(string) ⇒ Object
- #define(&block) ⇒ Object
- #dir(name) ⇒ Object
- #file(name, data = nil) ⇒ Object
- #find(path) ⇒ Object
- #get_file(path) ⇒ Object
-
#initialize(search_paths) ⇒ FileSet
constructor
A new instance of FileSet.
- #load(*path) ⇒ Object
- #populate ⇒ Object
- #unpath(parts) ⇒ Object
- #yaml_file(name) ⇒ Object
Constructor Details
#initialize(search_paths) ⇒ FileSet
Returns a new instance of FileSet.
153 154 155 156 157 |
# File 'lib/fileset.rb', line 153 def initialize(search_paths) @prefix = [] @files = {} @search_paths = search_paths.map{|p| unpath([*p])} end |
Instance Method Details
#add_dir(path) ⇒ Object
242 243 244 |
# File 'lib/fileset.rb', line 242 def add_dir(path) add_item(path, FileSetWorks::Directory.new(path)) end |
#add_file(path, data = nil) ⇒ Object
246 247 248 |
# File 'lib/fileset.rb', line 246 def add_file(path, data = nil) add_item(path, FileSetWorks::File.new(path, data)) end |
#add_item(path, item) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/fileset.rb', line 229 def add_item(path, item) if path.length > 1 case @files[path[0..-2]] when FileSetWorks::Directory when nil add_dir(path[0..-2]) else raise "Tried to add items below #{path[0..-2]} which is not a directory" end end @files[path] = item end |
#align(string) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/fileset.rb', line 263 def align(string) lines = string.split(/\n/) first = lines.shift match = /^(\s*)<<</.match(first) unless(match.nil?) catch(:haircut) do return lines.map do |line| raise line if /^#{match[1]}|^\s*$/ !~ line throw :haircut if /^#{match[1]}|^\s*$/ !~ line line.sub(/^#{match[1]}/, "") end.join("\n") end end return string end |
#define(&block) ⇒ Object
225 226 227 |
# File 'lib/fileset.rb', line 225 def define(&block) self.instance_eval &block end |
#dir(name) ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/fileset.rb', line 250 def dir(name) path = @prefix + [name] add_dir(path) @prefix.push(name) yield if block_given? @prefix.pop end |
#file(name, data = nil) ⇒ Object
258 259 260 261 |
# File 'lib/fileset.rb', line 258 def file(name, data=nil) path = @prefix + [name.to_s] add_file(path, data) end |
#find(path) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/fileset.rb', line 159 def find(path) path = unpath(path) @search_paths.reverse.map do |sp| ::File::join(sp + path) end.find do |path| ::File::exists?(path) end end |
#get_file(path) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/fileset.rb', line 172 def get_file(path) path = unpath(path) file_rep = @files[path] unless (file_path = find(path)).nil? file = FileSetWorks::LiveFile.new(file_path, file_rep) file.load return file end return file_rep end |
#load(*path) ⇒ Object
168 169 170 |
# File 'lib/fileset.rb', line 168 def load(*path) get_file(path).contents end |
#populate ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/fileset.rb', line 183 def populate() @files.keys.sort do |left, right| left.length <=> right.length end.each do |key| @files[key].populate(@search_paths) end end |
#unpath(parts) ⇒ Object
191 192 193 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 |
# File 'lib/fileset.rb', line 191 def unpath(parts) if Array === parts and parts.length == 1 parts = parts[0] end case parts when Array if (parts.find{|part| not (String === part or Symbol === part)}.nil?) parts = parts.map{|part| part.to_s} else raise "path must be composed of strings or symbols" end when String parts = path_split(parts) when Symbol parts = [parts.to_s] when ::File parts = parts.path parts = path_split(parts) else raise "path must be String, Array of Strings or File" end parts = parts.map do |part| if /^~/ =~ part ::File::(part).split(::File::Separator) else part end end.flatten return parts end |
#yaml_file(name) ⇒ Object
279 280 281 282 |
# File 'lib/fileset.rb', line 279 def yaml_file(name) path = @prefix + [name.to_s] @files[path] = FileSetWorks::YAMLFile::new(path, yield ) end |