Class: Net::FTP::List::Entry

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/net/ftp/list/entry.rb

Overview

Represents an entry of the FTP list. Gets returned when you parse a list.

Constant Summary

ALLOWED_ATTRIBUTES =

:nodoc:

[:raw, :basename, :dir, :file, :symlink, :mtime, :filesize, :device, :server_type]

Instance Method Summary collapse

Constructor Details

#initialize(raw_ls_line, optional_attributes = {}) ⇒ Entry

Create a new entry object. The additional argument is the list of metadata keys that can be used on the object. By default just takes and set the raw list entry.

Net::FTP::List.parse(raw_list_string) # => Net::FTP::List::Parser instance.


10
11
12
13
14
15
16
# File 'lib/net/ftp/list/entry.rb', line 10

def initialize(raw_ls_line, optional_attributes = {}) #:nodoc:
  @raw = raw_ls_line
  optional_attributes.each_pair do |key, value|
    raise ArgumentError, "#{key} is not supported" unless ALLOWED_ATTRIBUTES.include?(key)
    instance_variable_set("@#{key}", value)
  end
end

Instance Method Details

#<=>(other) ⇒ Fixnum

Compares the receiver against another object.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
# File 'lib/net/ftp/list/entry.rb', line 38

def <=>(other)
  if other.instance_of? self.class
    return self.filesize <=> other.filesize
  elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float
    return self.filesize <=> other
  end
  raise ArgumentError.new('comparison of %s with %s failed!' % [self.class, other.class])
end

#basenameObject Also known as: name

The items basename (filename).



54
55
56
# File 'lib/net/ftp/list/entry.rb', line 54

def basename
  @basename ||= ''
end

#device?Boolean

Looks like a device.



76
77
78
# File 'lib/net/ftp/list/entry.rb', line 76

def device?
  !!(@device ||= false)
end

#dir?Boolean Also known as: directory?

Looks like a directory, try CWD.



60
61
62
# File 'lib/net/ftp/list/entry.rb', line 60

def dir?
  !!(@dir ||= false)
end

#eql?(other) ⇒ true, false

Tests for objects equality (value and type).



24
25
26
27
28
29
# File 'lib/net/ftp/list/entry.rb', line 24

def eql?(other)
  return false if !other.instance_of? self.class
  return true if self.object_id == other.object_id

  self.raw == other.raw # if it's exactly the same line then the objects are the same
end

#file?Boolean

Looks like a file, try RETR.



66
67
68
# File 'lib/net/ftp/list/entry.rb', line 66

def file?
  !!(@file ||= false)
end

#filesizeObject Also known as: size

Returns the filesize of the entry or 0 for directorties



86
87
88
# File 'lib/net/ftp/list/entry.rb', line 86

def filesize
  @filesize || 0
end

#mtimeObject

Returns the modification time of the file/directory or the current time if unknown



81
82
83
# File 'lib/net/ftp/list/entry.rb', line 81

def mtime
  @mtime || Time.now
end

#rawObject Also known as: to_s

The raw list entry string.



48
49
50
# File 'lib/net/ftp/list/entry.rb', line 48

def raw
  @raw ||= ''
end

#server_typeObject

Returns the detected server type if this entry



92
93
94
# File 'lib/net/ftp/list/entry.rb', line 92

def server_type
  @server_type || "Unknown"
end

#symlink?Boolean

Looks like a symbolic link.



71
72
73
# File 'lib/net/ftp/list/entry.rb', line 71

def symlink?
  !!(@symlink ||= false)
end

#unknown?Boolean



96
97
98
# File 'lib/net/ftp/list/entry.rb', line 96

def unknown?
  @dir.nil? && @file.nil? && @symlink.nil? && @device.nil?
end