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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_ls_line, basename: nil, type: nil, mtime: nil, filesize: nil, server_type: nil, symlink_destination: nil) ⇒ 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.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/net/ftp/list/entry.rb', line 13

def initialize(raw_ls_line,
  basename: nil,
  type: nil,
  mtime: nil,
  filesize: nil,
  server_type: nil,
  symlink_destination: nil) #:nodoc:
  @raw = raw_ls_line.respond_to?(:force_encoding) ? raw_ls_line.force_encoding('utf-8') : raw_ls_line
  @basename = basename || ''
  @type = type
  @mtime = mtime || Time.now
  @filesize = filesize || 0
  @server_type = server_type || 'Unknown'
  @symlink_destination = symlink_destination
end

Instance Attribute Details

#basenameObject (readonly) Also known as: name

Returns the value of attribute basename.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def basename
  @basename
end

#filesizeObject (readonly) Also known as: size

Returns the value of attribute filesize.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def filesize
  @filesize
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def mtime
  @mtime
end

#rawObject (readonly) Also known as: to_s

Returns the value of attribute raw.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def raw
  @raw
end

#server_typeObject (readonly)

Returns the value of attribute server_type.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def server_type
  @server_type
end

Returns the value of attribute symlink_destination.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def symlink_destination
  @symlink_destination
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/net/ftp/list/entry.rb', line 5

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Integer

Compares the receiver against another object.

Parameters:

Returns:

  • (Integer)

    -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
# File 'lib/net/ftp/list/entry.rb', line 48

def <=>(other)
  case other
  when ::Net::FTP::List::Entry
    return filesize <=> other.filesize
  when Numeric
    return filesize <=> other
  end

  raise ArgumentError, format('comparison of %<self>s with %<other>s failed!', self: self.class, other: other.class)
end

#device?Boolean

Looks like a device.

Returns:

  • (Boolean)


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

def device?
  type == :device
end

#dir?Boolean Also known as: directory?

Looks like a directory, try CWD.

Returns:

  • (Boolean)


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

def dir?
  type == :dir
end

#eql?(other) ⇒ true, false

Tests for objects equality (value and type).

Parameters:

Returns:

  • (true, false)

    true if the objects are equal and have the same type; false otherwise.



35
36
37
38
39
40
# File 'lib/net/ftp/list/entry.rb', line 35

def eql?(other)
  return false unless other.is_a?(::Net::FTP::List::Entry)
  return true if equal?(other)

  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.

Returns:

  • (Boolean)


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

def file?
  type == :file
end

#symlink?Boolean

Looks like a symbolic link.

Returns:

  • (Boolean)


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

def symlink?
  type == :symlink
end

#unknown?Boolean

Is the entry type unknown?

Returns:

  • (Boolean)


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

def unknown?
  !dir? && !file? && !symlink? && !device?
end