Skip to content

Commit

Permalink
removing warning when overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
al6x committed Aug 26, 2011
1 parent 84584eb commit 0df835e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 196 deletions.
49 changes: 6 additions & 43 deletions lib/vfs/entries/dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ def create options = {}
entry = self.entry
attrs = entry.get
if attrs and attrs[:file] #entry.exist?
if options[:override]
entry.destroy
else
raise Error, "entry #{self} already exist!"
end
entry.destroy
elsif attrs and attrs[:dir]
# dir already exist, no need to recreate it
return self
Expand All @@ -57,36 +53,9 @@ def create options = {}
end
self
end
def create! options = {}
options[:override] = true
create options
end

def destroy options = {}
storage.open do |fs|
begin
fs.delete_dir path
rescue StandardError => e
attrs = get
if attrs and attrs[:file]
if options[:force]
file.destroy
else
raise Error, "can't destroy File #{dir} (You are trying to destroy it as if it's a Dir)"
end
elsif attrs and attrs[:dir]
# unknown internal error
raise e
else
# do nothing, file already not exist
end
end
end
self
end
def destroy! options = {}
options[:force] = true
destroy options
destroy_entry :dir, :file
end


Expand Down Expand Up @@ -153,7 +122,10 @@ def include? name
alias_method :has?, :include?

def empty?
entries.empty?
catch :break do
entries{|e| throw :break, false}
true
end
end


Expand All @@ -167,7 +139,6 @@ def copy_to to, options = {}
raise Error, "you can't copy to itself" if self == to

target = if to.is_a? File
raise Error, "can't copy Dir to File ('#{self}')!" unless options[:override]
to.dir
elsif to.is_a? Dir
to.dir #(name)
Expand All @@ -183,20 +154,12 @@ def copy_to to, options = {}

target
end
def copy_to! to, options = {}
options[:override] = true
copy_to to, options
end

def move_to to, options = {}
copy_to to, options
destroy options
to
end
def move_to! to, options = {}
options[:override] = true
move_to to, options
end

# class << self
# attr_accessor :dont_use_efficient_dir_copy
Expand Down
20 changes: 20 additions & 0 deletions lib/vfs/entries/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,25 @@ def eql? other
return false unless other.class == self.class
storage.eql?(other.storage) and path.eql?(other.path)
end

protected
def destroy_entry first = :file, second = :dir
storage.open do |fs|
begin
fs.send :"delete_#{first}", path
rescue StandardError => e
attrs = get
if attrs and attrs[first]
# some unknown error
raise e
elsif attrs and attrs[second]
fs.send :"delete_#{second}", path
else
# do nothing, entry already not exist
end
end
end
self
end
end
end
92 changes: 26 additions & 66 deletions lib/vfs/entries/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ def read options = {}, &block
end
end

def content options = {}
read options
end
# def content options = {}
# read options
# end

def create options = {}
write '', options
self
end
def create! options = {}
options[:override] = true
create options
end

def write *args, &block
if block
Expand All @@ -62,18 +58,18 @@ def write *args, &block
raise "can't do :override and :append at the same time!" if options[:override] and options[:append]

storage.open do |fs|
# TODO2 Performance lost, extra call to check file existence
# We need to check if the file exist before writing to it, otherwise it's
# impossible to distinguish if the StandardError caused by the 'already exist' error or
# some other error.
entry = self.entry
if entry.exist?
if options[:override]
entry.destroy
else
raise Error, "entry #{self} already exist!"
end
end
# # TODO2 Performance lost, extra call to check file existence
# # We need to check if the file exist before writing to it, otherwise it's
# # impossible to distinguish if the StandardError caused by the 'already exist' error or
# # some other error.
# entry = self.entry
# if entry.exist?
# if options[:override]
# entry.destroy
# else
# raise Error, "entry #{self} already exist!"
# end
# end

try = 0
begin
Expand All @@ -85,23 +81,20 @@ def write *args, &block
end
rescue StandardError => error
parent = self.parent
if parent.exist?
# some unknown error
raise error
else
if entry.exist?
entry.destroy
elsif !parent.exist?
parent.create(options)
else
# unknown error
raise error
end

try < 2 ? retry : raise(error)
end
end
self
end
def write! *args, &block
args << {} unless args.last.is_a? Hash
args.last[:override] = true
write *args, &block
end

def append *args, &block
options = (args.last.is_a?(Hash) && args.pop) || {}
Expand All @@ -110,37 +103,12 @@ def append *args, &block
end

def update options = {}, &block
options[:override] = true
data = read options
write block.call(data), options
end

def destroy options = {}
storage.open do |fs|
begin
fs.delete_file path
self
rescue StandardError => e
attrs = get
if attrs and attrs[:dir]
if options[:force]
dir.destroy
else
raise Error, "can't destroy Dir #{dir} (you are trying to destroy it as if it's a File)"
end
elsif attrs and attrs[:file]
# unknown internal error
raise e
else
# do nothing, file already not exist
end
end
end
self
end
def destroy! options = {}
options[:force] = true
destroy options
def destroy
destroy_entry
end


Expand All @@ -166,20 +134,12 @@ def copy_to to, options = {}

target
end
def copy_to! to, options = {}
options[:override] = true
copy_to to, options
end

def move_to to, options = {}
copy_to to, options
destroy options
def move_to to
copy_to to
destroy
to
end
def move_to! to, options = {}
options[:override] = true
move_to to, options
end


#
Expand Down
10 changes: 2 additions & 8 deletions lib/vfs/entries/universal_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ def exist?
#
# CRUD
#
def destroy options = {}
storage.open do |fs|
attrs = get
fs.delete_dir path if attrs and attrs[:dir]
fs.delete_file path if attrs and attrs[:file]
end
self
def destroy
destroy_entry
end
alias_method :destroy!, :destroy
end
end
4 changes: 2 additions & 2 deletions lib/vfs/storages/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def write_file original_path, append, &block
path = with_root original_path

# TODO2 Performance lost, extra call to check file existence
raise "can't write, entry #{original_path} already exist!" if !append and ::File.exist?(path)
# raise "can't write, entry #{original_path} already exist!" if !append and ::File.exist?(path)

option = append ? 'a' : 'w'
::File.open path, option do |out|
Expand Down Expand Up @@ -89,7 +89,7 @@ def delete_dir original_path
path = with_root original_path

# TODO2 Performance lost, extra call to check file existence
raise "can't delete file (#{original_path})!" if ::File.file?(path)
# raise "can't delete file (#{original_path})!" if ::File.file?(path)

::FileUtils.rm_r path
end
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ projects['cool_app'].copy_to deploy_dir

dbc = deploy_dir.file('config/database.yml') # <= the 'config' dir not exist yet
dbc.write("user: root\npassword: secret") # <= now the 'database.yml' and parent 'config' has been created
dbc.content =~ /database/ # => false, we forgot to add the database
dbc.read =~ /database/ # => false, we forgot to add the database
dbc.append("\ndatabase: mysql") # let's do it

dbc.update do |content| # and add host info
Expand Down
Loading

0 comments on commit 0df835e

Please sign in to comment.