class Stow::Local::File

Attributes

path[R]
store[R]

Public Class Methods

new(store, path) click to toggle source
# File lib/stow/local.rb, line 27
def initialize(store, path)
  @store = store
  @path = path
  return if absolute.start_with?(store.base) 
  raise OutsideBaseError, "Path '#{absolute}' leads outside of base '#{store.base}'"
end

Public Instance Methods

body() click to toggle source
# File lib/stow/local.rb, line 42
def body
  case @type
  when :string
    @body
  when :io
    @type = :string
    @body = @body.read
  when :file
    @type = :string
    @body = ::File.open(@body, 'rb') { |file| file.read }
  end
end
body=(content) click to toggle source
# File lib/stow/local.rb, line 34
def body=(content)
  @body = content
  @type = if content.respond_to?(:path) then :file
  elsif content.respond_to?(:read) then :io
  else :string
  end
end
copy(options = {}) click to toggle source
# File lib/stow/local.rb, line 65
def copy(options = {})
  path = options[:path]
  store = options[:store] || self.store
  raise ArgumentError, 'Must supply path' unless path
  copy = store.class::File.new(store, path)
  ::File.open(absolute, 'rb') do |file|
    copy.body = file
    copy.save
  end
  copy
end
destroy() click to toggle source
# File lib/stow/local.rb, line 82
def destroy
  return unless persisted?
  FileUtils.rm absolute
  dir = ::File.dirname(absolute)
  Dir.rmdir(dir) if (Dir.entries(dir) - %w(. ..)).empty?
end
save() click to toggle source
# File lib/stow/local.rb, line 55
def save
  FileUtils.mkdir_p(::File.dirname(absolute))
  case @type
  when :string then ::File.open(absolute, 'wb') { |f| f.write(@body) }
  when :file then FileUtils.copy_file(@body.path, absolute)
  when :io then ::File.open(absolute, 'wb') { |f| f.write(@body.read) }
  else raise TypeError, "Can't write #{@body.inspect}" unless persisted?
  end
end
size() click to toggle source
# File lib/stow/local.rb, line 89
def size
  case @type
  when :string, :io then @body.size
  when :file then ::File.size(@body.path)
  else persisted? ? ::File.size(absolute) : 0
  end
end
update(attributes) click to toggle source
# File lib/stow/local.rb, line 77
def update(attributes)
  self.body = attributes[:body]
  save
end

Private Instance Methods

absolute() click to toggle source
# File lib/stow/local.rb, line 103
def absolute
  @absolute ||= ::File.expand_path(path, store.base)
end
persisted?() click to toggle source
# File lib/stow/local.rb, line 99
def persisted?
  ::File.file?(absolute)
end