class Rush::Entry

Rush::Entry is the base class for Rush::File and Rush::Dir. One or more of these is instantiated whenever you use square brackets to access the filesystem on a box, as well as any other operation that returns an entry or list of entries.

Attributes

box[R]
name[R]
path[R]

Public Class Methods

factory(full_path, box=nil) click to toggle source

The factory checks to see if the full path has a trailing slash for creating a Rush::Dir rather than the default Rush::File.

# File lib/rush/entry.rb, line 30
def self.factory(full_path, box=nil)
  if full_path.tail(1) == '/'
    Rush::Dir.new(full_path, box)
  elsif File.directory?(full_path)
    Rush::Dir.new(full_path, box)
  else
    Rush::File.new(full_path, box)
  end
end
new(full_path, box=nil) click to toggle source

Initialize with full path to the file or dir, and the box it resides on.

# File lib/rush/entry.rb, line 9
def initialize(full_path, box=nil)
  full_path = ::File.expand_path(full_path, '/')
  @path = ::File.dirname(full_path)
  @name = ::File.basename(full_path)
  @box = box || Rush::Box.new('localhost')
end

Public Instance Methods

access() click to toggle source

Returns a hash with up to nine values, combining user/group/other with read/write/execute. The key is omitted if the value is false.

Examples:

entry.access                   # -> { :user_can_read => true, :user_can_write => true, :group_can_read => true }
entry.access[:other_can_read]  # -> true or nil
# File lib/rush/entry.rb, line 185
def access
  Rush::Access.new.from_octal(stat[:mode]).display_hash
end
access=(options) click to toggle source

Set the access permissions for the entry.

Permissions are set by role and permissions combinations which can be specified individually or grouped together. :user_can => :read, :user_can => :write is the same as :user_can => :read_write.

You can also insert 'and' if you find it reads better, like :user_and_group_can => :read_and_write.

Any permission excluded is set to deny access. The access call does not set partial permissions which combine with the existing state of the entry, like “chmod o+r” would.

Examples:

file.access = { :user_can => :read_write, :group_other_can => :read }
dir.access = { :user => 'adam', :group => 'users', :read_write_execute => :user_group }
# File lib/rush/entry.rb, line 173
def access=(options)
  connection.set_access(full_path, Rush::Access.parse(options))
end
changed_at() click to toggle source

Timestamp of most recent change to the entry (permissions, contents, etc).

# File lib/rush/entry.rb, line 78
def changed_at
  stat[:ctime]
end
chown(user = nil, group = nil, options = {}) click to toggle source

Change entry ownership

Changes owner and group on the named files (in list) to the user user and the group group. user and group may be an ID (Integer/String) or a name (String). If user or group is nil, this method does not change the attribute.

@param user [string/integer] The user to own the file @param group [string/integer] The group to own the file @param options [hash] the options to pass to FileUtils.chown (eg. 'noop', 'verbose' or 'recursive' )

# File lib/rush/entry.rb, line 197
def chown(user = nil, group = nil, options = {})
  connection.chown(full_path, user, group, options)
  self
end
chown_R(user = nil, group = nil, options = {}) click to toggle source

Shortcut to Entry::chown to pass the 'recursive' option by default

# File lib/rush/entry.rb, line 204
def chown_R(user = nil, group = nil, options = {})
  options[:recursive] = true
  chown(user, group, options)
end
connection() click to toggle source
# File lib/rush/entry.rb, line 52
def connection
  box ? box.connection : Rush::Connection::Local.new
end
copy_to(dir) click to toggle source

Copy the entry to another dir. Returns an object representing the new copy.

# File lib/rush/entry.rb, line 113
def copy_to(dir)
  raise Rush::NotADir unless dir.class == Rush::Dir

  if box == dir.box
    connection.copy(full_path, dir.full_path)
  else
    archive = connection.read_archive(full_path)
    dir.box.connection.write_archive(archive, dir.full_path)
  end

  new_full_path = "#{dir.full_path}#{name}"
  self.class.new(new_full_path, dir.box)
end
destroy() click to toggle source

Destroy the entry. If it is a dir, everything inside it will also be destroyed.

# File lib/rush/entry.rb, line 225
def destroy
  connection.destroy(full_path)
end
duplicate(new_name) click to toggle source

Rename an entry to another name within the same dir. The existing object will not be affected, but a new object representing the newly-created entry will be returned.

# File lib/rush/entry.rb, line 104
def duplicate(new_name)
  raise Rush::NameCannotContainSlash if new_name.match(/\//)
  new_full_path = "#{@path}/#{new_name}"
  connection.copy(full_path, new_full_path)
  self.class.new(new_full_path, box)
end
executables() click to toggle source
# File lib/rush/entry.rb, line 24
def executables
  Rush::Path.executables
end
exists?() click to toggle source

Return true if the entry currently exists on the filesystem of the box.

# File lib/rush/entry.rb, line 70
def exists?
  stat
  true
rescue Rush::DoesNotExist
  false
end
full_path() click to toggle source
# File lib/rush/entry.rb, line 61
def full_path
  "#{path}/#{name}"
end
hidden?() click to toggle source

Unix convention considers entries starting with a . to be hidden.

# File lib/rush/entry.rb, line 153
def hidden?
  name.slice(0, 1) == '.'
end
last_accessed() click to toggle source

Timestamp that entry was last accessed (read from or written to).

# File lib/rush/entry.rb, line 88
def last_accessed
  stat[:atime]
end
last_modified() click to toggle source

Timestamp of last modification of the contents.

# File lib/rush/entry.rb, line 83
def last_modified
  stat[:mtime]
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/rush/entry.rb, line 16
def method_missing(meth, *args, &block)
  if executables.include? meth.to_s
    open_with meth, *args
  else
    super
  end
end
move_to(dir) click to toggle source

Move the entry to another dir. The object will be updated to show its new location.

# File lib/rush/entry.rb, line 129
def move_to(dir)
  moved = copy_to(dir)
  destroy
  mimic(moved)
end
mv(new_name)
Alias for: rename
owner() click to toggle source

Chown in ruby way. Ruby way is creating accessors.

# File lib/rush/entry.rb, line 210
def owner
  stat = ::File.stat(full_path)
  { user: Etc.getpwuid(stat.uid).name, group: Etc.getgrgid(stat.gid).name }
end
owner=(params) click to toggle source
# File lib/rush/entry.rb, line 215
def owner=(params)
  case params
  when Hash then chown(params.delete(:user), params.delete(:group), params)
  when String then chown(params)
  when Numeric then chown(Etc.getpwuid(params).name)
  else raise 'Something wrong with params for chown'
  end
end
parent() click to toggle source

The parent dir. For example, box.parent == box

# File lib/rush/entry.rb, line 57
def parent
  @parent ||= Rush::Dir.new(@path)
end
quoted_path() click to toggle source
# File lib/rush/entry.rb, line 65
def quoted_path
  Rush.quote(full_path)
end
rename(new_name) click to toggle source

Rename an entry to another name within the same dir. The object's name will be updated to match the change on the filesystem.

# File lib/rush/entry.rb, line 94
def rename(new_name)
  connection.rename(@path, @name, new_name)
  @name = new_name
  self
end
Also aliased as: mv

Private Instance Methods

stat() click to toggle source
# File lib/rush/entry.rb, line 235
def stat
  connection.stat(full_path)
end