Class: Pione::Location::DataLocation

Inherits:
BasicLocation show all
Defined in:
lib/pione/location/data-location.rb

Constant Summary collapse

KNOWN_ATTRS =
[:need_caching, :real_appendable, :writable]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from BasicLocation

#address

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicLocation

location_type

Constructor Details

#initialize(uri) ⇒ DataLocation

Create a location with the URI.

Parameters:

  • uri (URI)

    location URI

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# 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

Class Attribute Details

.schemeString (readonly)

Returns location's scheme name.

Returns:

  • (String)

    location's scheme name



11
12
13
# File 'lib/pione/location/data-location.rb', line 11

def scheme
  @scheme
end

Instance Attribute Details

#pathPathname (readonly)

Returns path of the location.

Returns:

  • (Pathname)

    path of the location



59
60
61
# File 'lib/pione/location/data-location.rb', line 59

def path
  @path
end

#uriURI (readonly)

Returns URI of the location.

Returns:

  • (URI)

    URI of the location



55
56
57
# File 'lib/pione/location/data-location.rb', line 55

def uri
  @uri
end

Class Method Details

.define(name, val) ⇒ Object

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


28
29
30
31
32
33
34
# 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

.need_caching?Boolean

Return true if the location needs caching.

Returns:

  • (Boolean)


37
38
39
# File 'lib/pione/location/data-location.rb', line 37

def need_caching?
  @attr[:need_caching]
end

.real_appendable?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/pione/location/data-location.rb', line 41

def real_appendable?
  @attr[:real_appendable]
end

.set_scheme(name) ⇒ Object

Declare the name as location scheme.

Parameters:

  • name (String)

    scheme name



17
18
19
20
# File 'lib/pione/location/data-location.rb', line 17

def set_scheme(name)
  @scheme = name
  SCHEMES[name] = self
end

.writable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/pione/location/data-location.rb', line 45

def writable?
  @writable
end

Instance Method Details

#+(name) ⇒ BasicLocation

Create new location appended the name.

Parameters:

  • name (String)

    filename or directory name

Returns:



93
94
95
# File 'lib/pione/location/data-location.rb', line 93

def +(name)
  self.class.new(@uri.as_directory + name.to_s)
end

#==(other) ⇒ Object Also known as: eql?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



368
369
370
371
# 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) ⇒ void

This method returns an undefined value.

Append data to the location data.

Parameters:

  • data (String)

    data content



179
180
181
182
183
184
185
186
187
# 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

#as_directoryBasicLocation

Create new location that has URI as a directory.

Returns:



101
102
103
# File 'lib/pione/location/data-location.rb', line 101

def as_directory
  self.class.new(@uri.as_directory)
end

#basename(suffix = "") ⇒ String

Return the basename of the location.

Parameters:

  • suffix (String) (defaults to: "")

    suffix name

Returns:

  • (String)

    basename



111
112
113
# File 'lib/pione/location/data-location.rb', line 111

def basename(suffix="")
  File.basename(@path, suffix)
end

#cached?Boolean

Return true if the location is cached.

Returns:

  • (Boolean)

    true if the location is cached



147
148
149
# File 'lib/pione/location/data-location.rb', line 147

def cached?
  System::FileCache.cached?(self)
end

#copy(dest) ⇒ void

This method returns an undefined value.

Copy location's content to the destination.

Parameters:

Raises:

  • (NotImplementedError)


325
326
327
# File 'lib/pione/location/data-location.rb', line 325

def copy(dest)
  raise NotImplementedError
end

#create(data) ⇒ void

This method returns an undefined value.

Creates a file at the location. If a file exists at the location aleady, it raises an exception.

Parameters:

  • data (String)

    data content

Raises:

  • (NotImplementedError)


170
171
172
# File 'lib/pione/location/data-location.rb', line 170

def create(data)
  raise NotImplementedError
end

#ctimeTime

Return ctime of the location.

Returns:

  • (Time)

    ctime

Raises:

  • (NotImplementedError)


217
218
219
# File 'lib/pione/location/data-location.rb', line 217

def ctime
  raise NotImplementedError
end

#deletevoid

This method returns an undefined value.

Delete data of the location.

Raises:

  • (NotImplementedError)


209
210
211
# File 'lib/pione/location/data-location.rb', line 209

def delete
  raise NotImplementedError
end

#directory?Boolean

Return true if data at the location is a directory. When there exists no files and no direcotries, then return false.

Returns:

  • (Boolean)

    true if data at the location is a directory

Raises:

  • (NotImplementedError)


300
301
302
# File 'lib/pione/location/data-location.rb', line 300

def directory?
  raise NotImplementedError
end

#directory_entriesArray<Location>

Return directory entries of the location.

Returns:



272
273
274
275
276
# 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

#dirnameObject

Return the dirname of location. This method returns it as a location.



124
125
126
# File 'lib/pione/location/data-location.rb', line 124

def dirname
  rebuild(@path.dirname).as_directory
end

#entries(&b) ⇒ Array<Location>

Return entries of the location.

Returns:

Raises:

  • (NotImplementedError)


248
249
250
# File 'lib/pione/location/data-location.rb', line 248

def entries(&b)
  raise NotImplementedError
end

#exist?Boolean

Return true if there is data in the location.

Returns:

  • (Boolean)

    if there is data in the location

Raises:

  • (NotImplementedError)


282
283
284
# File 'lib/pione/location/data-location.rb', line 282

def exist?
  raise NotImplementedError
end

#extnameObject

Return the extension name of location.

Returns:

  • the extension name of location



119
120
121
# File 'lib/pione/location/data-location.rb', line 119

def extname
  File.extname(basename)
end

#file?Boolean

Return true if data at the location is a file. When there exists no files and no directories, then return false.

Returns:

  • (Boolean)

    true if data at the location is a file

Raises:

  • (NotImplementedError)


291
292
293
# File 'lib/pione/location/data-location.rb', line 291

def file?
  raise NotImplementedError
end

#file_entriesArray<Location>

Return file entries of the location.

Returns:



264
265
266
# File 'lib/pione/location/data-location.rb', line 264

def file_entries
  entries.select{|entry| entry.file?}
end

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



375
376
377
# File 'lib/pione/location/data-location.rb', line 375

def hash
  @uri.hash
end

#inspectObject Also known as: to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



362
363
364
# File 'lib/pione/location/data-location.rb', line 362

def inspect
  "#<%s %s:%s>" % [self.class, scheme, @path.to_s]
end

This method returns an undefined value.

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.

Parameters:

Raises:

  • (NotImplementedError)


336
337
338
# File 'lib/pione/location/data-location.rb', line 336

def link(dest)
  raise NotImplementedError
end

#localObject

Copy the content to temporary local location and return the location. If the scheme is local, return itself.



74
75
76
77
78
79
80
# 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

#local?Boolean

Return true if scheme of the location is local.

Returns:

  • (Boolean)


83
84
85
# File 'lib/pione/location/data-location.rb', line 83

def local?
  scheme == "local"
end

#mkdirvoid

This method returns an undefined value.

Make the path a directory.

Raises:

  • (NotImplementedError)


307
308
309
# File 'lib/pione/location/data-location.rb', line 307

def mkdir
  raise NotImplementedError
end

#move(dest) ⇒ void

This method returns an undefined value.

Move to the destination.

Parameters:

Raises:

  • (NotImplementedError)


316
317
318
# File 'lib/pione/location/data-location.rb', line 316

def move(dest)
  raise NotImplementedError
end

#mtimeTime

Return mtime of the location.

Returns:

  • (Time)

    mtime

Raises:

  • (NotImplementedError)


225
226
227
# File 'lib/pione/location/data-location.rb', line 225

def mtime
  raise NotImplementedError
end

#mtime=(time) ⇒ void

This method returns an undefined value.

Set mtime of the location.

Raises:

  • (NotImplementedError)


232
233
234
# File 'lib/pione/location/data-location.rb', line 232

def mtime=(time)
  raise NotImplementedError
end

#readString

Read location data.

Returns:

  • (String)

    data content

Raises:

  • (NotImplementedError)


193
194
195
# File 'lib/pione/location/data-location.rb', line 193

def read
  raise NotImplementedError
end

#rebuild(path) ⇒ Location

Rebuild location with the path.

Parameters:

  • path (Pathname)

    new path

Returns:



134
135
136
137
138
139
140
141
# 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

#rel_entries(option) ⇒ Array<String>

Return relative entries of the location.

Returns:

  • (Array<String>)

    entries of the location

Raises:

  • (NotImplementedError)


256
257
258
# File 'lib/pione/location/data-location.rb', line 256

def rel_entries(option)
  raise NotImplementedError
end

#sha1String

Return the digest string by SHA1.

Returns:

  • (String)

    hex-string of the file



353
354
355
356
357
358
359
# 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

#sizeInteger

Return byte size of data in the location.

Returns:

  • (Integer)

    byte size of data

Raises:

  • (NotImplementedError)


240
241
242
# File 'lib/pione/location/data-location.rb', line 240

def size
  raise NotImplementedError
end

#turn(dest) ⇒ void

This method returns an undefined value.

Move data to the destination and link self to it.

Parameters:

Raises:

  • (NotImplementedError)


345
346
347
# File 'lib/pione/location/data-location.rb', line 345

def turn(dest)
  raise NotImplementedError
end

#update(data) ⇒ void

This method returns an undefined value.

Update with the data.

Parameters:

  • data (String)

    new data content

Raises:

  • (NotImplementedError)


202
203
204
# File 'lib/pione/location/data-location.rb', line 202

def update(data)
  raise NotImplementedError
end

#write(data) ⇒ void

This method returns an undefined value.

Write a data into the location.

Parameters:

  • data (String)

    data content



156
157
158
159
160
161
162
# File 'lib/pione/location/data-location.rb', line 156

def write(data)
  if exist?
    update(data)
  else
    create(data)
  end
end