lib/overapp/files.rb in overapp-0.5.0 vs lib/overapp/files.rb in overapp-0.5.1
- old
+ new
@@ -9,39 +9,48 @@
end
end
class Files
include FromHash
include Enumerable
- fattr(:files) { [] }
+ fattr(:files) { {} }
fattr(:file_class) { TemplateFile }
def add(ops)
- files << file_class.new(:path => ops[:file], :full_body => ops[:body])
+ ops = file_class.new(:path => ops[:file], :full_body => ops[:body]) if ops.kind_of?(Hash)
+ self.files[ops.path] = ops
end
+ def delete(path)
+ self.files.delete(path)
+ end
+
def size
files.size
end
+
def apply(on_top,ops={})
- res = files.clone
+ res = self.class.new(:files => files.clone)
on_top.each do |top_file|
- if ops[:vars] && ops[:vars].size > 0
- #raise ops[:vars].inspect
- top_file.vars = ops[:vars]
- end
- existing = res.find { |x| x.path == top_file.path }
- if existing
- res -= [existing]
- new_file = top_file.combined(existing)
- res << new_file if new_file
- elsif top_file.has_note?
- raise MissingBaseFileError.new(:top_file => top_file, :base => self)
- else
- res << top_file.parsed
- end
+ res.apply_file(top_file,ops)
end
- self.class.new(:files => res)
+ res
end
+
+ def apply_file(top_file,ops={})
+ if ops[:vars] && ops[:vars].size > 0
+ top_file.vars = ops[:vars]
+ end
+ existing = files[top_file.path]
+ raise MissingBaseFileError.new(:top_file => top_file, :base => self) if !existing && top_file.has_note?
+ new_file = top_file.combined(existing)
+ if new_file
+ add new_file
+ else
+ delete top_file.path
+ end
+ end
+
+
def each(&b)
- files.each(&b)
+ files.values.each(&b)
end
def write_to!(dir)
each do |f|
f.write_to! dir
\ No newline at end of file