class Pione::Location::DataLocation
Constants
- KNOWN_ATTRS
Attributes
@return [String]
location's scheme name
@return [Pathname]
path of the location
@return [URI]
URI of the location
Public Class Methods
Define a location’s attribute.
-
need_caching : whether the location needs to be cached or not
-
real_appendable : whether the location can appendable or not
-
writable : whether the location is writable or not
# File lib/pione/location/data-location.rb, line 28 def define(name, val) if DataLocation::KNOWN_ATTRS.include?(name) (@attr ||= Hash.new)[name] = val else raise ArgumentError.new(name) end end
Return true if the location needs caching.
# File lib/pione/location/data-location.rb, line 37 def need_caching? @attr[:need_caching] end
Create a location with the URI
.
@param uri [URI]
location URI
# File lib/pione/location/data-location.rb, line 65 def initialize(uri) @address = uri.to_s @uri = uri.kind_of?(URI::Generic) ? uri : URI.parse(uri) @path = Pathname.new(uri.path) raise ArgumentError.new(uri) unless @uri.scheme = scheme end
# File lib/pione/location/data-location.rb, line 41 def real_appendable? @attr[:real_appendable] end
Declare the name as location scheme.
@param name [String]
scheme name
# File lib/pione/location/data-location.rb, line 17 def set_scheme(name) @scheme = name SCHEMES[name] = self end
# File lib/pione/location/data-location.rb, line 45 def writable? @writable end
Public Instance Methods
Create new location appended the name.
@param name [String]
filename or directory name
@return [BasicLocation]
new location
# File lib/pione/location/data-location.rb, line 93 def +(name) self.class.new(@uri.as_directory + name.to_s) end
@api private
# File lib/pione/location/data-location.rb, line 368 def ==(other) return false unless other.kind_of?(self.class) @uri == other.uri end
Append data to the location data.
@param data [String]
data content
@return [void]
# File lib/pione/location/data-location.rb, line 179 def append(data) if real_appendable? raise NotImplmentedError else _local = local _local.append(data) _local.copy(self) end end
Create new location that has URI
as a directory.
@return [BasicLocation]
new location
# File lib/pione/location/data-location.rb, line 101 def as_directory self.class.new(@uri.as_directory) end
Return the basename of the location.
@param suffix [String]
suffix name
@return [String]
basename
# File lib/pione/location/data-location.rb, line 111 def basename(suffix="") File.basename(@path, suffix) end
Return true if the location is cached.
@return [Boolean]
true if the location is cached
# File lib/pione/location/data-location.rb, line 147 def cached? System::FileCache.cached?(self) end
Copy location’s content to the destination.
@param dest [BasicLocation]
destination
@return [void]
# File lib/pione/location/data-location.rb, line 325 def copy(dest) raise NotImplementedError end
Creates a file at the location. If a file exists at the location aleady, it raises an exception.
@param data [String]
data content
@return [void]
# File lib/pione/location/data-location.rb, line 170 def create(data) raise NotImplementedError end
Return ctime of the location.
@return [Time]
ctime
# File lib/pione/location/data-location.rb, line 217 def ctime raise NotImplementedError end
Delete data of the location.
@return [void]
# File lib/pione/location/data-location.rb, line 209 def delete raise NotImplementedError end
Return true if data at the location is a directory. When there exists no files and no direcotries, then return false.
@return [Boolean]
true if data at the location is a directory
# File lib/pione/location/data-location.rb, line 300 def directory? raise NotImplementedError end
Return directory entries of the location.
@return [Array<Location>]
directory entries of the location
# File lib/pione/location/data-location.rb, line 272 def directory_entries entries.select do |entry| entry.directory? and not(entry.path.basename == "." or entry.path.basename == "..") end end
Return the dirname of location. This method returns it as a location.
# File lib/pione/location/data-location.rb, line 124 def dirname rebuild(@path.dirname).as_directory end
Return entries of the location.
@return [Array<Location>]
entries of the location
# File lib/pione/location/data-location.rb, line 248 def entries(&b) raise NotImplementedError end
Return true if there is data in the location.
@return [Boolean]
if there is data in the location
# File lib/pione/location/data-location.rb, line 282 def exist? raise NotImplementedError end
Return the extension name of location.
@return
the extension name of location
# File lib/pione/location/data-location.rb, line 119 def extname File.extname(basename) end
Return true if data at the location is a file. When there exists no files and no directories, then return false.
@return [Boolean]
true if data at the location is a file
# File lib/pione/location/data-location.rb, line 291 def file? raise NotImplementedError end
Return file entries of the location.
@return [Array<Location>]
file entries of the location
# File lib/pione/location/data-location.rb, line 264 def file_entries entries.select{|entry| entry.file?} end
@api private
# File lib/pione/location/data-location.rb, line 375 def hash @uri.hash end
@api private
# File lib/pione/location/data-location.rb, line 362 def inspect "#<%s %s:%s>" % [self.class, scheme, @path.to_s] end
Link to the destination. If the location scheme is same to destination, create link by a symbolic link or lightweight copy method. If not, copy it simply.
@param dest [BasicLocation]
destination
@return [void]
# File lib/pione/location/data-location.rb, line 336 def link(dest) raise NotImplementedError end
Copy the content to temporary local location and return the location. If the scheme is local, return itself.
# File lib/pione/location/data-location.rb, line 74 def local if scheme == "local" self else Location[Temppath.create].tap {|tmp| copy(tmp) if exist?} end end
Return true if scheme of the location is local.
# File lib/pione/location/data-location.rb, line 83 def local? scheme == "local" end
Make the path a directory.
@return [void]
# File lib/pione/location/data-location.rb, line 307 def mkdir raise NotImplementedError end
Move to the destination.
@param dest [BasicLocation]
destination
@return [void]
# File lib/pione/location/data-location.rb, line 316 def move(dest) raise NotImplementedError end
Return mtime of the location.
@return [Time]
mtime
# File lib/pione/location/data-location.rb, line 225 def mtime raise NotImplementedError end
Set mtime of the location.
@return [void]
# File lib/pione/location/data-location.rb, line 232 def mtime=(time) raise NotImplementedError end
Read location data.
@return [String]
data content
# File lib/pione/location/data-location.rb, line 193 def read raise NotImplementedError end
Rebuild location with the path.
@param path [Pathname]
new path
@return [Location]
location with new path
# File lib/pione/location/data-location.rb, line 134 def rebuild(path) scheme = @uri.scheme auth = @uri.user and @uri.password ? "%s:%s@" % [@uri.user, @uri.password] : "" host = @uri.host port = @uri.port ? ":%i" % @uri.port : "" path = path.expand_path("/").to_s Location["%s://%s%s%s%s" % [scheme, auth, host, port, path]] end
Return relative entries of the location.
@return [Array<String>]
entries of the location
# File lib/pione/location/data-location.rb, line 256 def rel_entries(option) raise NotImplementedError end
Return the digest string by SHA1.
@return [String]
hex-string of the file
# File lib/pione/location/data-location.rb, line 353 def sha1 if file? Digest::SHA1.file(local.path) else raise InvalidFileOperation.new(self) end end
Return byte size of data in the location.
@return [Integer]
byte size of data
# File lib/pione/location/data-location.rb, line 240 def size raise NotImplementedError end
Move data to the destination and link self to it.
@param dest [BasicLocation]
destination
@return [void]
# File lib/pione/location/data-location.rb, line 345 def turn(dest) raise NotImplementedError end
Update with the data.
@param data [String]
new data content
@return [void]
# File lib/pione/location/data-location.rb, line 202 def update(data) raise NotImplementedError end
Write a data into the location.
@param data [String]
data content
@return [void]
# File lib/pione/location/data-location.rb, line 156 def write(data) if exist? update(data) else create(data) end end